public void TracerProviderSdkSamplerAttributesAreAppliedToActivity(SamplingDecision sampling)
        {
            var testSampler = new TestSampler();

            testSampler.SamplingAction = (samplingParams) =>
            {
                var attributes = new Dictionary <string, object>();
                attributes.Add("tagkeybysampler", "tagvalueaddedbysampler");
                return(new SamplingResult(sampling, attributes));
            };

            using var activitySource = new ActivitySource(ActivitySourceName);
            using var sdk            = OpenTelemetrySdk.CreateTracerProviderBuilder()
                                       .AddSource(ActivitySourceName)
                                       .SetSampler(testSampler)
                                       .Build();

            using (var rootActivity = activitySource.StartActivity("root"))
            {
                Assert.NotNull(rootActivity);
                Assert.Equal(rootActivity.TraceId, testSampler.LatestSamplingParameters.TraceId);
                if (sampling != SamplingDecision.Drop)
                {
                    Assert.Contains(new KeyValuePair <string, object>("tagkeybysampler", "tagvalueaddedbysampler"), rootActivity.TagObjects);
                }
            }
        }
        public void ZPagesExporter_CustomActivityProcessor()
        {
            const string          ActivitySourceName    = "zpages.test";
            Guid                  requestId             = Guid.NewGuid();
            TestActivityProcessor testActivityProcessor = new TestActivityProcessor();

            bool startCalled = false;
            bool endCalled   = false;

            testActivityProcessor.StartAction =
                (a) =>
            {
                startCalled = true;
            };

            testActivityProcessor.EndAction =
                (a) =>
            {
                endCalled = true;
            };

            var openTelemetrySdk = OpenTelemetrySdk.CreateTracerProvider(b => b
                                                                         .AddActivitySource(ActivitySourceName)
                                                                         .UseZPagesExporter(
                                                                             processorConfigure: p => p.AddProcessor((next) => testActivityProcessor)));

            var source   = new ActivitySource(ActivitySourceName);
            var activity = source.StartActivity("Test Zipkin Activity");

            activity?.Stop();

            Assert.True(startCalled);
            Assert.True(endCalled);
        }
示例#3
0
 public OpenTelemetrySdkBenchmarksActivity()
 {
     // Not configuring pipeline, which will result in default NoOpActivityProcessor.
     var openTel = OpenTelemetrySdk.EnableOpenTelemetry(
         (builder) => builder.AddActivitySource("BenchMark")
         );
 }
        public async Task HttpWebRequestInstrumentationInjectsHeadersAsync()
        {
            var activityProcessor = new Mock <BaseProcessor <Activity> >();

            using var shutdownSignal = OpenTelemetrySdk.CreateTracerProviderBuilder()
                                       .AddProcessor(activityProcessor.Object)
                                       .AddHttpClientInstrumentation()
                                       .Build();

            var request = (HttpWebRequest)WebRequest.Create(this.url);

            request.Method = "GET";

            var parent = new Activity("parent")
                         .SetIdFormat(ActivityIdFormat.W3C)
                         .Start();

            parent.TraceStateString   = "k1=v1,k2=v2";
            parent.ActivityTraceFlags = ActivityTraceFlags.Recorded;

            using var response = await request.GetResponseAsync();

            Assert.Equal(3, activityProcessor.Invocations.Count);  // SetParentProvider/Begin/End called
            var activity = (Activity)activityProcessor.Invocations[2].Arguments[0];

            Assert.Equal(parent.TraceId, activity.Context.TraceId);
            Assert.Equal(parent.SpanId, activity.ParentSpanId);
            Assert.NotEqual(parent.SpanId, activity.Context.SpanId);
            Assert.NotEqual(default, activity.Context.SpanId);
        private static object RunWithActivitySource(string endpoint)
        {
            // Adding the OtlpExporter creates a GrpcChannel.
            // This switch must be set before creating a GrpcChannel/HttpClient when calling an insecure gRPC service.
            // See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

            // Enable OpenTelemetry for the sources "Samples.SampleServer" and "Samples.SampleClient"
            // and use OTLP exporter.
            using var openTelemetry = OpenTelemetrySdk.CreateTracerProviderBuilder()
                                      .AddSource("Samples.SampleClient", "Samples.SampleServer")
                                      .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("otlp-test"))
                                      .AddOtlpExporter(opt => opt.Endpoint = new Uri(endpoint))
                                      .Build();

            // The above line is required only in Applications
            // which decide to use OpenTelemetry.
            using (var sample = new InstrumentationWithActivitySource())
            {
                sample.Start();

                System.Console.WriteLine("Traces are being created and exported" +
                                         "to the OpenTelemetry Collector in the background. " +
                                         "Press ENTER to stop.");
                System.Console.ReadLine();
            }

            return(null);
        }
        private static object RunWithActivitySource()
        {
            // Enable OpenTelemetry for the sources "Samples.SampleServer" and "Samples.SampleClient"
            // and use Console exporter.
            using var openTelemetry = OpenTelemetrySdk.CreateTracerProviderBuilder()
                                      .AddSource("Samples.SampleClient", "Samples.SampleServer")
                                      .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("console-test"))
                                      .AddProcessor(new MyProcessor()) // This must be added before ConsoleExporter
                                      .AddConsoleExporter()
                                      .Build();

            // The above line is required only in applications
            // which decide to use OpenTelemetry.
            using (var sample = new InstrumentationWithActivitySource())
            {
                sample.Start();

                System.Console.WriteLine("Traces are being created and exported " +
                                         "to Console in the background. " +
                                         "Press ENTER to stop.");
                System.Console.ReadLine();
            }

            return(null);
        }
