Exemplo n.º 1
0
        public void TracerSdkSetsActivityDataRequestedToFalseWhenSuppressInstrumentationIsTrueForLegacyActivity()
        {
            using TestActivityProcessor testActivityProcessor = new TestActivityProcessor();

            bool startCalled = false;
            bool endCalled   = false;

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

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

            using var openTelemetry = Sdk.CreateTracerProviderBuilder()
                                      .AddLegacySource("random")
                                      .AddProcessor(testActivityProcessor)
                                      .SetSampler(new AlwaysOnSampler())
                                      .Build();

            using (SuppressInstrumentationScope.Begin(true))
            {
                using var activity = new Activity("random").Start();
                Assert.False(activity.IsAllDataRequested);
            }

            Assert.False(startCalled);
            Assert.False(endCalled);
        }
Exemplo n.º 2
0
        public void SdkSamplesAndProcessesLegacyActivityWithRightConfigOnWildCardMode()
        {
            bool samplerCalled = false;

            var sampler = new TestSampler
            {
                SamplingAction =
                    (samplingParameters) =>
                {
                    samplerCalled = true;
                    return(new SamplingResult(SamplingDecision.RecordAndSample));
                },
            };

            using TestActivityProcessor testActivityProcessor = new TestActivityProcessor();

            bool startCalled = false;
            bool endCalled   = false;

            testActivityProcessor.StartAction =
                (a) =>
            {
                Assert.True(samplerCalled);
                Assert.False(Sdk.SuppressInstrumentation);
                Assert.True(a.IsAllDataRequested);     // If Proccessor.OnStart is called, activity's IsAllDataRequested is set to true
                startCalled = true;
            };

            testActivityProcessor.EndAction =
                (a) =>
            {
                Assert.False(Sdk.SuppressInstrumentation);
                Assert.True(a.IsAllDataRequested);     // If Processor.OnEnd is called, activity's IsAllDataRequested is set to true
                endCalled = true;
            };

            var emptyActivitySource = new ActivitySource(string.Empty);

            Assert.False(emptyActivitySource.HasListeners()); // No ActivityListener for empty ActivitySource added yet

            var operationNameForLegacyActivity = "TestOperationName";

            // AddLegacyOperationName chained to TracerProviderBuilder
            using var tracerProvider = Sdk.CreateTracerProviderBuilder()
                                       .SetSampler(sampler)
                                       .AddSource("ABCCompany.XYZProduct.*") // Adding a wild card source
                                       .AddProcessor(testActivityProcessor)
                                       .AddLegacySource(operationNameForLegacyActivity)
                                       .Build();

            Assert.True(emptyActivitySource.HasListeners()); // Listener for empty ActivitySource added after TracerProvider build

            Activity activity = new Activity(operationNameForLegacyActivity);

            activity.Start();
            activity.Stop();

            Assert.True(startCalled); // Processor.OnStart is called since we added a legacy OperationName
            Assert.True(endCalled);   // Processor.OnEnd is called since we added a legacy OperationName
        }
