Search in sources :

Example 11 with SpanContext

use of io.opentracing.SpanContext in project jaeger-client-java by jaegertracing.

the class FilterIntegrationTest method testExtractorReturnsNullWhenTracerStateHeaderIsMissing.

/*
   * This test exists because opentracing's convention around missing tracer
   * state headers may change to stop supporting the automatic creation of
   * building a span.
   */
@Test
public void testExtractorReturnsNullWhenTracerStateHeaderIsMissing() {
    ContainerRequestContext reqContext = mock(ContainerRequestContext.class);
    given(reqContext.getHeaders()).willReturn(new MultivaluedHashMap<String, String>());
    ServerRequestCarrier carrier = new ServerRequestCarrier(reqContext);
    SpanContext spanCtx = tracer.extract(Format.Builtin.HTTP_HEADERS, carrier);
    assertNull(spanCtx);
}
Also used : SpanContext(io.opentracing.SpanContext) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) ServerRequestCarrier(com.uber.jaeger.filters.jaxrs2.ServerRequestCarrier) Test(org.junit.Test)

Example 12 with SpanContext

use of io.opentracing.SpanContext in project wildfly-swarm by wildfly-swarm.

the class HealthServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Tracer tracer = GlobalTracer.get();
    SpanContext context = (SpanContext) req.getAttribute(SERVER_SPAN_CONTEXT);
    tracer.buildSpan("health-check-1").asChildOf(context).startActive(true).close();
    resp.getWriter().write("alive");
}
Also used : SpanContext(io.opentracing.SpanContext) Tracer(io.opentracing.Tracer) GlobalTracer(io.opentracing.util.GlobalTracer)

Example 13 with SpanContext

use of io.opentracing.SpanContext in project wildfly-swarm by wildfly-swarm.

the class SimpleServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Tracer tracer = GlobalTracer.get();
    SpanContext context = (SpanContext) req.getAttribute(SERVER_SPAN_CONTEXT);
    tracer.buildSpan("business-operation-1").asChildOf(context).startActive(true).close();
    resp.getWriter().write("world");
}
Also used : SpanContext(io.opentracing.SpanContext) Tracer(io.opentracing.Tracer) GlobalTracer(io.opentracing.util.GlobalTracer)

Example 14 with SpanContext

use of io.opentracing.SpanContext in project ballerina by ballerina-lang.

the class StartSpanWithParentContext method execute.

@Override
public void execute(Context context) {
    String serviceName = context.getStringArgument(0);
    String spanName = context.getStringArgument(1);
    BMap tags = (BMap) context.getRefArgument(0);
    String reference = context.getRefArgument(1).stringValue();
    BStruct parentSpanContextStruct = (BStruct) context.getRefArgument(2);
    PrintStream err = System.err;
    Map<String, SpanContext> extractedSpanContextMap;
    if (ReferenceType.valueOf(reference) != ReferenceType.ROOT && parentSpanContextStruct != null && parentSpanContextStruct.getRefField(0) != null) {
        Map<String, String> parentSpanContextMap = Utils.toStringMap((BMap) parentSpanContextStruct.getRefField(0));
        extractedSpanContextMap = OpenTracerBallerinaWrapper.getInstance().extract(parentSpanContextMap);
    } else {
        extractedSpanContextMap = Collections.emptyMap();
    }
    String spanId = OpenTracerBallerinaWrapper.getInstance().startSpan(serviceName, spanName, Utils.toStringMap(tags), ReferenceType.valueOf(reference), extractedSpanContextMap);
    if (spanId != null) {
        context.setReturnValues(Utils.createSpanStruct(context, spanId, serviceName, spanName));
    } else {
        context.setReturnValues(Utils.createSpanStruct(context, null, null, null));
        err.println("ballerina: Can not use tracing API when tracing is disabled");
    }
}
Also used : PrintStream(java.io.PrintStream) BStruct(org.ballerinalang.model.values.BStruct) SpanContext(io.opentracing.SpanContext) BMap(org.ballerinalang.model.values.BMap)

Example 15 with SpanContext

use of io.opentracing.SpanContext in project ballerina by ballerina-lang.

the class OpenTracerBallerinaWrapper method startSpan.

/**
 * Method to start a span using parent span context.
 *
 * @param serviceName       name of the service the span should belong to
 * @param spanName          name of the span
 * @param tags              key value paired tags to attach to the span
 * @param referenceType     type of reference to any parent span
 * @param parentSpanContext map of the parent span context
 * @return unique id of the created span
 */
public String startSpan(String serviceName, String spanName, Map<String, String> tags, ReferenceType referenceType, Map<String, SpanContext> parentSpanContext) {
    if (enabled) {
        Map<String, Span> spanMap = new HashMap<>();
        Map<String, SpanContext> spanContextMap = new HashMap<>();
        Map<String, Tracer> tracers = tracerStore.getTracers(serviceName);
        tracers.forEach((tracerName, tracer) -> {
            Tracer.SpanBuilder spanBuilder = tracer.buildSpan(spanName);
            for (Map.Entry<String, String> tag : tags.entrySet()) {
                spanBuilder = spanBuilder.withTag(tag.getKey(), tag.getValue());
            }
            if (parentSpanContext != null && !parentSpanContext.isEmpty()) {
                spanBuilder = setParent(referenceType, parentSpanContext, spanBuilder, tracerName);
            }
            Span span = spanBuilder.start();
            spanMap.put(tracerName, span);
            spanContextMap.put(tracerName, span.context());
        });
        String spanId = UUID.randomUUID().toString();
        spanStore.addSpan(spanId, spanMap);
        spanStore.addSpanContext(spanId, spanContextMap);
        return spanId;
    } else {
        return null;
    }
}
Also used : SpanContext(io.opentracing.SpanContext) HashMap(java.util.HashMap) Tracer(io.opentracing.Tracer) Span(io.opentracing.Span) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

SpanContext (io.opentracing.SpanContext)20 ActiveSpan (io.opentracing.ActiveSpan)11 Test (org.junit.Test)7 Tracer (io.opentracing.Tracer)6 IOException (java.io.IOException)6 NoopActiveSpan (io.opentracing.NoopActiveSpanSource.NoopActiveSpan)5 ThreadLocalActiveSpan (io.opentracing.util.ThreadLocalActiveSpan)5 ProcessingException (javax.ws.rs.ProcessingException)5 BatfishException (org.batfish.common.BatfishException)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 Map (java.util.Map)3 Client (javax.ws.rs.client.Client)3 WebTarget (javax.ws.rs.client.WebTarget)3 Response (javax.ws.rs.core.Response)3 Task (org.batfish.common.Task)3 JSONArray (org.codehaus.jettison.json.JSONArray)3 JSONException (org.codehaus.jettison.json.JSONException)3 Span (io.opentracing.Span)2 TextMapExtractAdapter (io.opentracing.propagation.TextMapExtractAdapter)2 GlobalTracer (io.opentracing.util.GlobalTracer)2