OpencensusTracerFactory implements ApiTracerFactory { @Override public ApiTracer newTracer(ApiTracer parent, SpanName spanName, OperationType operationType) { Span parentSpan = internalTracer.getCurrentSpan(); if (parent instanceof OpencensusTracer) { parentSpan = ((OpencensusTracer) parent).getSpan(); } Span span = internalTracer .spanBuilderWithExplicitParent(spanName.toString(), parentSpan) .setRecordEvents(true) .startSpan(); span.putAttributes(spanAttributes); return new OpencensusTracer(internalTracer, span, operationType); } OpencensusTracerFactory();  OpencensusTracerFactory(Map<String, String> spanAttributes); @InternalApi("Visible for testing")  OpencensusTracerFactory(Tracer internalTracer, @Nonnull Map<String, String> spanAttributes); @Override ApiTracer newTracer(ApiTracer parent, SpanName spanName, OperationType operationType); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void testSpanNamePassthrough() { OpencensusTracerFactory factory = new OpencensusTracerFactory(internalTracer, ImmutableMap.<String, String>of()); factory.newTracer( NoopApiTracer.getInstance(), SpanName.of("FakeClient", "FakeMethod"), OperationType.Unary); verify(internalTracer) .spanBuilderWithExplicitParent(eq("FakeClient.FakeMethod"), nullable(Span.class)); }
@Test public void testImplicitParentSpan() { OpencensusTracerFactory factory = new OpencensusTracerFactory(internalTracer, defaultSpanAttributes); Span parentSpan = mock(Span.class); Context origContext = ContextUtils.withValue(Context.current(), parentSpan).attach(); try { factory.newTracer( NoopApiTracer.getInstance(), SpanName.of("FakeClient", "FakeMethod"), OperationType.Unary); } finally { Context.current().detach(origContext); } verify(internalTracer).spanBuilderWithExplicitParent(anyString(), same(parentSpan)); }
@Test public void testExplicitParent() { OpencensusTracerFactory factory = new OpencensusTracerFactory(internalTracer, defaultSpanAttributes); Span parentSpan = mock(Span.class); OpencensusTracer parentTracer = new OpencensusTracer(internalTracer, parentSpan, OperationType.Unary); factory.newTracer(parentTracer, SpanName.of("FakeClient", "FakeMethod"), OperationType.Unary); verify(internalTracer).spanBuilderWithExplicitParent(anyString(), same(parentSpan)); }
@Test public void testExplicitParentOverridesImplicit() { OpencensusTracerFactory factory = new OpencensusTracerFactory(internalTracer, defaultSpanAttributes); Span parentSpan = mock(Span.class); OpencensusTracer parentTracer = new OpencensusTracer(internalTracer, parentSpan, OperationType.Unary); Context origContext = ContextUtils.withValue(Context.current(), parentSpan).attach(); try { factory.newTracer(parentTracer, SpanName.of("FakeClient", "FakeMethod"), OperationType.Unary); } finally { Context.current().detach(origContext); } verify(internalTracer).spanBuilderWithExplicitParent(anyString(), same(parentSpan)); }
@Test public void testSpanAttributes() { OpencensusTracerFactory factory = new OpencensusTracerFactory(internalTracer, ImmutableMap.of("gax.version", "1.2.3")); factory.newTracer( NoopApiTracer.getInstance(), SpanName.of("FakeClient", "FakeMethod"), OperationType.Unary); verify(span, times(1)) .putAttributes( ImmutableMap.of("gax.version", AttributeValue.stringAttributeValue("1.2.3"))); }