Exemplo n.º 3
0
        public void CompositeActivityProcessor_ShutsDownAll()
        {
            var p1 = new TestActivityProcessor(null, null);
            var p2 = new TestActivityProcessor(null, null);

            using (var processor = new CompositeActivityProcessor(new[] { p1, p2 }))
            {
                processor.ShutdownAsync(default).Wait();
        public void BroadcastProcessor_ShutsDownAll()
        {
            var processor1 = new TestActivityProcessor(null, null);
            var processor2 = new TestActivityProcessor(null, null);

            var broadcastProcessor = new BroadcastActivityProcessor(new[] { processor1, processor2 });

            broadcastProcessor.ShutdownAsync(default);
Exemplo n.º 5
0
        public void CompositeActivityProcessor_BadArgs()
        {
            Assert.Throws <ArgumentNullException>(() => new CompositeActivityProcessor(null));
            Assert.Throws <ArgumentException>(() => new CompositeActivityProcessor(new ActivityProcessor[0]));

            using var p1        = new TestActivityProcessor(null, null);
            using var processor = new CompositeActivityProcessor(new[] { p1 });
            Assert.Throws <ArgumentNullException>(() => processor.AddProcessor(null));
        }
Exemplo n.º 6
0
        public void CompositeActivityProcessor_BadArgs()
        {
            Assert.Throws <ArgumentNullException>(() => new CompositeProcessor <Activity>(null));
            Assert.Throws <ArgumentException>(() => new CompositeProcessor <Activity>(Array.Empty <BaseProcessor <Activity> >()));

            using var p1        = new TestActivityProcessor(null, null);
            using var processor = new CompositeProcessor <Activity>(new[] { p1 });
            Assert.Throws <ArgumentNullException>(() => processor.AddProcessor(null));
        }
Exemplo n.º 7
0
        public void CompositeActivityProcessor_ShutsDownAll()
        {
            using var p1 = new TestActivityProcessor(null, null);
            using var p2 = new TestActivityProcessor(null, null);

            using var processor = new CompositeProcessor <Activity>(new[] { p1, p2 });
            processor.Shutdown();
            Assert.True(p1.ShutdownCalled);
            Assert.True(p2.ShutdownCalled);
        }
Exemplo n.º 8
0
        public void CompositeActivityProcessor_ForceFlush(int timeout)
        {
            using var p1 = new TestActivityProcessor(null, null);
            using var p2 = new TestActivityProcessor(null, null);

            using var processor = new CompositeProcessor <Activity>(new[] { p1, p2 });
            processor.ForceFlush(timeout);

            Assert.True(p1.ForceFlushCalled);
            Assert.True(p2.ForceFlushCalled);
        }
Exemplo n.º 9
0
        public void CompositeActivityProcessor_ProcessorThrows()
        {
            using var p1 = new TestActivityProcessor(
                      activity => { throw new Exception("Start exception"); },
                      activity => { throw new Exception("End exception"); });

            var activity = new Activity("test");

            using var processor = new CompositeProcessor <Activity>(new[] { p1 });
            Assert.Throws <Exception>(() => { processor.OnStart(activity); });
            Assert.Throws <Exception>(() => { processor.OnEnd(activity); });
        }
Exemplo n.º 10
0
        public void CompositeActivityProcessor_ForceFlush()
        {
            using var p1 = new TestActivityProcessor(null, null);
            using var p2 = new TestActivityProcessor(null, null);

            using (var processor = new CompositeActivityProcessor(new[] { p1, p2 }))
            {
                processor.ForceFlush();
                Assert.True(p1.ForceFlushCalled);
                Assert.True(p2.ForceFlushCalled);
            }
        }
Exemplo n.º 11
0
        public void TracerProviderSdkFlushesProcessorForcibly()
        {
            using TestActivityProcessor testActivityProcessor = new TestActivityProcessor();

            using var tracerProvider = Sdk.CreateTracerProviderBuilder()
                                       .AddProcessor(testActivityProcessor)
                                       .Build();

            var isFlushed = tracerProvider.ForceFlush();

            Assert.True(isFlushed);
            Assert.True(testActivityProcessor.ForceFlushCalled);
        }
        public void BroadcastProcessor_OneProcessorThrows()
        {
            bool start1Called = false;
            bool start2Called = false;
            bool end1Called   = false;
            bool end2Called   = false;
            var  processor1   = new TestActivityProcessor(
                ss =>
            {
                start1Called = true;
                Assert.False(start2Called);
                Assert.False(end1Called);
                Assert.False(end2Called);

                throw new Exception("Start exception");
            }, se =>
            {
                end1Called = true;
                Assert.True(start1Called);
                Assert.True(start2Called);
                Assert.False(end2Called);
                throw new Exception("End exception");
            });

            var processor2 = new TestActivityProcessor(
                ss =>
            {
                start2Called = true;
                Assert.True(start1Called);
                Assert.False(end1Called);
                Assert.False(end2Called);
            }, se =>
            {
                end2Called = true;
                Assert.True(start1Called);
                Assert.True(start2Called);
                Assert.True(end1Called);
            });

            var broadcastProcessor = new BroadcastActivityProcessor(new[] { processor1, processor2 });

            var activity = new Activity("somename");

            broadcastProcessor.OnStart(activity);
            Assert.True(start1Called);
            Assert.True(start2Called);

            broadcastProcessor.OnEnd(activity);
            Assert.True(end1Called);
            Assert.True(end2Called);
        }
Exemplo n.º 13
0
        public void SdkProcessesLegacyActivityWhenActivitySourceIsUpdatedWithAddSource()
        {
            using TestActivityProcessor testActivityProcessor = new TestActivityProcessor();

            bool startCalled = false;
            bool endCalled   = false;

            testActivityProcessor.StartAction =
                (a) =>
            {
                Assert.False(Sdk.SuppressInstrumentation);
                Assert.True(a.IsAllDataRequested);     // If Proccessor.OnStart is called, activity's IsAllDataRequested is set to true
                startCalled = true;
            };

            testActivityProcessor.EndAction =
                (a) =>
            {
                Assert.False(Sdk.SuppressInstrumentation);
                Assert.True(a.IsAllDataRequested);     // If Processor.OnEnd is called, activity's IsAllDataRequested is set to true
                endCalled = true;
            };

            var emptyActivitySource = new ActivitySource(string.Empty);

            Assert.False(emptyActivitySource.HasListeners()); // No ActivityListener for empty ActivitySource added yet

            var operationNameForLegacyActivity = "TestOperationName";
            var activitySourceForLegacyActvity = new ActivitySource("TestActivitySource", "1.0.0");

            // AddLegacyOperationName chained to TracerProviderBuilder
            using var tracerProvider = Sdk.CreateTracerProviderBuilder()
                                       .AddSource(activitySourceForLegacyActvity.Name) // Add the updated ActivitySource as a Source
                                       .AddLegacySource(operationNameForLegacyActivity)
                                       .AddProcessor(testActivityProcessor)
                                       .Build();

            Assert.True(emptyActivitySource.HasListeners()); // Listener for empty ActivitySource added after TracerProvider build

            Activity activity = new Activity(operationNameForLegacyActivity);

            activity.Start();
            ActivityInstrumentationHelper.SetActivitySourceProperty(activity, activitySourceForLegacyActvity);
            activity.Stop();

            Assert.True(startCalled); // Processor.OnStart is called since we provided the legacy OperationName
            Assert.True(endCalled);   // Processor.OnEnd is not called since the ActivitySource is updated and the updated source name is added as a Source to the provider
        }
        public void BroadcastProcessor_CallsAllProcessorSequentially()
        {
            bool start1Called = false;
            bool start2Called = false;
            bool end1Called   = false;
            bool end2Called   = false;
            var  processor1   = new TestActivityProcessor(
                ss =>
            {
                start1Called = true;
                Assert.False(start2Called);
                Assert.False(end1Called);
                Assert.False(end2Called);
            }, se =>
            {
                end1Called = true;
                Assert.True(start1Called);
                Assert.True(start2Called);
                Assert.False(end2Called);
            });
            var processor2 = new TestActivityProcessor(
                ss =>
            {
                start2Called = true;
                Assert.True(start1Called);
                Assert.False(end1Called);
                Assert.False(end2Called);
            }, se =>
            {
                end2Called = true;
                Assert.True(start1Called);
                Assert.True(start2Called);
                Assert.True(end1Called);
            });

            var broadcastProcessor = new BroadcastActivityProcessor(new[] { processor1, processor2 });

            var activity = new Activity("somename");

            broadcastProcessor.OnStart(activity);
            Assert.True(start1Called);
            Assert.True(start2Called);

            broadcastProcessor.OnEnd(activity);
            Assert.True(end1Called);
            Assert.True(end2Called);
        }
Exemplo n.º 15
0
        public void ProcessorDoesNotReceiveNotRecordDecisionSpan()
        {
            var testSampler = new TestSampler();

            using TestActivityProcessor testActivityProcessor = new TestActivityProcessor();

            bool startCalled = false;
            bool endCalled   = false;

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

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

            using var openTelemetry = Sdk.CreateTracerProviderBuilder()
                                      .AddSource("random")
                                      .AddProcessor(testActivityProcessor)
                                      .SetSampler(testSampler)
                                      .Build();

            testSampler.SamplingAction = (samplingParameters) =>
            {
                return(new SamplingResult(SamplingDecision.Drop));
            };

            using ActivitySource source = new ActivitySource("random");
            var activity = source.StartActivity("somename");

            activity.Stop();

            Assert.False(activity.IsAllDataRequested);
            Assert.Equal(ActivityTraceFlags.None, activity.ActivityTraceFlags);
            Assert.False(activity.Recorded);
            Assert.False(startCalled);
            Assert.False(endCalled);
        }
Exemplo n.º 16
0
        public void SdkDoesNotProcessLegacyActivityWithNoAdditionalConfig()
        {
            using TestActivityProcessor testActivityProcessor = new TestActivityProcessor();

            bool startCalled = false;
            bool endCalled   = false;

            testActivityProcessor.StartAction =
                (a) =>
            {
                Assert.False(Sdk.SuppressInstrumentation);
                Assert.True(a.IsAllDataRequested);     // If Proccessor.OnStart is called, activity's IsAllDataRequested is set to true
                startCalled = true;
            };

            testActivityProcessor.EndAction =
                (a) =>
            {
                Assert.False(Sdk.SuppressInstrumentation);
                Assert.True(a.IsAllDataRequested);     // If Processor.OnEnd is called, activity's IsAllDataRequested is set to true
                endCalled = true;
            };

            var emptyActivitySource = new ActivitySource(string.Empty);

            Assert.False(emptyActivitySource.HasListeners()); // No ActivityListener for empty ActivitySource added yet

            // No AddLegacyOperationName chained to TracerProviderBuilder
            using var tracerProvider = Sdk.CreateTracerProviderBuilder()
                                       .AddProcessor(testActivityProcessor)
                                       .Build();

            Assert.False(emptyActivitySource.HasListeners()); // No listener for empty ActivitySource even after build

            Activity activity = new Activity("Test");

            activity.Start();
            activity.Stop();

            Assert.False(startCalled); // Processor.OnStart is not called since we did not add any legacy OperationName
            Assert.False(endCalled);   // Processor.OnEnd is not called since we did not add any legacy OperationName
        }
Exemplo n.º 17
0
        public void CompositeActivityProcessor_CallsAllProcessorSequentially()
        {
            var result = string.Empty;

            var p1 = new TestActivityProcessor(
                activity => { result += "1"; },
                activity => { result += "3"; });
            var p2 = new TestActivityProcessor(
                activity => { result += "2"; },
                activity => { result += "4"; });

            var activity = new Activity("test");

            using (var processor = new CompositeActivityProcessor(new[] { p1, p2 }))
            {
                processor.OnStart(activity);
                processor.OnEnd(activity);
            }

            Assert.Equal("1234", result);
        }
Exemplo n.º 18
0
        public void CompositeActivityProcessor_ForceFlush(int timeout)
        {
            using var p1 = new TestActivityProcessor(null, null);
            using var p2 = new TestActivityProcessor(null, null);

            using (var processor = new CompositeActivityProcessor(new[] { p1, p2 }))
            {
                processor.ForceFlush(timeout);

                if (timeout != 0)
                {
                    Assert.True(p1.ForceFlushCalled);
                    Assert.True(p2.ForceFlushCalled);
                }
                else
                {
                    Assert.False(p1.ForceFlushCalled);
                    Assert.False(p2.ForceFlushCalled);
                }
            }
        }
        public void TracerProvideSdkCreatesActivitySource()
        {
            using TestActivityProcessor testActivityProcessor = new TestActivityProcessor();

            bool startCalled = false;
            bool endCalled   = false;

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

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

            TestInstrumentation testInstrumentation = null;

            using var tracerProvider = Sdk.CreateTracerProviderBuilder()
                                       .AddProcessor(testActivityProcessor)
                                       .AddDiagnosticSourceInstrumentation((adapter) =>
            {
                testInstrumentation = new TestInstrumentation(adapter);
                return(testInstrumentation);
            })
                                       .Build();

            var      adapter  = testInstrumentation.Adapter;
            Activity activity = new Activity("test");

            activity.Start();
            adapter.Start(activity, ActivityKind.Internal, new ActivitySource("test", "1.0.0"));
            adapter.Stop(activity);
            activity.Stop();

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

            // As Processors can be added anytime after Provider construction,
            // the following validates that updated processors are reflected
            // in ActivitySourceAdapter.
            TestActivityProcessor testActivityProcessorNew = new TestActivityProcessor();

            bool startCalledNew = false;
            bool endCalledNew   = false;

            testActivityProcessorNew.StartAction =
                (a) =>
            {
                startCalledNew = true;
            };

            testActivityProcessorNew.EndAction =
                (a) =>
            {
                endCalledNew = true;
            };

            tracerProvider.AddProcessor(testActivityProcessorNew);
            Activity activityNew = new Activity("test");

            activityNew.Start();
            adapter.Start(activityNew, ActivityKind.Internal, new ActivitySource("test", "1.0.0"));
            adapter.Stop(activityNew);
            activityNew.Stop();

            Assert.True(startCalledNew);
            Assert.True(endCalledNew);
        }
Exemplo n.º 20
0
        public void SdkProcessesLegacyActivityEvenAfterAddingNewProcessor()
        {
            using TestActivityProcessor testActivityProcessor = new TestActivityProcessor();

            bool startCalled = false;
            bool endCalled   = false;

            testActivityProcessor.StartAction =
                (a) =>
            {
                Assert.False(Sdk.SuppressInstrumentation);
                Assert.True(a.IsAllDataRequested);     // If Proccessor.OnStart is called, activity's IsAllDataRequested is set to true
                startCalled = true;
            };

            testActivityProcessor.EndAction =
                (a) =>
            {
                Assert.False(Sdk.SuppressInstrumentation);
                Assert.True(a.IsAllDataRequested);     // If Processor.OnEnd is called, activity's IsAllDataRequested is set to true
                endCalled = true;
            };

            var operationNameForLegacyActivity = "TestOperationName";

            // AddLegacyOperationName chained to TracerProviderBuilder
            using var tracerProvider = Sdk.CreateTracerProviderBuilder()
                                       .AddProcessor(testActivityProcessor)
                                       .AddLegacySource(operationNameForLegacyActivity)
                                       .Build();

            Activity activity = new Activity(operationNameForLegacyActivity);

            activity.Start();
            activity.Stop();

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

            // As Processors can be added anytime after Provider construction, the following validates
            // the following validates that updated processors are processing the legacy activities created from here on.
            TestActivityProcessor testActivityProcessorNew = new TestActivityProcessor();

            bool startCalledNew = false;
            bool endCalledNew   = false;

            testActivityProcessorNew.StartAction =
                (a) =>
            {
                Assert.False(Sdk.SuppressInstrumentation);
                Assert.True(a.IsAllDataRequested);     // If Proccessor.OnStart is called, activity's IsAllDataRequested is set to true
                startCalledNew = true;
            };

            testActivityProcessorNew.EndAction =
                (a) =>
            {
                Assert.False(Sdk.SuppressInstrumentation);
                Assert.True(a.IsAllDataRequested);     // If Processor.OnEnd is called, activity's IsAllDataRequested is set to true
                endCalledNew = true;
            };

            tracerProvider.AddProcessor(testActivityProcessorNew);

            Activity activityNew = new Activity(operationNameForLegacyActivity); // Create a new Activity with the same operation name

            activityNew.Start();
            activityNew.Stop();

            Assert.True(startCalledNew);
            Assert.True(endCalledNew);
        }
 public ActivitySourceAdapterTest()
 {
     this.testSampler           = new TestSampler();
     this.testProcessor         = new TestActivityProcessor();
     this.activitySourceAdapter = new ActivitySourceAdapter(this.testSampler, this.testProcessor, this.testResource);
 }
Exemplo n.º 22
0
        public void TracerProvideSdkCreatesActivitySource()
        {
            TestActivityProcessor testActivityProcessor = new TestActivityProcessor();

            bool startCalled = false;
            bool endCalled   = false;

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

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

            TestInstrumentation testInstrumentation = null;

            using var tracerProvider = Sdk.CreateTracerProviderBuilder()
                                       .AddProcessor(testActivityProcessor)
                                       .AddInstrumentation((adapter) =>
            {
                testInstrumentation = new TestInstrumentation(adapter);
                return(testInstrumentation);
            })
                                       .Build();

            var      adapter  = testInstrumentation.Adapter;
            Activity activity = new Activity("test");

            activity.Start();
            adapter.Start(activity);
            adapter.Stop(activity);
            activity.Stop();

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

            /*
             * Uncomment when issue 1075 is fixed.
             * TestActivityProcessor testActivityProcessorNew = new TestActivityProcessor();
             *
             * bool startCalledNew = false;
             * bool endCalledNew = false;
             *
             * testActivityProcessorNew.StartAction =
             *  (a) =>
             *  {
             *      startCalledNew = true;
             *  };
             *
             * testActivityProcessorNew.EndAction =
             *  (a) =>
             *  {
             *      endCalledNew = true;
             *  };
             *
             * tracerProvider.AddProcessor(testActivityProcessorNew);
             * Activity activityNew = new Activity("test");
             * activityNew.Start();
             * adapter.Start(activityNew);
             * adapter.Stop(activityNew);
             * activityNew.Stop();
             *
             * Assert.True(startCalledNew);
             * Assert.True(endCalledNew);
             */
        }