コード例 #1
0
        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 = Sdk.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]);
        }
コード例 #2
0
        public void TraceExportResultIsSuccess(OtlpExportProtocol protocol, string endpoint, ExportProcessorType exportProcessorType, bool forceFlush)
        {
#if NETCOREAPP3_1
            // Adding the OtlpExporter creates a GrpcChannel.
            // This switch must be set before creating a GrpcChannel when calling an insecure HTTP/2 endpoint.
            // See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
            if (protocol == OtlpExportProtocol.Grpc)
            {
                AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            }
#endif
            using EventWaitHandle handle = new ManualResetEvent(false);

            var exporterOptions = new OtlpExporterOptions
            {
                Endpoint                    = new Uri($"http://{CollectorHostname}{endpoint}"),
                Protocol                    = protocol,
                ExportProcessorType         = exportProcessorType,
                BatchExportProcessorOptions = new()
                {
                    ScheduledDelayMilliseconds = ExportIntervalMilliseconds,
                },
            };

            DelegatingTestExporter <Activity> delegatingExporter = null;

            var activitySourceName = "otlp.collector.test";

            var builder = Sdk.CreateTracerProviderBuilder()
                          .AddSource(activitySourceName);

            OtlpTraceExporterHelperExtensions.AddOtlpExporter(
                builder,
                exporterOptions,
                configure: null,
                serviceProvider: null,
                configureExporterInstance: otlpExporter =>
            {
                delegatingExporter = new DelegatingTestExporter <Activity>(otlpExporter, onExportAction: () => handle.Set());
                return(delegatingExporter);
            });

            using (var tracerProvider = builder.Build())
            {
                using var source = new ActivitySource(activitySourceName);
                var activity = source.StartActivity($"{protocol} Test Activity");
                activity?.Stop();

                Assert.NotNull(delegatingExporter);

                if (forceFlush)
                {
                    Assert.True(tracerProvider.ForceFlush());
                    Assert.Single(delegatingExporter.ExportResults);
                    Assert.Equal(ExportResult.Success, delegatingExporter.ExportResults[0]);
                }
                else if (exporterOptions.ExportProcessorType == ExportProcessorType.Batch)
                {
                    Assert.True(handle.WaitOne(ExportIntervalMilliseconds * 2));
                    Assert.Single(delegatingExporter.ExportResults);
                    Assert.Equal(ExportResult.Success, delegatingExporter.ExportResults[0]);
                }
            }

            if (!forceFlush && exportProcessorType == ExportProcessorType.Simple)
            {
                Assert.Single(delegatingExporter.ExportResults);
                Assert.Equal(ExportResult.Success, delegatingExporter.ExportResults[0]);
            }
        }
コード例 #3
0
        public void MetricExportResultIsSuccess(OtlpExportProtocol protocol, string endpoint, bool useManualExport, bool forceFlush)
        {
#if NETCOREAPP3_1
            // Adding the OtlpExporter creates a GrpcChannel.
            // This switch must be set before creating a GrpcChannel when calling an insecure HTTP/2 endpoint.
            // See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
            if (protocol == OtlpExportProtocol.Grpc)
            {
                AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            }
#endif
            using EventWaitHandle handle = new ManualResetEvent(false);

            var exporterOptions = new OtlpExporterOptions
            {
                Endpoint = new Uri($"http://{CollectorHostname}{endpoint}"),
                Protocol = protocol,
            };

            DelegatingTestExporter <Metric> delegatingExporter = null;

            var meterName = "otlp.collector.test";

            var builder = Sdk.CreateMeterProviderBuilder()
                          .AddMeter(meterName);

            var readerOptions = new MetricReaderOptions();
            readerOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = useManualExport ? Timeout.Infinite : ExportIntervalMilliseconds;

            OtlpMetricExporterExtensions.AddOtlpExporter(
                builder,
                exporterOptions,
                readerOptions,
                configureExporter: null,
                configureExporterAndMetricReader: null,
                serviceProvider: null,
                configureExporterInstance: otlpExporter =>
            {
                delegatingExporter = new DelegatingTestExporter <Metric>(otlpExporter, onExportAction: () => handle.Set());
                return(delegatingExporter);
            });

            using (var meterProvider = builder.Build())
            {
                using var meter = new Meter(meterName);

                var counter = meter.CreateCounter <int>("test_counter");

                counter.Add(18);

                Assert.NotNull(delegatingExporter);

                if (forceFlush)
                {
                    Assert.True(meterProvider.ForceFlush());
                    Assert.Single(delegatingExporter.ExportResults);
                    Assert.Equal(ExportResult.Success, delegatingExporter.ExportResults[0]);
                }
                else if (!useManualExport)
                {
                    Assert.True(handle.WaitOne(ExportIntervalMilliseconds * 2));
                    Assert.Single(delegatingExporter.ExportResults);
                    Assert.Equal(ExportResult.Success, delegatingExporter.ExportResults[0]);
                }
            }

            if (!forceFlush && useManualExport)
            {
                Assert.Single(delegatingExporter.ExportResults);
                Assert.Equal(ExportResult.Success, delegatingExporter.ExportResults[0]);
            }
        }