Search in sources :

Example 1 with SpanTracingConfig

use of io.helidon.tracing.config.SpanTracingConfig in project helidon by oracle.

the class DbClientTracing method apply.

@Override
protected Single<DbClientServiceContext> apply(DbClientServiceContext serviceContext) {
    SpanTracingConfig spanConfig = TracingConfigUtil.spanConfig("dbclient", "statement");
    if (!spanConfig.enabled()) {
        return Single.just(serviceContext);
    }
    Context context = serviceContext.context();
    Tracer tracer = context.get(Tracer.class).orElseGet(GlobalTracer::get);
    // now if span context is missing, we build a span without a parent
    Tracer.SpanBuilder spanBuilder = tracer.buildSpan(serviceContext.statementName());
    context.get(SpanContext.class).ifPresent(spanBuilder::asChildOf);
    Span span = spanBuilder.start();
    span.setTag("db.operation", serviceContext.statementType().toString());
    if (spanConfig.logEnabled("statement", true)) {
        Tags.DB_STATEMENT.set(span, serviceContext.statement());
    }
    Tags.COMPONENT.set(span, "dbclient");
    Tags.DB_TYPE.set(span, serviceContext.dbType());
    serviceContext.statementFuture().thenAccept(nothing -> {
        if (spanConfig.logEnabled("statement-finish", true)) {
            span.log(Map.of("type", "statement"));
        }
    });
    serviceContext.resultFuture().thenAccept(count -> {
        if (spanConfig.logEnabled("result-finish", true)) {
            span.log(Map.of("type", "result", "count", count));
        }
        span.finish();
    }).exceptionally(throwable -> {
        Tags.ERROR.set(span, Boolean.TRUE);
        span.log(Map.of("event", "error", "error.kind", "Exception", "error.object", throwable, "message", throwable.getMessage()));
        span.finish();
        return null;
    });
    return Single.just(serviceContext);
}
Also used : Context(io.helidon.common.context.Context) DbClientServiceContext(io.helidon.dbclient.DbClientServiceContext) SpanContext(io.opentracing.SpanContext) DbClientServiceBase(io.helidon.dbclient.common.DbClientServiceBase) Tracer(io.opentracing.Tracer) Config(io.helidon.config.Config) TracingConfigUtil(io.helidon.tracing.config.TracingConfigUtil) Context(io.helidon.common.context.Context) GlobalTracer(io.opentracing.util.GlobalTracer) DbClientServiceContext(io.helidon.dbclient.DbClientServiceContext) Tags(io.opentracing.tag.Tags) SpanContext(io.opentracing.SpanContext) Map(java.util.Map) Single(io.helidon.common.reactive.Single) Span(io.opentracing.Span) SpanTracingConfig(io.helidon.tracing.config.SpanTracingConfig) SpanContext(io.opentracing.SpanContext) Tracer(io.opentracing.Tracer) GlobalTracer(io.opentracing.util.GlobalTracer) GlobalTracer(io.opentracing.util.GlobalTracer) Span(io.opentracing.Span) SpanTracingConfig(io.helidon.tracing.config.SpanTracingConfig)

Example 2 with SpanTracingConfig

use of io.helidon.tracing.config.SpanTracingConfig in project helidon by oracle.

the class SecurityTracing method roleMapTracing.

/**
 * Create a tracing pan for a role mapper.
 *
 * @param id role mapper identification (such as {@code idcs})
 * @return role mapper tracing (each invocation creates a new instance)
 */
public RoleMapTracing roleMapTracing(String id) {
    AtnTracing atn = atnTracing();
    String spanName = SPAN_ROLE_MAP_PREFIX + id;
    SpanTracingConfig rmTracingConfig = tracedConfig.span(SPAN_ROLE_MAP_PREFIX + id);
    Optional<Span> atnSpan = newSpan(rmTracingConfig, spanName, atn.findParent());
    return new RoleMapTracing(parentSpanContext(), parentSpan(), span(), atnSpan, atnSpanConfig);
}
Also used : Span(io.opentracing.Span) SpanTracingConfig(io.helidon.tracing.config.SpanTracingConfig)

Example 3 with SpanTracingConfig

use of io.helidon.tracing.config.SpanTracingConfig in project helidon by oracle.

the class SecurityTracing method createSecuritySpan.

