Example #1
0
        public void SetTagIntTagValue()
        {
            var spanMock = Defaults.GetOpenTelemetrySpanMock();
            var shim     = new SpanShim(spanMock);

            Assert.Throws <ArgumentNullException>(() => shim.SetTag((IntTag)null, 1));

            shim.SetTag(new IntTag("foo"), 1);

            Assert.Single(spanMock.Attributes);
            Assert.Equal("foo", spanMock.Attributes.First().Key);
            Assert.Equal(1L, spanMock.Attributes.First().Value);
        }
        public void LogWithExplicitTimestamp()
        {
            var spanMock = Defaults.GetOpenTelemetrySpanMock();
            var shim     = new SpanShim(spanMock);

            shim.Log(DateTimeOffset.UtcNow, "foo");

            Assert.Single(spanMock.Events);
            var first = spanMock.Events.First();

            Assert.Equal("foo", first.Name);
            Assert.False(first.Attributes.Any());
        }
Example #3
0
        public void Log()
        {
            var spanMock = Defaults.GetOpenTelemetrySpanMock();
            var shim     = new SpanShim(spanMock);

            shim.Log("foo");

            Assert.Single(spanMock.Events);
            var first = spanMock.Events.First();

            Assert.Equal("foo", first.Name);
            Assert.False(first.Attributes.Any());
        }
        public void WithTag_KeyIsNullStringValue()
        {
            var spanMock = Defaults.GetOpenTelemetrySpanMock();
            var shim     = new SpanBuilderShim(GetDefaultTracerMock(spanMock).Object, "foo");

            shim.WithTag((string)null, "unused");

            // build
            shim.Start();

            // Null key was ignored
            Assert.Empty(spanMock.Attributes);
        }
        public void Start()
        {
            var spanMock   = Defaults.GetOpenTelemetrySpanMock();
            var tracerMock = GetDefaultTracerMock(spanMock);
            var shim       = new SpanBuilderShim(tracerMock.Object, "foo");

            // build
            var span = shim.Start() as SpanShim;

            // Just check the return value is a SpanShim and that the underlying OpenTelemetry Span.
            // There is nothing left to verify because the rest of the tests were already calling .Start() prior to verification.
            Assert.NotNull(span);
            Assert.Equal(spanMock, span.Span);
        }
Example #6
0
        public void Extract_UnknownFormatIgnored()
        {
            var tracerMock = new Mock <Trace.Tracer>();
            var shim       = new TracerShim(tracerMock.Object, new TraceContextFormat(), new BinaryFormat());

            var spanContextShim = new SpanContextShim(Defaults.GetOpenTelemetrySpanContext());

            // Only two specific types of ITextMap are supported, and neither is a Mock.
            var mockCarrier = new Mock <ITextMap>();
            var context     = shim.Extract(new Mock <IFormat <ITextMap> >().Object, mockCarrier.Object);

            // Verify that the carrier mock was never called.
            mockCarrier.Verify(x => x.GetEnumerator(), Times.Never);
        }
        public void Inject_UnknownFormatIgnored()
        {
            var shim = TracerShim.Create(tracer);

            var spanContextShim = new SpanContextShim(Defaults.GetOpenTelemetrySpanContext());

            // Only two specific types of ITextMap are supported, and neither is a Mock.
            var mockCarrier = new Mock <ITextMap>();

            shim.Inject(spanContextShim, new Mock <IFormat <ITextMap> >().Object, mockCarrier.Object);

            // Verify that the carrier mock was never called.
            mockCarrier.Verify(x => x.Set(It.IsAny <string>(), It.IsAny <string>()), Times.Never);
        }
