Exemplo n.º 1
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.º 2
0
        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            = Sdk.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);
                }
            }
        }
Exemplo n.º 3
0
        public void SdkSamplesLegacyActivityWithCustomSampler(SamplingDecision samplingDecision, bool isAllDataRequested, bool hasRecordedFlag)
        {
            var operationNameForLegacyActivity = "TestOperationName";
            var sampler = new TestSampler()
            {
                SamplingAction = (samplingParameters) => new SamplingResult(samplingDecision)
            };

            using var tracerProvider = Sdk.CreateTracerProviderBuilder()
                                       .SetSampler(sampler)
                                       .AddLegacySource(operationNameForLegacyActivity)
                                       .Build();

            Activity activity = new Activity(operationNameForLegacyActivity);

            activity.Start();

            Assert.Equal(isAllDataRequested, activity.IsAllDataRequested);
            Assert.Equal(hasRecordedFlag, activity.ActivityTraceFlags.HasFlag(ActivityTraceFlags.Recorded));

            // Validating ActivityTraceFlags is not enough as it does not get reflected on
            // Id, If the Id is accessed before the sampler runs.
            // https://github.com/open-telemetry/opentelemetry-dotnet/issues/2700
            Assert.EndsWith(hasRecordedFlag ? "-01" : "-00", activity.Id);

            activity.Stop();
        }
        public void ProcessorSendsRecordAndSampledDecisionSpanToExporter()
        {
            var testSampler = new TestSampler();

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

            int exportCalledCount = 0;
            var activityExporter  = new TestActivityExporter(_ => Interlocked.Increment(ref exportCalledCount));

            using var activityProcessor = new BatchingActivityProcessor(activityExporter, 128, DefaultDelay, DefaultTimeout, 1);
            using var openTelemetrySdk  = Sdk.CreateTracerProviderBuilder()
                                          .AddSource(ActivitySourceName)
                                          .AddProcessor(activityProcessor)
                                          .SetSampler(testSampler)
                                          .Build();

            var activity1 = this.CreateSampledEndedActivity(ActivityName1);
            var activity2 = this.CreateNotSampledEndedActivity(ActivityName2);

            Assert.True(activity1.IsAllDataRequested);
            Assert.Equal(ActivityTraceFlags.Recorded, activity1.ActivityTraceFlags);
            Assert.True(activity1.Recorded);

            Assert.True(activity2.IsAllDataRequested);
            Assert.Equal(ActivityTraceFlags.Recorded, activity2.ActivityTraceFlags);
            Assert.True(activity2.Recorded);

            var exported = this.WaitForActivities(activityExporter, 2, DefaultTimeout);

            Assert.Equal(2, exportCalledCount);
            Assert.Equal(2, exported.Length);
        }
Exemplo n.º 5
0
        public void TracerSdkSetsActivitySamplingResultBasedOnSamplingDecision()
        {
            var testSampler = new TestSampler();

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

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

            using (var activity = activitySource.StartActivity("root"))
            {
                Assert.NotNull(activity);
                Assert.True(activity.IsAllDataRequested);
                Assert.True(activity.Recorded);
            }

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

            using (var activity = activitySource.StartActivity("root"))
            {
                // Even if sampling returns false, for root activities,
                // activity is still created with PropagationOnly.
                Assert.NotNull(activity);
                Assert.True(activity.IsAllDataRequested);
                Assert.False(activity.Recorded);
            }

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

            using (var activity = activitySource.StartActivity("root"))
            {
                // Even if sampling returns false, for root activities,
                // activity is still created with PropagationOnly.
                Assert.NotNull(activity);
                Assert.False(activity.IsAllDataRequested);
                Assert.False(activity.Recorded);

                using (var innerActivity = activitySource.StartActivity("inner"))
                {
                    // This is not a root activity.
                    // If sampling returns false, no activity is created at all.
                    Assert.Null(innerActivity);
                }
            }
        }
Exemplo n.º 6
0
        public void TracerSdkSetsActivitySamplingResultToNoneWhenSuppressInstrumentationIsTrue()
        {
            using var scope = SuppressInstrumentationScope.Begin();

            var testSampler = new TestSampler();

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

            using (var activity = activitySource.StartActivity("root"))
            {
                Assert.Null(activity);
            }
        }
