Example #1
0
        /// <summary>
        /// Adds New Relic exporter to the TracerProvider.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> builder to use.</param>
        /// <param name="configure">A method to configure the exporter options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddNewRelicExporter(this TracerProviderBuilder builder, Action <NewRelicExporterOptions> configure)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var options = new NewRelicExporterOptions();

            configure?.Invoke(options);
            var exporter = new NewRelicTraceExporter(options);

            if (options.ExportProcessorType == ExportProcessorType.Simple)
            {
                return(builder.AddProcessor(new SimpleExportProcessor <Activity>(exporter)));
            }
            else
            {
                return(builder.AddProcessor(new BatchExportProcessor <Activity>(
                                                exporter,
                                                options.BatchExportProcessorOptions.MaxQueueSize,
                                                options.BatchExportProcessorOptions.ScheduledDelayMilliseconds,
                                                options.BatchExportProcessorOptions.ExporterTimeoutMilliseconds,
                                                options.BatchExportProcessorOptions.MaxExportBatchSize)));
            }
        }
        public void Setup()
        {
            var config         = new TelemetryConfiguration().WithAPIKey("123456").WithServiceName(testServiceName);
            var mockDataSender = new NRSpans.SpanDataSender(config);

            //Capture the spans that were requested to be sent to New Relic.
            mockDataSender.WithCaptureSendDataAsyncDelegate((sb, retryId) =>
            {
                _resultNRSpans.AddRange(sb.Spans);
            });

            //Prevent actually sending those spans to New Relic.
            mockDataSender.WithHttpHandlerImpl((json) =>
            {
                return(Task.FromResult(new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.OK)));
            });

            var exporter = new NewRelicTraceExporter(mockDataSender, config, null);

            using (var tracerFactory = TracerFactory.Create(
                       builder => builder.AddProcessorPipeline(
                           c => c.SetExporter(exporter))))
            {
                var tracer = tracerFactory.GetTracer("TestTracer");

                //  Creating the following spans                        Trace       Expected Outcome
                //  -----------------------------------------------------------------------------------
                //  0   Test Span 1                                     Trace 1     Included
                //  1       Test Span 2                                 Trace 1     Included
                //  2   Test Span 3                                     Trace 2     Included
                //  3   Test Span 4                                     Trace 3     Included
                //  4       Shoul be Filtered - HTTP Call to NR         Trace 3     Excluded
                //  5           Should be filtered - Child of HTTP      Trace 3     Excluded

                _otSpans.Add(tracer.StartRootSpan("Test Span 1"));
                _otSpans.Add(tracer.StartSpan("Test Span 2", _otSpans[0]));
                _otSpans.Add(tracer.StartRootSpan("Test Span 3"));
                _otSpans.Add(tracer.StartRootSpan("Test Span 4"));
                _otSpans.Add(tracer.StartSpan("Should Be Filtered - HTTP Call to NR", _otSpans[3]).PutHttpRawUrlAttribute(config.TraceUrl));
                _otSpans.Add(tracer.StartSpan("Should Be Filtered - Child of HTTP", _otSpans[4]));

                _otSpans[0].Status = Status.Ok;
                _otSpans[1].Status = Status.Aborted;
                _otSpans[2].Status = Status.Ok;
                _otSpans[3].Status = Status.Ok;
                _otSpans[4].Status = Status.Ok;
                _otSpans[5].Status = Status.Ok;

                Thread.Sleep(100);
                _otSpans[1].End();
                Thread.Sleep(125);
                _otSpans[0].End();
                Thread.Sleep(150);
                _otSpans[2].End();
                Thread.Sleep(175);
                _otSpans[5].End();
                Thread.Sleep(100);
                _otSpans[4].End();
                Thread.Sleep(50);
                _otSpans[3].End();
            }
        }