Search in sources :

Example 16 with SpanContext

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

the class OpenTracerManager method startSpan.

@Override
public Map<String, Object> startSpan(long invocationId, String spanName, Map<String, ?> spanContextMap, Map<String, String> tags, String serviceName) {
    Map<String, Object> spanMap = new HashMap<>();
    Map<String, Tracer> tracers = tracerStore.getTracers(serviceName);
    for (Map.Entry spanContextEntry : spanContextMap.entrySet()) {
        Tracer tracer = tracers.get(spanContextEntry.getKey().toString());
        Tracer.SpanBuilder spanBuilder = tracer.buildSpan(spanName);
        for (Map.Entry<String, String> tag : tags.entrySet()) {
            spanBuilder = spanBuilder.withTag(tag.getKey(), tag.getValue());
        }
        if (spanContextEntry.getValue() != null) {
            spanBuilder = spanBuilder.asChildOf((SpanContext) spanContextEntry.getValue());
        }
        Span span = spanBuilder.start();
        spanMap.put(spanContextEntry.getKey().toString(), span);
    }
    return spanMap;
}
Also used : SpanContext(io.opentracing.SpanContext) HashMap(java.util.HashMap) Tracer(io.opentracing.Tracer) Map(java.util.Map) HashMap(java.util.HashMap) Span(io.opentracing.Span)

Example 17 with SpanContext

use of io.opentracing.SpanContext in project batfish by batfish.

the class Driver method runBatfishThroughService.

public static List<String> runBatfishThroughService(final String taskId, String[] args) {
    final Settings settings;
    try {
        settings = new Settings(_mainSettings);
        settings.setRunMode(RunMode.WORKER);
        settings.parseCommandLine(args);
        // assign taskId for status updates, termination requests
        settings.setTaskId(taskId);
    } catch (Exception e) {
        return Arrays.asList("failure", "Initialization failed: " + ExceptionUtils.getStackTrace(e));
    }
    try {
        Batfish.initTestrigSettings(settings);
    } catch (Exception e) {
        return Arrays.asList("failure", "Failed while applying auto basedir. (All arguments are supplied?): " + e.getMessage());
    }
    if (settings.canExecute()) {
        if (claimIdle()) {
            try {
                final BatfishLogger jobLogger = new BatfishLogger(settings.getLogLevel(), settings.getTimestamp(), settings.getLogFile(), settings.getLogTee(), false);
                settings.setLogger(jobLogger);
                final Task task = new Task(args);
                logTask(taskId, task);
                @Nullable SpanContext runTaskSpanContext = GlobalTracer.get().activeSpan() == null ? null : GlobalTracer.get().activeSpan().context();
                // run batfish on a new thread and set idle to true when done
                Thread thread = new Thread() {

                    @Override
                    public void run() {
                        try (ActiveSpan runBatfishSpan = GlobalTracer.get().buildSpan("Initialize Batfish in a new thread").addReference(References.FOLLOWS_FROM, runTaskSpanContext).startActive()) {
                            // avoid unused warning
                            assert runBatfishSpan != null;
                            task.setStatus(TaskStatus.InProgress);
                            String errMsg = runBatfish(settings);
                            if (errMsg == null) {
                                task.setStatus(TaskStatus.TerminatedNormally);
                            } else {
                                task.setStatus(TaskStatus.TerminatedAbnormally);
                                task.setErrMessage(errMsg);
                            }
                            task.setTerminated(new Date());
                            jobLogger.close();
                            makeIdle();
                        }
                    }
                };
                thread.start();
                return Arrays.asList(BfConsts.SVC_SUCCESS_KEY, "running now");
            } catch (Exception e) {
                _mainLogger.error("Exception while running task: " + e.getMessage());
                makeIdle();
                return Arrays.asList(BfConsts.SVC_FAILURE_KEY, e.getMessage());
            }
        } else {
            return Arrays.asList(BfConsts.SVC_FAILURE_KEY, "Not idle");
        }
    } else {
        return Arrays.asList(BfConsts.SVC_FAILURE_KEY, "Non-executable command");
    }
}
Also used : Task(org.batfish.common.Task) SpanContext(io.opentracing.SpanContext) BatfishLogger(org.batfish.common.BatfishLogger) ActiveSpan(io.opentracing.ActiveSpan) EnvironmentSettings(org.batfish.config.Settings.EnvironmentSettings) Settings(org.batfish.config.Settings) TestrigSettings(org.batfish.config.Settings.TestrigSettings) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ProcessingException(javax.ws.rs.ProcessingException) QuestionException(org.batfish.common.QuestionException) BatfishException(org.batfish.common.BatfishException) IOException(java.io.IOException) CleanBatfishException(org.batfish.common.CleanBatfishException) Nullable(javax.annotation.Nullable) Date(java.util.Date)

