public void LogUsingFields() { var spanMock = Defaults.GetOpenTelemetrySpanMock(); var shim = new SpanShim(spanMock); Assert.Throws <ArgumentNullException>(() => shim.Log((IEnumerable <KeyValuePair <string, object> >)null)); shim.Log(new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>("foo", "bar"), }); // "event" is a special event name shim.Log(new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>("event", "foo"), }); var first = spanMock.Events.FirstOrDefault(); var last = spanMock.Events.LastOrDefault(); Assert.Equal(2, spanMock.Events.Count); Assert.Equal(SpanShim.DefaultEventName, first.Name); Assert.True(first.Attributes.Any()); Assert.Equal("foo", last.Name); Assert.False(last.Attributes.Any()); }
public void LogUsingFieldsWithExplicitTimestamp() { // TODO Explicit timestamps are unsupported in OpenTelemetry? var spanMock = Defaults.GetOpenTelemetrySpanMock(); var shim = new SpanShim(spanMock); Assert.Throws <ArgumentNullException>(() => shim.Log((IEnumerable <KeyValuePair <string, object> >)null)); shim.Log(DateTimeOffset.UtcNow, new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>("foo", "bar"), }); // "event" is a special event name shim.Log(DateTimeOffset.UtcNow, new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>("event", "foo"), }); Assert.Equal(2, spanMock.Events.Count); var first = spanMock.Events.First(); var last = spanMock.Events.Last(); Assert.Equal(SpanShim.DefaultEventName, first.Name); Assert.True(first.Attributes.Any()); Assert.Equal("foo", last.Name); Assert.False(last.Attributes.Any()); }
private static Mock <Trace.ISpanBuilder> GetDefaultSpanBuilderMock(SpanMock spanMock = null) { var mock = new Mock <Trace.ISpanBuilder>(); spanMock = spanMock ?? Defaults.GetOpenTelemetrySpanMock(); mock.Setup(x => x.StartSpan()).Returns(spanMock); return(mock); }
private static Mock <Tracer> GetDefaultTracerMock(SpanMock spanMock = null) { var mock = new Mock <Tracer>(); spanMock = spanMock ?? Defaults.GetOpenTelemetrySpanMock(); mock.Setup(x => x.StartRootSpan(It.IsAny <string>(), It.IsAny <SpanKind>(), It.IsAny <SpanCreationOptions>())).Returns(spanMock); mock.Setup(x => x.StartSpan(It.IsAny <string>(), It.IsAny <TelemetrySpan>(), It.IsAny <SpanKind>(), It.IsAny <SpanCreationOptions>())).Returns(spanMock); mock.Setup(x => x.StartSpan(It.IsAny <string>(), It.IsAny <SpanContext>(), It.IsAny <SpanKind>(), It.IsAny <SpanCreationOptions>())).Returns(spanMock); mock.Setup(x => x.StartSpanFromActivity(It.IsAny <string>(), It.IsAny <Activity>(), It.IsAny <SpanKind>(), It.IsAny <IEnumerable <Link> >())).Returns(spanMock); return(mock); }
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()); }
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 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(1, (long)spanMock.Attributes.First().Value); }
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); }
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 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()); }
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_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_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); }
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); }
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 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); }
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); }