Search in sources :

Example 1 with TracingContext

use of io.helidon.tracing.jersey.client.internal.TracingContext in project helidon by oracle.

the class MpTracingContextFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext) {
    ServerRequest serverRequest = this.request.get();
    Tracer tracer = serverRequest.tracer();
    Optional<SpanContext> parentSpan = serverRequest.spanContext();
    boolean clientEnabled = config.getOptionalValue("tracing.client.enabled", Boolean.class).orElse(true);
    TracingContext tracingContext = TracingContext.create(tracer, serverRequest.headers().toMap(), clientEnabled);
    parentSpan.ifPresent(tracingContext::parentSpan);
    Contexts.context().ifPresent(ctx -> ctx.register(tracingContext));
}
Also used : TracingContext(io.helidon.tracing.jersey.client.internal.TracingContext) SpanContext(io.opentracing.SpanContext) Tracer(io.opentracing.Tracer) ServerRequest(io.helidon.webserver.ServerRequest)

Example 2 with TracingContext

use of io.helidon.tracing.jersey.client.internal.TracingContext 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 3 with TracingContext

use of io.helidon.tracing.jersey.client.internal.TracingContext in project helidon by oracle.

the class ClientTracingFilter method findParentSpan.

private Optional<SpanContext> findParentSpan(Tracer tracer, ClientRequestContext requestContext, Optional<TracingContext> tracingContext) {
    // parent span lookup
    // first is the configured span in request properties
    Optional<SpanContext> property = property(requestContext, SpanContext.class, CURRENT_SPAN_CONTEXT_PROPERTY_NAME);
    if (property.isPresent()) {
        return property;
    }
    // then the active span
    Span activeSpan = tracer.activeSpan();
    if (null != activeSpan) {
        return Optional.of(activeSpan.context());
    }
    // then spans registered in context
    return // from injected span context
    tracingContext.map(TracingContext::parentSpan).or(() -> Contexts.context().flatMap(ctx -> ctx.get(ClientTracingFilter.class, SpanContext.class))).or(() -> Contexts.context().flatMap(ctx -> ctx.get(SpanContext.class)));
}
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) Span(io.opentracing.Span)

Aggregations

TracingContext (io.helidon.tracing.jersey.client.internal.TracingContext)3 SpanContext (io.opentracing.SpanContext)3 Tracer (io.opentracing.Tracer)3 Contexts (io.helidon.common.context.Contexts)2 HelidonServiceLoader (io.helidon.common.serviceloader.HelidonServiceLoader)2 SpanTracingConfig (io.helidon.tracing.config.SpanTracingConfig)2 TracingConfigUtil (io.helidon.tracing.config.TracingConfigUtil)2 TracerProvider (io.helidon.tracing.spi.TracerProvider)2 Span (io.opentracing.Span)2 Format (io.opentracing.propagation.Format)2 TextMapAdapter (io.opentracing.propagation.TextMapAdapter)2 Tags (io.opentracing.tag.Tags)2 GlobalTracer (io.opentracing.util.GlobalTracer)2 Priority (jakarta.annotation.Priority)2 Priorities (jakarta.ws.rs.Priorities)2 ClientRequestContext (jakarta.ws.rs.client.ClientRequestContext)2 ClientRequestFilter (jakarta.ws.rs.client.ClientRequestFilter)2 ClientResponseContext (jakarta.ws.rs.client.ClientResponseContext)2 ClientResponseFilter (jakarta.ws.rs.client.ClientResponseFilter)2 MultivaluedMap (jakarta.ws.rs.core.MultivaluedMap)2