Example 18 with SpanContext

use of io.opentracing.SpanContext in project batfish by batfish.

the class WorkItemTest method testInjectExtract.

@Test
public void testInjectExtract() {
    MockContext sourceContext;
    try (ActiveSpan activeSpan = _mockTracer.buildSpan("test span").startActive()) {
        SpanContext sourceContextTmp = activeSpan.context();
        assertThat(sourceContextTmp, instanceOf(MockContext.class));
        sourceContext = (MockContext) sourceContextTmp;
        _workItem.setSourceSpan(activeSpan, _mockTracer);
    }
    SpanContext extractedContextTmp = _workItem.getSourceSpan(_mockTracer);
    assertThat(extractedContextTmp, notNullValue());
    assertThat(extractedContextTmp, instanceOf(MockContext.class));
    MockContext extractedContext = (MockContext) extractedContextTmp;
    // test that injected and extracted spans have same span context data
    assertThat(extractedContext.traceId(), equalTo(sourceContext.traceId()));
    assertThat(extractedContext.spanId(), equalTo(sourceContext.spanId()));
}
Also used : MockContext(io.opentracing.mock.MockSpan.MockContext) SpanContext(io.opentracing.SpanContext) ThreadLocalActiveSpan(io.opentracing.util.ThreadLocalActiveSpan) ActiveSpan(io.opentracing.ActiveSpan) NoopActiveSpan(io.opentracing.NoopActiveSpanSource.NoopActiveSpan) Test(org.junit.Test)

Example 19 with SpanContext

use of io.opentracing.SpanContext in project batfish by batfish.

the class WorkItemTest method testExtractOnly.

@Test
public void testExtractOnly() {
    SpanContext sourceSpanContext = _workItem.getSourceSpan(_mockTracer);
    assertThat(sourceSpanContext, nullValue());
    try (ActiveSpan childSpan = _mockTracer.buildSpan("test dangling child").addReference(References.FOLLOWS_FROM, sourceSpanContext).startActive()) {
        assertThat(childSpan, notNullValue());
        assertThat(childSpan, instanceOf(ThreadLocalActiveSpan.class));
    }
}
Also used : SpanContext(io.opentracing.SpanContext) ThreadLocalActiveSpan(io.opentracing.util.ThreadLocalActiveSpan) ActiveSpan(io.opentracing.ActiveSpan) NoopActiveSpan(io.opentracing.NoopActiveSpanSource.NoopActiveSpan) ThreadLocalActiveSpan(io.opentracing.util.ThreadLocalActiveSpan) Test(org.junit.Test)

Example 20 with SpanContext

use of io.opentracing.SpanContext in project motan by weibocom.

the class OpenTracingFilter method extractTraceInfo.

protected Span extractTraceInfo(Request request, Tracer tracer) {
    String operationName = buildOperationName(request);
    SpanBuilder span = tracer.buildSpan(operationName);
    try {
        SpanContext spanContext = tracer.extract(Format.Builtin.TEXT_MAP, new TextMapExtractAdapter(request.getAttachments()));
        if (spanContext != null) {
            span.asChildOf(spanContext);
        }
    } catch (Exception e) {
        span.withTag("Error", "extract from request fail, error msg:" + e.getMessage());
    }
    return span.start();
}
Also used : SpanBuilder(io.opentracing.Tracer.SpanBuilder) TextMapExtractAdapter(io.opentracing.propagation.TextMapExtractAdapter) SpanContext(io.opentracing.SpanContext)

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