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));
}
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)));
}
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)));
}
Aggregations