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