示例#1
0
        internal static object Run(OpenTracingShimOptions options)
        {
            // Enable OpenTelemetry for the source "MyCompany.MyProduct.MyWebServer"
            // and use Console exporter.
            using var openTelemetry = OpenTelemetrySdk.EnableOpenTelemetry(
                      (builder) => builder.AddActivitySource("MyCompany.MyProduct.MyWebServer")
                      .SetResource(Resources.CreateServiceResource("MyServiceName"))
                      .UseConsoleExporter(opt => opt.DisplayAsJson = options.DisplayAsJson));

            // The above line is required only in applications
            // which decide to use OpenTelemetry.

            // Following shows how to use the OpenTracing shim

            var tracer = new TracerShim(TracerProvider.GetTracer("MyCompany.MyProduct.MyWebServer"), new TraceContextFormat());

            using (IScope parentScope = tracer.BuildSpan("Parent").StartActive(finishSpanOnDispose: true))
            {
                parentScope.Span.SetTag("my", "value");
                parentScope.Span.SetOperationName("parent span new name");

                // The child scope will automatically use parentScope as its parent.
                using (IScope childScope = tracer.BuildSpan("Child").StartActive(finishSpanOnDispose: true))
                {
                    childScope.Span.SetTag("Child Tag", "Child Tag Value").SetTag("ch", "value").SetTag("more", "attributes");
                }
            }

            System.Console.WriteLine("Press Enter key to exit.");

            return(null);
        }
        internal static object Run(OpenTelemetryShimOptions options)
        {
            // Enable OpenTelemetry for the source "MyCompany.MyProduct.MyWebServer"
            // and use a single pipeline with a custom MyProcessor, and Console exporter.
            using var openTelemetry = OpenTelemetrySdk.EnableOpenTelemetry(
                      (builder) => builder.AddActivitySource("MyCompany.MyProduct.MyWebServer")
                      .SetResource(Resources.CreateServiceResource("MyServiceName"))
                      .UseConsoleExporter(opt => opt.DisplayAsJson = options.DisplayAsJson));

            // The above line is required only in applications
            // which decide to use Open Telemetry.

            var tracer = TracerProvider.GetTracer("MyCompany.MyProduct.MyWebServer");
            var span   = tracer.StartSpan("parent span");

            span.SetAttribute("my", "value");
            span.UpdateName("parent span new name");

            var spanChild = tracer.StartSpan("child span");

            spanChild.AddEvent("sample event").SetAttribute("ch", "value").SetAttribute("more", "attributes");
            spanChild.End();

            span.End();

            System.Console.WriteLine("Press Enter key to exit.");

            return(null);
        }
        public void CtorArgumentValidation()
        {
            var tracer = TracerProvider.GetTracer(TracerName);

            Assert.Throws <ArgumentNullException>(() => new SpanBuilderShim(null, "foo"));
            Assert.Throws <ArgumentNullException>(() => new SpanBuilderShim(tracer, null));
        }
示例#4
0
        public CurrentSpanTests()
        {
            Activity.DefaultIdFormat      = ActivityIdFormat.W3C;
            Activity.ForceDefaultIdFormat = true;

            this.tracer = TracerProvider.GetTracer(null);
        }
示例#5
0
        public void Activate_SpanMustBeShim()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new ScopeManagerShim(tracer);

            Assert.Throws <ArgumentException>(() => shim.Activate(new Mock <global::OpenTracing.ISpan>().Object, true));
        }
        public void SpanContextIsNotNull()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new SpanShim(tracer.StartSpan(SpanName));

            // ISpanContext validation handled in a separate test class
            Assert.NotNull(shim.Context);
        }
        public void FinishSpan()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new SpanShim(tracer.StartSpan(SpanName));

            shim.Finish();

            Assert.NotEqual(default, shim.Span.Activity.Duration);
        public void ScopeManager_NotNull()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new TracerShim(tracer, new TraceContextFormat());

            // Internals of the ScopeManagerShim tested elsewhere
            Assert.NotNull(shim.ScopeManager as ScopeManagerShim);
        }
