Search in sources :

Example 6 with SpanBuilder

use of io.opentelemetry.api.trace.SpanBuilder in project wildfly by wildfly.

the class OpenTelemetryContainerFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext) {
    if (tracer != null) {
        final boolean remoteContext = requestContext.getHeaders().keySet().stream().anyMatch(k -> W3CTraceContextPropagator.getInstance().fields().contains(k));
        final UriInfo uriInfo = requestContext.getUriInfo();
        final URI requestUri = uriInfo.getRequestUri();
        final String method = requestContext.getMethod();
        final String uri = uriInfo.getPath();
        SpanBuilder spanBuilder = tracer.spanBuilder(method + " " + uri).setSpanKind(SpanKind.SERVER);
        if (remoteContext) {
            spanBuilder.setParent(openTelemetry.getPropagators().getTextMapPropagator().extract(Context.current(), requestContext, new TextMapGetter<ContainerRequestContext>() {

                @Override
                public String get(ContainerRequestContext requestContext, String key) {
                    if (requestContext.getHeaders().containsKey(key)) {
                        return requestContext.getHeaders().get(key).get(0);
                    }
                    return null;
                }

                @Override
                public Iterable<String> keys(ContainerRequestContext requestContext) {
                    return requestContext.getHeaders().keySet();
                }
            }));
        } else {
            spanBuilder.setNoParent();
        }
        Span serverSpan = spanBuilder.startSpan();
        serverSpan.makeCurrent();
        serverSpan.setAttribute(SemanticAttributes.HTTP_METHOD, method);
        serverSpan.setAttribute(SemanticAttributes.HTTP_SCHEME, requestUri.getScheme());
        serverSpan.setAttribute(SemanticAttributes.HTTP_HOST, requestUri.getHost() + ":" + requestUri.getPort());
        serverSpan.setAttribute(SemanticAttributes.HTTP_TARGET, uri);
        requestContext.setProperty(SERVER_SPAN, serverSpan);
    }
}
Also used : SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) TextMapGetter(io.opentelemetry.context.propagation.TextMapGetter) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) URI(java.net.URI) Span(io.opentelemetry.api.trace.Span) UriInfo(javax.ws.rs.core.UriInfo)

Example 7 with SpanBuilder

use of io.opentelemetry.api.trace.SpanBuilder in project accumulo by apache.

the class TraceUtil method startSpan.

private static Span startSpan(Class<?> caller, String spanName, SpanKind kind, Map<String, String> attributes, TInfo tinfo) {
    if (!enabled) {
        return Span.getInvalid();
    }
    final String name = String.format(SPAN_FORMAT, caller.getSimpleName(), spanName);
    final SpanBuilder builder = getTracer(getOpenTelemetry()).spanBuilder(name);
    if (kind != null) {
        builder.setSpanKind(kind);
    }
    if (attributes != null) {
        attributes.forEach(builder::setAttribute);
    }
    if (tinfo != null) {
        builder.setParent(getContext(tinfo));
    }
    return builder.startSpan();
}
Also used : SpanBuilder(io.opentelemetry.api.trace.SpanBuilder)

Aggregations

SpanBuilder (io.opentelemetry.api.trace.SpanBuilder)7 AttributeKey (io.opentelemetry.api.common.AttributeKey)5 Span (io.opentelemetry.api.trace.Span)1 TextMapGetter (io.opentelemetry.context.propagation.TextMapGetter)1 URI (java.net.URI)1 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)1 UriInfo (javax.ws.rs.core.UriInfo)1 IpcClientSpanBuilder (org.apache.hadoop.hbase.client.trace.IpcClientSpanBuilder)1