public void Inject_Ok()
        {
            var shim = TracerShim.Create(tracer);

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

            var mockCarrier = new Mock <ITextMap>();

            shim.Inject(spanContextShim, BuiltinFormats.TextMap, mockCarrier.Object);

            // Verify that the carrier mock was invoked at least one time.
            mockCarrier.Verify(x => x.Set(It.IsAny <string>(), It.IsAny <string>()), Times.AtLeastOnce);
        }
        public void Inject_ArgumentValidation()
        {
            var shim = TracerShim.Create(tracer);

            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 Extract_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>();
            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);
        }
Example #4
0
        public void Inject_UnknownFormatIgnored()
        {
            var shim = TracerShim.Create(NoopTracer.Instance);

            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 #5
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);
        }
        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);
        }
        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);
        }
 internal static SpanContextShim GetSpanContextShim()
 {
     return(new SpanContextShim(Defaults.GetOpenTelemetrySpanContext()));
 }