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