示例#9
0
        public void Active_IsNull()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new ScopeManagerShim(tracer);

            Assert.Null(Activity.Current);
            Assert.Null(shim.Active);
        }
        public void BuildSpan_NotNull()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new TracerShim(tracer, new TraceContextFormat());

            // Internals of the SpanBuilderShim tested elsewhere
            Assert.NotNull(shim.BuildSpan("foo") as SpanBuilderShim);
        }
        public void Extract_ArgumentValidation()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new TracerShim(tracer, new TraceContextFormat());

            Assert.Throws <ArgumentNullException>(() => shim.Extract(null, new Mock <ITextMap>().Object));
            Assert.Throws <ArgumentNullException>(() => shim.Extract(new Mock <IFormat <ITextMap> >().Object, null));
        }
        public void WithTag_KeyIsErrorBoolValue()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new SpanBuilderShim(tracer, "foo");

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

            // build
            var spanShim = (SpanShim)shim.Start();

            // Span status should be set
            Assert.Equal(Status.Unknown, spanShim.Span.Activity.GetStatus());
        }
        public void CtorArgumentValidation()
        {
            // null tracer and text format
            Assert.Throws <ArgumentNullException>(() => new TracerShim(null, null));

            // null tracer
            Assert.Throws <ArgumentNullException>(() => new TracerShim(null, new TraceContextFormat()));

            // null context format
            var tracerMock = new Mock <Trace.Tracer>();

            Assert.Throws <ArgumentNullException>(() => new TracerShim(TracerProvider.GetTracer("test"), null));
        }
        public void Start()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new SpanBuilderShim(tracer, "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("foo", span.Span.Activity.OperationName);
        }
        public void WithTag_KeyIsNullStringValue()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new SpanBuilderShim(tracer, "foo");

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

            // build
            var spanShim = (SpanShim)shim.Start();

            // Null key was ignored
            Assert.Empty(spanShim.Span.Activity.Tags);
        }
        public void AsChildOfWithSpanContext()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new SpanBuilderShim(tracer, "foo");

            // Add a parent
            var spanContext = SpanContextShimTests.GetSpanContextShim();
            var test        = shim.AsChildOf(spanContext);

            // build
            var spanShim = (SpanShim)shim.Start();

            Assert.NotNull(spanShim.Span.Activity.ParentId);
        }
示例#17
0
        public OpenTelemetrySdkBenchmarks()
        {
            using var openTelemetryAlwaysOnSample = OpenTelemetrySdk.EnableOpenTelemetry(
                      (builder) => builder.AddActivitySource("AlwaysOnSample").SetSampler(new AlwaysOnSampler()));

            using var openTelemetryAlwaysOffSample = OpenTelemetrySdk.EnableOpenTelemetry(
                      (builder) => builder.AddActivitySource("AlwaysOffSample").SetSampler(new AlwaysOffSampler()));

            using var openTelemetryNoOp = OpenTelemetrySdk.EnableOpenTelemetry(null);

            this.alwaysSampleTracer = TracerProvider.GetTracer("AlwaysOnSample");
            this.neverSampleTracer  = TracerProvider.GetTracer("AlwaysOffSample");
            this.noopTracer         = TracerProvider.GetTracer("NoOp");
        }
        public void Inject_ArgumentValidation()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new TracerShim(tracer, new TraceContextFormat());

            var spanContextShim = new SpanContextShim(new SpanContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None));
            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 tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new SpanBuilderShim(tracer, "foo");

            shim.WithTag("foo", null);

            // build
            var spanShim = (SpanShim)shim.Start();

            // Null value was turned into string.empty
            Assert.Equal("foo", spanShim.Span.Activity.Tags.First().Key);
            Assert.Equal(string.Empty, spanShim.Span.Activity.Tags.First().Value);
        }
        public void Extract_UnknownFormatIgnored()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new TracerShim(tracer, new TraceContextFormat());

            var spanContextShim = new SpanContextShim(new SpanContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None));

            // 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 AsChildOf_WithNullSpanContext()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new SpanBuilderShim(tracer, "foo");

            // Add a null parent
            shim.AsChildOf((global::OpenTracing.ISpanContext)null);

            // build
            var spanShim = (SpanShim)shim.Start();

            // should be no parent.
            Assert.Null(spanShim.Span.Activity.Parent);
        }
        public void StartWithExplicitTimestamp()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new SpanBuilderShim(tracer, "foo");

            var startTimestamp = DateTimeOffset.Now;

            shim.WithStartTimestamp(startTimestamp);

            // build
            var spanShim = (SpanShim)shim.Start();

            Assert.Equal(startTimestamp, spanShim.Span.Activity.StartTimeUtc);
        }
        public void AsChildOf_WithNullSpan()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new SpanBuilderShim(tracer, "foo");

            // Add a null parent
            shim.AsChildOf((global::OpenTracing.ISpan)null);

            // build
            var spanShim = (SpanShim)shim.Start();

            Assert.Equal("foo", spanShim.Span.Activity.OperationName);
            Assert.Null(spanShim.Span.Activity.Parent);
        }
        public void WithTag_KeyIsSpanKindStringValue()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new SpanBuilderShim(tracer, "foo");

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

            // build
            var spanShim = (SpanShim)shim.Start();

            // Not an attribute
            Assert.Empty(spanShim.Span.Activity.Tags);
            Assert.Equal("foo", spanShim.Span.Activity.OperationName);
            Assert.Equal(ActivityKind.Client, spanShim.Span.Activity.Kind);
        }
        public void Inject_UnknownFormatIgnored()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new TracerShim(tracer, new TraceContextFormat());

            var spanContextShim = new SpanContextShim(new SpanContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.Recorded));

            // 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);
        }
