Example #1
0
        public void BuildSamplingParametersHandlesCurrentActivity()
        {
            using var activitySource = new ActivitySource(nameof(this.BuildSamplingParametersHandlesCurrentActivity));

            var testSampler = new TestSampler {
                DesiredSamplingResult = new SamplingResult(true)
            };

            using var listener = new ActivityListener
                  {
                      ShouldListenTo = _ => true,
                      GetRequestedDataUsingContext = (ref ActivityCreationOptions <ActivityContext> options) =>
                                                     Sdk.ComputeActivityDataRequest(options, testSampler),
                  };

            ActivitySource.AddActivityListener(listener);

            using (var root = activitySource.StartActivity("root"))
            {
                Assert.Equal(default(ActivitySpanId), root.ParentSpanId);

                // This enforces the current behavior that the traceId passed to the sampler for the
                // root span/activity is not the traceId actually used.
                Assert.NotEqual(root.TraceId, testSampler.LatestSamplingParameters.TraceId);
            }

            using (var parent = activitySource.StartActivity("parent", ActivityKind.Client))
            {
                // This enforces the current behavior that the traceId passed to the sampler for the
                // root span/activity is not the traceId actually used.
                Assert.NotEqual(parent.TraceId, testSampler.LatestSamplingParameters.TraceId);
                using (var child = activitySource.StartActivity("child"))
                {
                    Assert.Equal(parent.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(customContext.TraceId, fromCustomContext.TraceId);
                Assert.Equal(customContext.SpanId, fromCustomContext.ParentSpanId);
                Assert.NotEqual(customContext.SpanId, fromCustomContext.SpanId);
            }

            // Preserve traceId in case span is propagated but not recorded (sampled per OpenTelemetry parlance) and
            // no data is requested for children spans.
            testSampler.DesiredSamplingResult = new SamplingResult(false);
            using (var root = activitySource.StartActivity("root"))
            {
                Assert.Equal(default(ActivitySpanId), root.ParentSpanId);

                using (var child = activitySource.StartActivity("child"))
                {
                    Assert.Null(child);
                    Assert.Equal(root.TraceId, testSampler.LatestSamplingParameters.TraceId);
                    Assert.Same(Activity.Current, root);
                }
            }
        }