use of io.opencensus.trace.SpanContext in project instrumentation-java by census-instrumentation.
the class SpanBuilderImpl method startSpan.
@Override
public Span startSpan() {
if (remoteParentSpanContext != null) {
return startSpanInternal(remoteParentSpanContext, Boolean.TRUE, name, sampler, parentLinks, recordEvents, kind, null);
} else {
// This is not a child of a remote Span. Get the parent SpanContext from the parent Span if
// any.
SpanContext parentContext = null;
Boolean hasRemoteParent = null;
if (parent != null) {
parentContext = parent.getContext();
hasRemoteParent = Boolean.FALSE;
}
return startSpanInternal(parentContext, hasRemoteParent, name, sampler, parentLinks, recordEvents, kind, parent);
}
}
use of io.opencensus.trace.SpanContext in project instrumentation-java by census-instrumentation.
the class B3FormatTest method parseMissingSampledAndMissingFlag.
@Test
public void parseMissingSampledAndMissingFlag() throws SpanContextParseException {
Map<String, String> headersNotSampled = new HashMap<String, String>();
headersNotSampled.put(X_B3_TRACE_ID, TRACE_ID_BASE16);
headersNotSampled.put(X_B3_SPAN_ID, SPAN_ID_BASE16);
SpanContext spanContext = SpanContext.create(TRACE_ID, SPAN_ID, TraceOptions.DEFAULT);
assertThat(b3Format.extract(headersNotSampled, getter)).isEqualTo(spanContext);
}
use of io.opencensus.trace.SpanContext in project grpc-java by grpc.
the class CensusModulesTest method serverBasicTracingNoHeaders.
@Test
public void serverBasicTracingNoHeaders() {
ServerStreamTracer.Factory tracerFactory = censusTracing.getServerTracerFactory();
ServerStreamTracer serverStreamTracer = tracerFactory.newServerStreamTracer(method.getFullMethodName(), new Metadata());
verifyNoInteractions(mockTracingPropagationHandler);
verify(tracer).spanBuilderWithRemoteParent(eq("Recv.package1.service2.method3"), ArgumentMatchers.<SpanContext>isNull());
verify(spyServerSpanBuilder).setRecordEvents(eq(true));
Context filteredContext = serverStreamTracer.filterContext(Context.ROOT);
assertSame(spyServerSpan, ContextUtils.getValue(filteredContext));
serverStreamTracer.serverCallStarted(new CallInfo<>(method, Attributes.EMPTY, null));
verify(spyServerSpan, never()).end(any(EndSpanOptions.class));
serverStreamTracer.outboundMessage(0);
serverStreamTracer.outboundMessageSent(0, 882, -1);
serverStreamTracer.inboundMessage(0);
serverStreamTracer.outboundMessage(1);
serverStreamTracer.outboundMessageSent(1, -1, 27);
serverStreamTracer.inboundMessageRead(0, 255, 90);
serverStreamTracer.streamClosed(Status.CANCELLED);
InOrder inOrder = inOrder(spyServerSpan);
inOrder.verify(spyServerSpan, times(3)).addMessageEvent(messageEventCaptor.capture());
List<MessageEvent> events = messageEventCaptor.getAllValues();
assertEquals(MessageEvent.builder(MessageEvent.Type.SENT, 0).setCompressedMessageSize(882).build(), events.get(0));
assertEquals(MessageEvent.builder(MessageEvent.Type.SENT, 1).setUncompressedMessageSize(27).build(), events.get(1));
assertEquals(MessageEvent.builder(MessageEvent.Type.RECEIVED, 0).setCompressedMessageSize(255).setUncompressedMessageSize(90).build(), events.get(2));
inOrder.verify(spyServerSpan).end(EndSpanOptions.builder().setStatus(io.opencensus.trace.Status.CANCELLED).setSampleToLocalSpanStore(false).build());
verifyNoMoreInteractions(spyServerSpan);
}
use of io.opencensus.trace.SpanContext in project grpc-java by grpc.
the class AbstractInteropTest method censusContextsPropagated.
@Test(timeout = 10000)
public void censusContextsPropagated() {
Assume.assumeTrue("Skip the test because server is not in the same process.", server != null);
Assume.assumeTrue(customCensusModulePresent());
Span clientParentSpan = Tracing.getTracer().spanBuilder("Test.interopTest").startSpan();
// A valid ID is guaranteed to be unique, so we can verify it is actually propagated.
assertTrue(clientParentSpan.getContext().getTraceId().isValid());
Context ctx = io.opencensus.tags.unsafe.ContextUtils.withValue(Context.ROOT, tagger.emptyBuilder().putLocal(StatsTestUtils.EXTRA_TAG, TagValue.create("extra value")).build());
ctx = ContextUtils.withValue(ctx, clientParentSpan);
Context origCtx = ctx.attach();
try {
blockingStub.unaryCall(SimpleRequest.getDefaultInstance());
Context serverCtx = contextCapture.get();
assertNotNull(serverCtx);
FakeTagContext statsCtx = (FakeTagContext) io.opencensus.tags.unsafe.ContextUtils.getValue(serverCtx);
assertNotNull(statsCtx);
Map<TagKey, TagValue> tags = statsCtx.getTags();
boolean tagFound = false;
for (Map.Entry<TagKey, TagValue> tag : tags.entrySet()) {
if (tag.getKey().equals(StatsTestUtils.EXTRA_TAG)) {
assertEquals(TagValue.create("extra value"), tag.getValue());
tagFound = true;
}
}
assertTrue("tag not found", tagFound);
Span span = ContextUtils.getValue(serverCtx);
assertNotNull(span);
SpanContext spanContext = span.getContext();
assertEquals(clientParentSpan.getContext().getTraceId(), spanContext.getTraceId());
} finally {
ctx.detach(origCtx);
}
}
use of io.opencensus.trace.SpanContext in project instrumentation-java by census-instrumentation.
the class HttpServerHandler method handleStart.
/**
* Instrument an incoming request before it is handled.
*
* <p>This method will create a span under the deserialized propagated parent context. If the
* parent context is not present, the span will be created under the current context.
*
* <p>The generated span will NOT be set as current context. User can control when to enter the
* scope of this span. Use {@link AbstractHttpHandler#getSpanFromContext} to retrieve the span.
*
* @param carrier the entity that holds the HTTP information.
* @param request the request entity.
* @return the {@link HttpRequestContext} that contains stats and trace data associated with the
* request.
* @since 0.19
*/
public HttpRequestContext handleStart(C carrier, Q request) {
checkNotNull(carrier, "carrier");
checkNotNull(request, "request");
SpanBuilder spanBuilder = null;
String spanName = getSpanName(request, extractor);
// de-serialize the context
SpanContext spanContext = null;
try {
spanContext = textFormat.extract(carrier, getter);
} catch (SpanContextParseException e) {
// TODO: Currently we cannot distinguish between context parse error and missing context.
// Logging would be annoying so we just ignore this error and do not even log a message.
}
if (spanContext == null || publicEndpoint) {
spanBuilder = tracer.spanBuilder(spanName);
} else {
spanBuilder = tracer.spanBuilderWithRemoteParent(spanName, spanContext);
}
Span span = spanBuilder.setSpanKind(Kind.SERVER).startSpan();
if (publicEndpoint && spanContext != null) {
span.addLink(Link.fromSpanContext(spanContext, Type.PARENT_LINKED_SPAN));
}
if (span.getOptions().contains(Options.RECORD_EVENTS)) {
addSpanRequestAttributes(span, request, extractor);
}
return getNewContext(span, tagger.getCurrentTagContext());
}
Aggregations