Exemplo n.º 7
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.º 8
0
        public void SdkSamplesLegacyActivityWithCustomSampler(SamplingDecision samplingDecision, bool isAllDataRequested, bool hasRecordedFlag)
        {
            var operationNameForLegacyActivity = "TestOperationName";
            var sampler = new TestSampler()
            {
                SamplingAction = (samplingParameters) => new SamplingResult(samplingDecision)
            };

            using var tracerProvider = Sdk.CreateTracerProviderBuilder()
                                       .SetSampler(sampler)
                                       .AddLegacySource(operationNameForLegacyActivity)
                                       .Build();

            Activity activity = new Activity(operationNameForLegacyActivity);

            activity.Start();

            Assert.Equal(isAllDataRequested, activity.IsAllDataRequested);
            Assert.Equal(hasRecordedFlag, activity.ActivityTraceFlags.HasFlag(ActivityTraceFlags.Recorded));

            activity.Stop();
        }
 public ActivitySourceAdapterTest()
 {
     this.testSampler           = new TestSampler();
     this.testProcessor         = new TestActivityProcessor();
     this.activitySourceAdapter = new ActivitySourceAdapter(this.testSampler, this.testProcessor, this.testResource);
 }
Exemplo n.º 10
0
        public void TracerProviderSdkInvokesSamplingWithCorrectParameters()
        {
            var testSampler = new TestSampler();

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

            // OpenTelemetry Sdk is expected to set default to W3C.
            Assert.True(Activity.DefaultIdFormat == ActivityIdFormat.W3C);

            using (var rootActivity = activitySource.StartActivity("root"))
            {
                Assert.NotNull(rootActivity);
                Assert.True(rootActivity.ParentSpanId == default);

                // Validate that the TraceId seen by Sampler is same as the
                // Activity when it got created.
                Assert.Equal(rootActivity.TraceId, testSampler.LatestSamplingParameters.TraceId);
            }

            using (var parent = activitySource.StartActivity("parent", ActivityKind.Client))
            {
                Assert.Equal(parent.TraceId, testSampler.LatestSamplingParameters.TraceId);
                using (var child = activitySource.StartActivity("child"))
                {
                    Assert.Equal(child.TraceId, testSampler.LatestSamplingParameters.TraceId);
                    Assert.Equal(parent.TraceId, child.TraceId);
                    Assert.Equal(parent.SpanId, child.ParentSpanId);
                }
            }

            var customContext = new ActivityContext(
                ActivityTraceId.CreateRandom(),
                ActivitySpanId.CreateRandom(),
                ActivityTraceFlags.None);

            using (var fromCustomContext =
                       activitySource.StartActivity("customContext", ActivityKind.Client, customContext))
            {
                Assert.Equal(fromCustomContext.TraceId, testSampler.LatestSamplingParameters.TraceId);
                Assert.Equal(customContext.TraceId, fromCustomContext.TraceId);
                Assert.Equal(customContext.SpanId, fromCustomContext.ParentSpanId);
                Assert.NotEqual(customContext.SpanId, fromCustomContext.SpanId);
            }

            // Validate that when StartActivity is called using Parent as string,
            // Sampling is called correctly.
            var act = new Activity("anything").Start();

            act.Stop();
            var customContextAsString = act.Id;
            var expectedTraceId       = act.TraceId;
            var expectedParentSpanId  = act.SpanId;

            using (var fromCustomContextAsString =
                       activitySource.StartActivity("customContext", ActivityKind.Client, customContextAsString))
            {
                Assert.Equal(fromCustomContextAsString.TraceId, testSampler.LatestSamplingParameters.TraceId);
                Assert.Equal(expectedTraceId, fromCustomContextAsString.TraceId);
                Assert.Equal(expectedParentSpanId, fromCustomContextAsString.ParentSpanId);
            }

            using (var fromInvalidW3CIdParent =
                       activitySource.StartActivity("customContext", ActivityKind.Client, "InvalidW3CIdParent"))
            {
                // OpenTelemetry ActivityContext does not support
                // non W3C Ids. Starting activity with non W3C Ids
                // will result in no activity being created.
                Assert.Null(fromInvalidW3CIdParent);
            }
        }