示例#7
0
        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 tracerProvider = OpenTelemetrySdk.CreateTracerProviderBuilder()
                                       .AddSource("MyCompany.MyProduct.MyWebServer")
                                       .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("MyServiceName"))
                                       .AddConsoleExporter()
                                       .Build();

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

            var tracer = TracerProvider.Default.GetTracer("MyCompany.MyProduct.MyWebServer");

            using (var parentSpan = tracer.StartActiveSpan("parent span"))
            {
                parentSpan.SetAttribute("mystring", "value");
                parentSpan.SetAttribute("myint", 100);
                parentSpan.SetAttribute("mydouble", 101.089);
                parentSpan.SetAttribute("mybool", true);
                parentSpan.UpdateName("parent span new name");

                var childSpan = tracer.StartSpan("child span");
                childSpan.AddEvent("sample event").SetAttribute("ch", "value").SetAttribute("more", "attributes");
                childSpan.SetStatus(Status.Ok);
                childSpan.End();
            }

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

            return(null);
        }
示例#8
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);
        }
        public void EnumerateLinksNonempty()
        {
            using var openTelemetrySdk = OpenTelemetrySdk.CreateTracerProviderBuilder()
                                         .AddSource(ActivitySourceName)
                                         .Build();

            var links = new[]
示例#10
0
        public TraceShimBenchmarks()
        {
            Activity.DefaultIdFormat = ActivityIdFormat.W3C;

            OpenTelemetrySdk.CreateTracerProviderBuilder()
            .SetSampler(new AlwaysOnSampler())
            .AddSource("Benchmark.OneProcessor")
            .AddProcessor(new DummyActivityProcessor())
            .Build();

            OpenTelemetrySdk.CreateTracerProviderBuilder()
            .SetSampler(new AlwaysOnSampler())
            .AddSource("Benchmark.TwoProcessors")
            .AddProcessor(new DummyActivityProcessor())
            .AddProcessor(new DummyActivityProcessor())
            .Build();

            OpenTelemetrySdk.CreateTracerProviderBuilder()
            .SetSampler(new AlwaysOnSampler())
            .AddSource("Benchmark.ThreeProcessors")
            .AddProcessor(new DummyActivityProcessor())
            .AddProcessor(new DummyActivityProcessor())
            .AddProcessor(new DummyActivityProcessor())
            .Build();
        }
        public void ExportResultIsSuccess()
        {
#if NETCOREAPP3_1
            // Adding the OtlpExporter creates a GrpcChannel.
            // This switch must be set before creating a GrpcChannel/HttpClient when calling an insecure gRPC service.
            // See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
#endif

            var exporterOptions = new OtlpExporterOptions
            {
                Endpoint = new System.Uri($"http://{CollectorEndpoint}"),
            };

            var otlpExporter            = new OtlpTraceExporter(exporterOptions);
            var delegatingExporter      = new DelegatingTestExporter <Activity>(otlpExporter);
            var exportActivityProcessor = new SimpleActivityExportProcessor(delegatingExporter);

            var activitySourceName = "otlp.collector.test";

            var builder = OpenTelemetrySdk.CreateTracerProviderBuilder()
                          .AddSource(activitySourceName)
                          .AddProcessor(exportActivityProcessor);

            using var tracerProvider = builder.Build();

            var source   = new ActivitySource(activitySourceName);
            var activity = source.StartActivity("Test Activity");
            activity?.Stop();

            Assert.Single(delegatingExporter.ExportResults);
            Assert.Equal(ExportResult.Success, delegatingExporter.ExportResults[0]);
        }
        public void UseOpenTelemetryProtocolActivityExporterWithCustomActivityProcessor()
        {
            const string          ActivitySourceName    = "otlp.test";
            TestActivityProcessor testActivityProcessor = new TestActivityProcessor();

            bool startCalled = false;
            bool endCalled   = false;

            testActivityProcessor.StartAction =
                (a) =>
            {
                startCalled = true;
            };

            testActivityProcessor.EndAction =
                (a) =>
            {
                endCalled = true;
            };

            var openTelemetrySdk = OpenTelemetrySdk.CreateTracerProvider(b => b
                                                                         .AddActivitySource(ActivitySourceName)
                                                                         .UseOtlpExporter(
                                                                             null, p => p.AddProcessor((next) => testActivityProcessor)));

            var source   = new ActivitySource(ActivitySourceName);
            var activity = source.StartActivity("Test Otlp Activity");

            activity?.Stop();

            Assert.True(startCalled);
            Assert.True(endCalled);
        }