private static Optional<Span> createSecuritySpan(ComponentTracingConfig componentConfig, Optional<SpanContext> parentSpan) {
    return tracer().flatMap(tracer -> {
        SpanTracingConfig spanConfig = componentConfig.span(SPAN_SECURITY);
        if (spanConfig.enabled()) {
            Tracer.SpanBuilder builder = tracer.buildSpan(spanConfig.newName().orElse(SPAN_SECURITY));
            parentSpan.ifPresent(builder::asChildOf);
            return Optional.of(builder.start());
        } else {
            return Optional.empty();
        }
    });
}
Also used : Tracer(io.opentracing.Tracer) SpanTracingConfig(io.helidon.tracing.config.SpanTracingConfig)

Example 4 with SpanTracingConfig

use of io.helidon.tracing.config.SpanTracingConfig in project helidon by oracle.

the class ClientTracingFilter method filter.

@Override
public void filter(ClientRequestContext requestContext) {
    // if we run within Jersey server, the tracing context will be filled in by TracingHelperFilter
    // if not, it will be empty
    Optional<TracingContext> tracingContext = Contexts.context().flatMap(ctx -> ctx.get(TracingContext.class));
    // maybe we are disabled
    if (tracingDisabled(requestContext, tracingContext)) {
        return;
    }
    // also we may configure tracing through other means
    SpanTracingConfig spanConfig = TracingConfigUtil.spanConfig(JAX_RS_TRACING_COMPONENT, SPAN_OPERATION_NAME);
    if (!spanConfig.enabled()) {
        return;
    }
    Tracer tracer = findTracer(requestContext, tracingContext);
    Optional<SpanContext> parentSpan = findParentSpan(tracer, requestContext, tracingContext);
    Map<String, List<String>> inboundHeaders = findInboundHeaders(tracingContext);
    String spanName = findSpanName(requestContext, spanConfig);
    // create a new span for this jersey client request
    Span currentSpan = createSpan(requestContext, tracer, parentSpan, spanName);
    // register it so we can close the span on response
    requestContext.setProperty(SPAN_PROPERTY_NAME, currentSpan);
    // and also register it with Context, so we can close the span in case of an exception that does not hit the
    // response filter
    Contexts.context().ifPresent(ctx -> ctx.register(SPAN_PROPERTY_NAME, currentSpan));
    Contexts.context().ifPresent(ctx -> ctx.register(TracingConfigUtil.OUTBOUND_SPAN_QUALIFIER, currentSpan.context()));
    // propagate tracing headers, so remote service can use currentSpan as its parent
    Map<String, List<String>> tracingHeaders = tracingHeaders(tracer, currentSpan);
    Map<String, List<String>> outboundHeaders = tracerProvider.map(provider -> provider.updateOutboundHeaders(currentSpan, tracer, parentSpan.orElse(null), tracingHeaders, inboundHeaders)).orElse(tracingHeaders);
    // add headers from inbound request that were not added by tracing provider and are needed
    // by supported proxies or routing services
    outboundHeaders = updateOutboundHeaders(outboundHeaders, inboundHeaders);
    // update the headers to be correctly propagated to remote service
    MultivaluedMap<String, Object> headers = requestContext.getHeaders();
    outboundHeaders.forEach((key, value) -> headers.put(key, new ArrayList<>(value)));
}
Also used : TracingContext(io.helidon.tracing.jersey.client.internal.TracingContext) HashMap(java.util.HashMap) ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Tags(io.opentracing.tag.Tags) ArrayList(java.util.ArrayList) ClientResponseFilter(jakarta.ws.rs.client.ClientResponseFilter) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) Map(java.util.Map) URI(java.net.URI) Priority(jakarta.annotation.Priority) Iterator(java.util.Iterator) Tracer(io.opentracing.Tracer) TracingConfigUtil(io.helidon.tracing.config.TracingConfigUtil) ClientRequestFilter(jakarta.ws.rs.client.ClientRequestFilter) ServiceLoader(java.util.ServiceLoader) GlobalTracer(io.opentracing.util.GlobalTracer) HelidonServiceLoader(io.helidon.common.serviceloader.HelidonServiceLoader) Collectors(java.util.stream.Collectors) Contexts(io.helidon.common.context.Contexts) ClientResponseContext(jakarta.ws.rs.client.ClientResponseContext) SpanContext(io.opentracing.SpanContext) Priorities(jakarta.ws.rs.Priorities) List(java.util.List) Format(io.opentracing.propagation.Format) TextMapAdapter(io.opentracing.propagation.TextMapAdapter) Optional(java.util.Optional) TracerProvider(io.helidon.tracing.spi.TracerProvider) Span(io.opentracing.Span) SpanTracingConfig(io.helidon.tracing.config.SpanTracingConfig) TracingContext(io.helidon.tracing.jersey.client.internal.TracingContext) SpanContext(io.opentracing.SpanContext) Tracer(io.opentracing.Tracer) GlobalTracer(io.opentracing.util.GlobalTracer) ArrayList(java.util.ArrayList) Span(io.opentracing.Span) SpanTracingConfig(io.helidon.tracing.config.SpanTracingConfig) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with SpanTracingConfig

