SpanContextHandlerTracer implements Tracer { @Override public TraceContext startSpan(String name) { StartSpanOptions defaultOptions = new StartSpanOptions(); return startSpan(name, defaultOptions); } SpanContextHandlerTracer(Set<TraceSink> sinks, SpanContextHandler contextHandler, SpanContextFactory spanContextFactory, TimestampFactory timestampFactory);  SpanContextHandlerTracer(TraceSink sink, SpanContextHandler contextHandler, SpanContextFactory spanContextFactory, TimestampFactory timestampFactory); @Override TraceContext startSpan(String name); @Override TraceContext startSpan(String name, StartSpanOptions options); @Override void endSpan(TraceContext traceContext); @Override void endSpan(TraceContext traceContext, EndSpanOptions options); @Override void annotateSpan(TraceContext traceContext, Labels labels); @Override void setStackTrace(TraceContext traceContext, StackTrace stackTrace);  }
@Test public void testStartSpan() throws Exception { TraceContext traceContext = tracer.startSpan("foo"); assertThat(sink.startSpanEvents).hasSize(1); TestTraceSink.StartSpanEvent startEvent1 = sink.startSpanEvents.get(0); assertThat(traceContext.getHandle().getCurrentSpanContext()).isEqualTo(contextHandler.current()); assertThat(traceContext.getHandle().getCurrentSpanContext()).isEqualTo(startEvent1.context); assertThat(startEvent1.name).isEqualTo("foo"); }
@Test public void testStartSpanWithOptions() { StartSpanOptions options = new StartSpanOptions(); options.setSpanKind(SpanKind.RPC_CLIENT); Timestamp ts = new Timestamp() { @Override public long getSeconds() { return 1; } @Override public int getNanos() { return 2; } }; options.setTimestamp(ts); TraceContext traceContext = tracer.startSpan("foo", options); assertThat(sink.startSpanEvents).hasSize(1); TestTraceSink.StartSpanEvent startEvent1 = sink.startSpanEvents.get(0); assertThat(traceContext.getHandle().getCurrentSpanContext()).isEqualTo(contextHandler.current()); assertThat(traceContext.getHandle().getCurrentSpanContext()).isEqualTo(startEvent1.context); assertThat(startEvent1.name).isEqualTo("foo"); assertThat(startEvent1.kind).isEqualTo(SpanKind.RPC_CLIENT); assertThat(startEvent1.timestamp).isEqualTo(ts); }
@Test public void testStartSpanWithTraceOptionsTrue() { SpanContext initial = new SpanContext( new TraceId(BigInteger.valueOf(1000)), new SpanId(2000), new TraceOptions()); contextHandler.attach(initial); StartSpanOptions options = new StartSpanOptions() .setEnableTrace(true) .setEnableStackTrace(true); TraceContext traceContext = tracer.startSpan("foo", options); assertThat(sink.startSpanEvents).hasSize(1); TestTraceSink.StartSpanEvent startEvent1 = sink.startSpanEvents.get(0); SpanContext currentContext = traceContext.getHandle().getCurrentSpanContext(); assertThat(currentContext).isEqualTo(contextHandler.current()); assertThat(currentContext).isEqualTo(startEvent1.context); assertThat(currentContext.getTraceOptions().getTraceEnabled()).isEqualTo(true); assertThat(currentContext.getTraceOptions().getStackTraceEnabled()).isEqualTo(true); assertThat(startEvent1.name).isEqualTo("foo"); }
@Test public void testStartSpanWithTraceOptionsFalse() { SpanContext initial = new SpanContext( new TraceId(BigInteger.valueOf(1000)), new SpanId(2000), new TraceOptions(3)); contextHandler.attach(initial); StartSpanOptions options = new StartSpanOptions() .setEnableTrace(false) .setEnableStackTrace(false); TraceContext traceContext = tracer.startSpan("foo", options); assertThat(sink.startSpanEvents).hasSize(1); TestTraceSink.StartSpanEvent startEvent1 = sink.startSpanEvents.get(0); SpanContext currentContext = traceContext.getHandle().getCurrentSpanContext(); assertThat(currentContext).isEqualTo(contextHandler.current()); assertThat(currentContext).isEqualTo(startEvent1.context); assertThat(currentContext.getTraceOptions().getTraceEnabled()).isEqualTo(false); assertThat(currentContext.getTraceOptions().getStackTraceEnabled()).isEqualTo(false); assertThat(startEvent1.name).isEqualTo("foo"); }
@Test public void testStartSpanAddsAgentLabel() { TraceContext traceContext = tracer.startSpan("foo"); assertThat(sink.annotateEvents).hasSize(1); TestTraceSink.AnnotateEvent annotateEvent = sink.annotateEvents.get(0); assertThat(annotateEvent.context).isEqualTo(traceContext.getHandle().getCurrentSpanContext()); String expectedAgent = "cloud-trace-java " + SdkVersion.get(); assertThat(annotateEvent.labels.getLabels()).contains(new Label("/agent", expectedAgent)); }