示例#26
0
        public void Active_IsNotNull()
        {
            var tracer          = TracerProvider.GetTracer(TracerName);
            var shim            = new ScopeManagerShim(tracer);
            var openTracingSpan = new SpanShim(tracer.StartSpan(SpanName));

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

            Assert.NotNull(scope);

            var activeScope = shim.Active;

            Assert.Equal(scope.Span.Context.SpanId, activeScope.Span.Context.SpanId);
            openTracingSpan.Finish();
        }
        public void AsChildOf_WithSpan()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new SpanBuilderShim(tracer, "foo");

            // Add a parent.
            var span = new SpanShim(tracer.StartSpan(SpanName1));

            shim.AsChildOf(span);

            // build
            var spanShim = (SpanShim)shim.Start();

            Assert.Equal("foo", spanShim.Span.Activity.OperationName);
            Assert.NotNull(spanShim.Span.Activity.ParentId);
        }
        public void IgnoreActiveSpan()
        {
            var tracer = TracerProvider.GetTracer(TracerName);
            var shim   = new SpanBuilderShim(tracer, "foo");

            // Add a parent. The shim requires that the ISpan implementation be a SpanShim
            shim.AsChildOf(new SpanShim(tracer.StartSpan(SpanName1)));

            // Set to Ignore
            shim.IgnoreActiveSpan();

            // build
            var spanShim = (SpanShim)shim.Start();

            Assert.Equal("foo", spanShim.Span.Activity.OperationName);
        }
        public override async ValueTask Invoke(ServiceContext context, Func <ServiceContext, ValueTask> next)
        {
            // https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/rpc.md#grpc

            // TracerName must match with ActivitySource name.
            var tracer = tracerProvider.GetTracer(telemetryOption.ActivitySourceName, telemetryOption.TracerVersion);

            // Client -> Server incoming filter
            // span name must be `$package.$service/$method` but MagicOnion has no $package.
            using (var span = tracer.StartSpan($"{context.MethodType}:{context.CallContext.Method}", SpanKind.Server))
            {
                try
                {
                    span.SetAttribute("grpc.method", context.MethodType.ToString());
                    span.SetAttribute("rpc.system", "grpc");
                    span.SetAttribute("rpc.service", context.ServiceType.Name);
                    span.SetAttribute("rpc.method", context.CallContext.Method);
                    // todo: context.CallContext.Peer/Host format is https://github.com/grpc/grpc/blob/master/doc/naming.md and not uri standard.
                    span.SetAttribute("net.peer.name", context.CallContext.Peer);
                    span.SetAttribute("net.host.name", context.CallContext.Host);
                    span.SetAttribute("message.type", "RECIEVED");
                    span.SetAttribute("message.id", context.ContextId.ToString());
                    span.SetAttribute("message.uncompressed_size", context.GetRawRequest()?.LongLength.ToString() ?? "0");

                    // todo: net.peer.name not report on tracer. use custom tag
                    span.SetAttribute("magiconion.peer.ip", context.CallContext.Peer);
                    span.SetAttribute("magiconion.auth.enabled", (!string.IsNullOrEmpty(context.CallContext.AuthContext.PeerIdentityPropertyName)).ToString());
                    span.SetAttribute("magiconion.auth.peer.authenticated", context.CallContext.AuthContext.IsPeerAuthenticated.ToString());

                    await next(context);

                    span.SetAttribute("grpc.status_code", ((long)context.CallContext.Status.StatusCode).ToString());
                    span.Status = OpenTelemetrygRpcStatusHelper.ConvertStatus(context.CallContext.Status.StatusCode);
                }
                catch (Exception ex)
                {
                    span.SetAttribute("exception", ex.ToString());
                    span.SetAttribute("grpc.status_code", ((long)context.CallContext.Status.StatusCode).ToString());
                    span.SetAttribute("grpc.status_detail", context.CallContext.Status.Detail);
                    span.Status = OpenTelemetrygRpcStatusHelper.ConvertStatus(context.CallContext.Status.StatusCode);
                    throw;
                }
            }
        }
示例#30
0
        public void Activate()
        {
            var tracer   = TracerProvider.GetTracer(TracerName);
            var shim     = new ScopeManagerShim(tracer);
            var spanShim = new SpanShim(tracer.StartSpan(SpanName));

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

#if DEBUG
            Assert.Equal(0, shim.SpanScopeTableCount);
#endif

            spanShim.Finish();
            Assert.NotEqual(default, spanShim.Span.Activity.Duration);