Example #8
0
        public void Inject_ArgumentValidation()
        {
            var tracerMock = new Mock <Trace.Tracer>();
            var shim       = new TracerShim(tracerMock.Object, new TraceContextFormat(), new BinaryFormat());

            var spanContextShim = new SpanContextShim(Defaults.GetOpenTelemetrySpanContext());
            var mockFormat      = new Mock <IFormat <ITextMap> >();
            var mockCarrier     = new Mock <ITextMap>();

            Assert.Throws <ArgumentNullException>(() => shim.Inject(null, mockFormat.Object, mockCarrier.Object));
            Assert.Throws <ArgumentException>(() => shim.Inject(new Mock <ISpanContext>().Object, mockFormat.Object, mockCarrier.Object));
            Assert.Throws <ArgumentNullException>(() => shim.Inject(spanContextShim, null, mockCarrier.Object));
            Assert.Throws <ArgumentNullException>(() => shim.Inject(spanContextShim, mockFormat.Object, null));
        }
        public void WithTag_ValueIsNullStringValue()
        {
            var spanMock = Defaults.GetOpenTelemetrySpanMock();
            var shim     = new SpanBuilderShim(GetDefaultTracerMock(spanMock).Object, "foo");

            shim.WithTag("foo", null);

            // build
            shim.Start();

            // Null value was turned into string.empty
            Assert.Equal("foo", spanMock.Attributes.First().Key);
            Assert.Equal(string.Empty, spanMock.Attributes.First().Value);
        }
        public void IgnoreActiveSpan()
        {
            var tracerMock = GetDefaultTracerMock();
            var shim       = new SpanBuilderShim(tracerMock.Object, "foo");

            // Add a parent. The shim requires that the ISpan implementation be a SpanShim
            shim.AsChildOf(new SpanShim(Defaults.GetOpenTelemetryMockSpan().Object));

            // Set to Ignore
            shim.IgnoreActiveSpan();

            // build
            shim.Start();

            tracerMock.Verify(o => o.StartRootSpan("foo", 0,
Example #11
0
        public void WithTag_KeyisSpanKindStringValue()
        {
            var spanMock        = Defaults.GetOpenTelemetrySpanMock();
            var spanBuilderMock = GetDefaultSpanBuilderMock(spanMock);
            var shim            = new SpanBuilderShim(GetDefaultTracer(spanBuilderMock), "foo");

            shim.WithTag(global::OpenTracing.Tag.Tags.SpanKind.Key, global::OpenTracing.Tag.Tags.SpanKindClient);

            // build
            shim.Start();

            // Not an attribute
            Assert.Empty(spanMock.Attributes);

            spanBuilderMock.Verify(o => o.SetSpanKind(Trace.SpanKind.Client), Times.Once);
        }
        public void WithTag_KeyIsSpanKindStringValue()
        {
            var spanMock   = Defaults.GetOpenTelemetrySpanMock();
            var tracerMock = GetDefaultTracerMock(spanMock);
            var shim       = new SpanBuilderShim(tracerMock.Object, "foo");

            shim.WithTag(global::OpenTracing.Tag.Tags.SpanKind.Key, global::OpenTracing.Tag.Tags.SpanKindClient);

            // build
            shim.Start();

            // Not an attribute
            Assert.Empty(spanMock.Attributes);

            tracerMock.Verify(o => o.StartSpan("foo", It.IsAny <TelemetrySpan>(), SpanKind.Client, It.Is <SpanCreationOptions>(so => so.StartTimestamp == default && !so.Links.Any())), Times.Once);
        }
        public void WithTag_KeyIsErrorBoolValue()
        {
            var spanMock = Defaults.GetOpenTelemetrySpanMock();
            var shim     = new SpanBuilderShim(GetDefaultTracerMock(spanMock).Object, "foo");

            shim.WithTag(global::OpenTracing.Tag.Tags.Error.Key, true);

            // build
            shim.Start();

            // Not an attribute
            Assert.Empty(spanMock.Attributes);

            // Span status should be set
            Assert.Equal(Status.Unknown, spanMock.GetStatus());
        }
Example #14
0
        public void AsChildOf_WithSpan()
        {
            var tracerMock = GetDefaultTracerMock();
            var shim       = new SpanBuilderShim(tracerMock.Object, "foo");

            // Add a parent.
            var span = new SpanShim(Defaults.GetOpenTelemetryMockSpan().Object);

            shim.AsChildOf(span);

            // build
            shim.Start();

            tracerMock.Verify(o => o.StartSpan("foo", span.Span, 0,
                                               It.Is <SpanCreationOptions>(so => so.StartTimestamp == default && !so.Links.Any())), Times.Once);
        }
Example #15
0
        public void AsChildOf_WithSpan()
        {
            var spanBuilderMock = GetDefaultSpanBuilderMock();
            var shim            = new SpanBuilderShim(GetDefaultTracer(spanBuilderMock), "foo");

            // Add a parent.
            var span = new SpanShim(Defaults.GetOpenTelemetryMockSpan().Object);

            shim.AsChildOf(span);

            // build
            shim.Start();

            spanBuilderMock.Verify(o => o.SetParent(span.Span), Times.Once);
            spanBuilderMock.Verify(o => o.SetParent(It.IsAny <Trace.SpanContext>()), Times.Never);
        }
Example #16
0
        public void WithTag_KeyisErrorStringValue()
        {
            var spanMock        = Defaults.GetOpenTelemetrySpanMock();
            var spanBuilderMock = GetDefaultSpanBuilderMock(spanMock);
            var shim            = new SpanBuilderShim(GetDefaultTracer(spanBuilderMock), "foo");

            shim.WithTag(global::OpenTracing.Tag.Tags.Error.Key, "true");

            // build
            shim.Start();

            // Not an attribute
            Assert.Empty(spanMock.Attributes);

            // Span status should be set
            Assert.Equal(Trace.Status.Unknown, spanMock.Status);
        }
Example #17
0
        public void IgnoreActiveSpan()
        {
            var tracerMock = GetDefaultTracerMock();
            var shim       = new SpanBuilderShim(tracerMock.Object, "foo");

            // Add a parent. The shim requires that the ISpan implementation be a SpanShim
            shim.AsChildOf(new SpanShim(Defaults.GetOpenTelemetryMockSpan().Object));

            // Set to Ignore
            shim.IgnoreActiveSpan();

            // build
            shim.Start();

            tracerMock.Verify(o => o.StartRootSpan("foo", 0,
                                                   It.Is <SpanCreationOptions>(so => so.StartTimestamp == default && !so.Links.Any())), Times.Once);
        }
Example #18
0
        public void Active_IsNotNull()
        {
            var tracerMock      = new Mock <ITracer>();
            var shim            = new ScopeManagerShim(tracerMock.Object);
            var openTracingSpan = new SpanShim(Defaults.GetOpenTelemetrySpanMock());
            var scopeMock       = new Mock <IDisposable>();

            tracerMock.Setup(x => x.WithSpan(openTracingSpan.Span)).Returns(scopeMock.Object);
            tracerMock.Setup(x => x.CurrentSpan).Returns(openTracingSpan.Span);

            var scope = shim.Activate(openTracingSpan, true);

            Assert.NotNull(scope);

            var activeScope = shim.Active;

            Assert.Equal(scope, activeScope);
        }
Example #19
0
        public void AsChildOf_MultipleCallsWithSpan()
        {
            var tracerMock = GetDefaultTracerMock();
            var shim       = new SpanBuilderShim(tracerMock.Object, "foo");

            // Multiple calls
            var span1 = new SpanShim(Defaults.GetOpenTelemetryMockSpan().Object);
            var span2 = new SpanShim(Defaults.GetOpenTelemetryMockSpan().Object);

            shim.AsChildOf(span1);
            shim.AsChildOf(span2);

            // build
            shim.Start();

            tracerMock.Verify(o => o.StartSpan("foo", span1.Span, 0,
                                               It.Is <SpanCreationOptions>(so => so.StartTimestamp == default && so.Links.Single().Context.Equals(span2.Span.Context))), Times.Once);
        }
        public void SetTagIntOrStringTagValue()
        {
            var spanMock = Defaults.GetOpenTelemetrySpanMock();
            var shim     = new SpanShim(spanMock);

            Assert.Throws <ArgumentNullException>(() => shim.SetTag((IntOrStringTag)null, "foo"));

            shim.SetTag(new IntOrStringTag("foo"), 1);
            shim.SetTag(new IntOrStringTag("bar"), "baz");

            Assert.Equal(2, spanMock.Attributes.Count);

            Assert.Equal("foo", spanMock.Attributes.First().Key);
            Assert.Equal(1, (long)spanMock.Attributes.First().Value);

            Assert.Equal("bar", spanMock.Attributes.Last().Key);
            Assert.Equal("baz", (string)spanMock.Attributes.Last().Value);
        }
        public void SetTagBooleanTagValue()
        {
            var spanMock = Defaults.GetOpenTelemetrySpanMock();
            var shim     = new SpanShim(spanMock);

            Assert.Throws <ArgumentNullException>(() => shim.SetTag((BooleanTag)null, true));

            shim.SetTag(new BooleanTag("foo"), true);
            shim.SetTag(new BooleanTag(global::OpenTracing.Tag.Tags.Error.Key), true);

            Assert.Single(spanMock.Attributes);
            Assert.Equal("foo", spanMock.Attributes.First().Key);
            Assert.True((bool)spanMock.Attributes.First().Value);

            // A boolean tag named "error" is a special case that must be checked
            Assert.Equal(Status.Unknown, spanMock.Status);
            shim.SetTag(global::OpenTracing.Tag.Tags.Error.Key, false);
            Assert.Equal(Status.Ok, spanMock.Status);
        }
        public void WithTag_VariousValueTypes()
        {
            var spanMock = Defaults.GetOpenTelemetrySpanMock();
            var shim     = new SpanBuilderShim(GetDefaultTracerMock(spanMock).Object, "foo");

            shim.WithTag("foo", "unused");
            shim.WithTag("bar", false);
            shim.WithTag("baz", 1);
            shim.WithTag("bizzle", 1D);
            shim.WithTag(new global::OpenTracing.Tag.BooleanTag("shnizzle"), true);
            shim.WithTag(new global::OpenTracing.Tag.IntOrStringTag("febrizzle"), "unused");
            shim.WithTag(new global::OpenTracing.Tag.StringTag("mobizzle"), "unused");

            // build
            shim.Start();

            // Just verify the count
            Assert.Equal(7, spanMock.Attributes.Count);
        }
Example #23
0
        public void InjectExtract_Binary_Ok()
        {
            var tracerMock = new Mock <Trace.Tracer>();

            var carrier = new BinaryCarrier();

            var spanContextShim = new SpanContextShim(Defaults.GetOpenTelemetrySpanContext());

            var format = new BinaryFormat();

            var shim = new TracerShim(tracerMock.Object, new TraceContextFormat(), format);

            // first inject
            shim.Inject(spanContextShim, BuiltinFormats.Binary, carrier);

            // then extract
            var extractedSpanContext = shim.Extract(BuiltinFormats.Binary, carrier);

            AssertOpenTracerSpanContextEqual(spanContextShim, extractedSpanContext);
        }
Example #24
0
        public void AsChildOf_MultipleCallsWithSpan()
        {
            var spanBuilderMock = GetDefaultSpanBuilderMock();
            var shim            = new SpanBuilderShim(GetDefaultTracer(spanBuilderMock), "foo");

            // Multiple calls
            var span1 = new SpanShim(Defaults.GetOpenTelemetryMockSpan().Object);
            var span2 = new SpanShim(Defaults.GetOpenTelemetryMockSpan().Object);

            shim.AsChildOf(span1);
            shim.AsChildOf(span2);

            // build
            shim.Start();

            spanBuilderMock.Verify(o => o.SetParent(span1.Span), Times.Once);
            spanBuilderMock.Verify(o => o.SetParent(It.IsAny <Trace.SpanContext>()), Times.Never);

            // The rest become links
            spanBuilderMock.Verify(o => o.AddLink(It.Is <Trace.ILink>(link => link.Context == span2.Span.Context)), Times.Once);
        }
Example #25
0
        public void IgnoreActiveSpan()
        {
            var spanBuilderMock = GetDefaultSpanBuilderMock();
            var shim            = new SpanBuilderShim(GetDefaultTracer(spanBuilderMock), "foo");

            // Add a parent. The shim requires that the ISpan implementation be a SpanShim
            shim.AsChildOf(new SpanShim(Defaults.GetOpenTelemetryMockSpan().Object));

            // Set to Ignore
            shim.IgnoreActiveSpan();

            spanBuilderMock.Verify(o => o.SetParent(It.IsAny <Trace.SpanContext>()), Times.Never);

            // There should be two methods calls to the underlying the builder. SetNoParent is last.
            int callOrder = 0;

            spanBuilderMock.Setup(x => x.SetParent(It.IsAny <Trace.ISpan>())).Callback(() => Assert.Equal(0, callOrder++));
            spanBuilderMock.Setup(x => x.SetNoParent()).Callback(() => Assert.Equal(1, callOrder++));

            // build
            shim.Start();
        }
        public void InjectExtract_TextMap_Ok()
        {
            var tracerMock = new Mock <Trace.ITracer>();

            var carrier = new TextMapCarrier();

            var spanContextShim = new SpanContextShim(Defaults.GetOpenTelemetrySpanContext());

            var format = new TraceContextFormat();

            tracerMock.Setup(x => x.TextFormat).Returns(format);

            var shim = TracerShim.Create(tracerMock.Object);

            // first inject
            shim.Inject(spanContextShim, BuiltinFormats.TextMap, carrier);

            // then extract
            var extractedSpanContext = shim.Extract(BuiltinFormats.TextMap, carrier);

            AssertOpenTracerSpanContextEqual(spanContextShim, extractedSpanContext);
        }
Example #27
0
        public void Activate()
        {
            var tracerMock = new Mock <ITracer>();
            var shim       = new ScopeManagerShim(tracerMock.Object);
            var scopeMock  = new Mock <IDisposable>();
            var spanShim   = new SpanShim(Defaults.GetOpenTelemetryMockSpan().Object);

            tracerMock.Setup(x => x.WithSpan(spanShim.Span)).Returns(scopeMock.Object);

            using (shim.Activate(spanShim, true))
            {
#if DEBUG
                Assert.Equal(1, shim.SpanScopeTableCount);
#endif
            }

#if DEBUG
            Assert.Equal(0, shim.SpanScopeTableCount);
#endif
            tracerMock.Verify(x => x.WithSpan(spanShim.Span), Times.Once);
            scopeMock.Verify(x => x.Dispose(), Times.Once);
        }
        public void Extract_Ok()
        {
            var shim = TracerShim.Create(tracer);

            var mockCarrier = new Mock <ITextMap>();

            // The ProxyTracer uses OpenTelemetry.Context.Propagation.TraceContextFormat, so we need to satisfy that.
            var traceContextFormat = new TraceContextFormat();
            var spanContext        = Defaults.GetOpenTelemetrySpanContext();

            var carrierMap = new Dictionary <string, string>();

            // inject the SpanContext into the carrier map
            traceContextFormat.Inject <Dictionary <string, string> >(spanContext, carrierMap, (map, key, value) => map.Add(key, value));

            // send the populated carrier map to the extract method.
            mockCarrier.Setup(x => x.GetEnumerator()).Returns(carrierMap.GetEnumerator());
            var spanContextShim = shim.Extract(BuiltinFormats.TextMap, mockCarrier.Object) as SpanContextShim;

            // Verify that the carrier was called
            mockCarrier.Verify(x => x.GetEnumerator(), Times.Once);

            Assert.Equal(spanContext, spanContextShim.SpanContext);
        }
        public void FinishSpanUsingSpecificTimestamp()
        {
            var shim = new SpanShim(Defaults.GetOpenTelemetryMockSpan().Object);

            Assert.Throws <NotImplementedException>(() => shim.Finish(DateTimeOffset.UtcNow));
        }
 internal static SpanContextShim GetSpanContextShim()
 {
     return(new SpanContextShim(Defaults.GetOpenTelemetrySpanContext()));
 }