use of io.helidon.tracing.config.SpanTracingConfig in project helidon by oracle.

the class AbstractTracingFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext) {
    if (!tracingEnabled(requestContext)) {
        return;
    }
    Context context = Contexts.context().orElseThrow(() -> new IllegalStateException("Context must be available in Jersey"));
    String spanName = spanName(requestContext);
    SpanTracingConfig spanConfig = TracingConfigUtil.spanConfig(ClientTracingFilter.JAX_RS_TRACING_COMPONENT, spanName, context);
    if (spanConfig.enabled()) {
        spanName = spanConfig.newName().orElse(spanName);
        Tracer tracer = context.get(Tracer.class).orElseGet(GlobalTracer::get);
        SpanContext parentSpan = context.get(ServerRequest.class, SpanContext.class).orElseGet(() -> context.get(SpanContext.class).orElse(null));
        Tracer.SpanBuilder spanBuilder = tracer.buildSpan(spanName).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER).withTag(Tags.HTTP_METHOD.getKey(), requestContext.getMethod()).withTag(Tags.HTTP_URL.getKey(), url(requestContext)).withTag(Tags.COMPONENT.getKey(), "jaxrs");
        if (null != parentSpan) {
            spanBuilder.asChildOf(parentSpan);
        }
        configureSpan(spanBuilder);
        Span span = spanBuilder.start();
        Scope spanScope = tracer.scopeManager().activate(span);
        context.register(span);
        context.register(spanScope);
        context.register(ClientTracingFilter.class, span.context());
        requestContext.setProperty(SPAN_PROPERTY, span);
        requestContext.setProperty(SPAN_SCOPE_PROPERTY, spanScope);
        if (context.get(TracingContext.class).isEmpty()) {
            context.register(TracingContext.create(tracer, requestContext.getHeaders()));
        }
        context.get(TracingContext.class).ifPresent(tctx -> tctx.parentSpan(span.context()));
        if (null == parentSpan) {
            // register current span as the parent span for other (unless already exists)
            context.register(span.context());
        }
    }
}
Also used : Context(io.helidon.common.context.Context) TracingContext(io.helidon.tracing.jersey.client.internal.TracingContext) SpanContext(io.opentracing.SpanContext) ContainerRequestContext(jakarta.ws.rs.container.ContainerRequestContext) ContainerResponseContext(jakarta.ws.rs.container.ContainerResponseContext) TracingContext(io.helidon.tracing.jersey.client.internal.TracingContext) SpanContext(io.opentracing.SpanContext) Scope(io.opentracing.Scope) Tracer(io.opentracing.Tracer) GlobalTracer(io.opentracing.util.GlobalTracer) GlobalTracer(io.opentracing.util.GlobalTracer) ServerRequest(io.helidon.webserver.ServerRequest) Span(io.opentracing.Span) SpanTracingConfig(io.helidon.tracing.config.SpanTracingConfig)

Aggregations

SpanTracingConfig (io.helidon.tracing.config.SpanTracingConfig)6 Tracer (io.opentracing.Tracer)5 Span (io.opentracing.Span)4 SpanContext (io.opentracing.SpanContext)4 GlobalTracer (io.opentracing.util.GlobalTracer)3 Context (io.helidon.common.context.Context)2 TracingConfigUtil (io.helidon.tracing.config.TracingConfigUtil)2 TracingContext (io.helidon.tracing.jersey.client.internal.TracingContext)2 Tags (io.opentracing.tag.Tags)2 Map (java.util.Map)2 Contexts (io.helidon.common.context.Contexts)1 Single (io.helidon.common.reactive.Single)1 HelidonServiceLoader (io.helidon.common.serviceloader.HelidonServiceLoader)1 Config (io.helidon.config.Config)1 DbClientServiceContext (io.helidon.dbclient.DbClientServiceContext)1 DbClientServiceBase (io.helidon.dbclient.common.DbClientServiceBase)1 TracerProvider (io.helidon.tracing.spi.TracerProvider)1 ServerRequest (io.helidon.webserver.ServerRequest)1 Scope (io.opentracing.Scope)1 Format (io.opentracing.propagation.Format)1