From 9223282da831f54df7bce99fa31774681496537d Mon Sep 17 00:00:00 2001 From: Sergey Kanzhelev Date: Thu, 7 Nov 2019 14:26:35 -0800 Subject: [PATCH] fix sampling setters (#327) * fix sampling setters * Update src/OpenTelemetry/Trace/Configuration/TracerFactory.cs Co-Authored-By: Bruno Garcia --- .../Configuration/TracerConfiguration.cs | 15 +-- .../Trace/Configuration/TracerFactory.cs | 5 +- .../Trace/ISampler.cs | 0 .../Sampler/Internal/AlwaysParentSampler.cs | 48 +++++++ .../Sampler/Internal/AlwaysSampleSampler.cs | 0 .../Sampler/Internal/NeverSampleSampler.cs | 0 .../Trace/Sampler/Samplers.cs | 5 + src/OpenTelemetry/Trace/Span.cs | 53 +++----- src/OpenTelemetry/Trace/Tracer.cs | 15 ++- .../Impl/Trace/Config/TraceParamsTest.cs | 16 +-- .../Impl/Trace/Config/TracerBuilderTests.cs | 4 +- .../Trace/Export/SimpleSpanProcessorTests.cs | 6 +- .../Impl/Trace/SpanTest.cs | 118 ------------------ .../Impl/Trace/TracerTest.cs | 35 +++--- .../Impl/Trace/TracingTest.cs | 1 - 15 files changed, 113 insertions(+), 208 deletions(-) rename src/{OpenTelemetry.Api => OpenTelemetry}/Trace/ISampler.cs (100%) create mode 100644 src/OpenTelemetry/Trace/Sampler/Internal/AlwaysParentSampler.cs rename src/{OpenTelemetry.Api => OpenTelemetry}/Trace/Sampler/Internal/AlwaysSampleSampler.cs (100%) rename src/{OpenTelemetry.Api => OpenTelemetry}/Trace/Sampler/Internal/NeverSampleSampler.cs (100%) rename src/{OpenTelemetry.Api => OpenTelemetry}/Trace/Sampler/Samplers.cs (86%) diff --git a/src/OpenTelemetry/Trace/Configuration/TracerConfiguration.cs b/src/OpenTelemetry/Trace/Configuration/TracerConfiguration.cs index 7976a71dc..6dbd41f00 100644 --- a/src/OpenTelemetry/Trace/Configuration/TracerConfiguration.cs +++ b/src/OpenTelemetry/Trace/Configuration/TracerConfiguration.cs @@ -28,16 +28,11 @@ namespace OpenTelemetry.Trace.Configuration private const int DefaultSpanMaxNumLinks = 32; public TracerConfiguration() - : this(Samplers.AlwaysSample, DefaultSpanMaxNumAttributes, DefaultSpanMaxNumEvents, DefaultSpanMaxNumLinks) + : this(DefaultSpanMaxNumAttributes, DefaultSpanMaxNumEvents, DefaultSpanMaxNumLinks) { } - public TracerConfiguration(ISampler sampler) - : this(sampler, DefaultSpanMaxNumAttributes, DefaultSpanMaxNumEvents, DefaultSpanMaxNumLinks) - { - } - - public TracerConfiguration(ISampler sampler, int maxNumberOfAttributes, int maxNumberOfEvents, int maxNumberOfLinks) + public TracerConfiguration(int maxNumberOfAttributes, int maxNumberOfEvents, int maxNumberOfLinks) { if (maxNumberOfAttributes <= 0) { @@ -54,17 +49,11 @@ namespace OpenTelemetry.Trace.Configuration throw new ArgumentOutOfRangeException(nameof(maxNumberOfLinks)); } - this.Sampler = sampler ?? throw new ArgumentNullException(nameof(sampler)); this.MaxNumberOfAttributes = maxNumberOfAttributes; this.MaxNumberOfEvents = maxNumberOfEvents; this.MaxNumberOfLinks = maxNumberOfLinks; } - /// - /// Gets the sampler. - /// - public ISampler Sampler { get; } - /// /// Gets the maximum number of attributes on span. /// diff --git a/src/OpenTelemetry/Trace/Configuration/TracerFactory.cs b/src/OpenTelemetry/Trace/Configuration/TracerFactory.cs index 8cda79f9b..fbb0f8237 100644 --- a/src/OpenTelemetry/Trace/Configuration/TracerFactory.cs +++ b/src/OpenTelemetry/Trace/Configuration/TracerFactory.cs @@ -43,9 +43,8 @@ namespace OpenTelemetry.Trace.Configuration { this.sampler = builder.Sampler ?? Samplers.AlwaysSample; - // TODO separate sampler from options this.configurationOptions = - builder.TracerConfigurationOptions ?? new TracerConfiguration(this.sampler); + builder.TracerConfigurationOptions ?? new TracerConfiguration(); if (builder.ProcessingPipelines == null || !builder.ProcessingPipelines.Any()) { @@ -77,6 +76,7 @@ namespace OpenTelemetry.Trace.Configuration this.defaultTracer = new Tracer( this.spanProcessor, + this.sampler, this.configurationOptions, this.binaryFormat, this.textFormat, @@ -124,6 +124,7 @@ namespace OpenTelemetry.Trace.Configuration { tracer = this.defaultTracer = new Tracer( this.spanProcessor, + this.sampler, this.configurationOptions, this.binaryFormat, this.textFormat, diff --git a/src/OpenTelemetry.Api/Trace/ISampler.cs b/src/OpenTelemetry/Trace/ISampler.cs similarity index 100% rename from src/OpenTelemetry.Api/Trace/ISampler.cs rename to src/OpenTelemetry/Trace/ISampler.cs diff --git a/src/OpenTelemetry/Trace/Sampler/Internal/AlwaysParentSampler.cs b/src/OpenTelemetry/Trace/Sampler/Internal/AlwaysParentSampler.cs new file mode 100644 index 000000000..d946b7a58 --- /dev/null +++ b/src/OpenTelemetry/Trace/Sampler/Internal/AlwaysParentSampler.cs @@ -0,0 +1,48 @@ +// +// Copyright 2018, OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +using System.Collections.Generic; +using System.Diagnostics; + +namespace OpenTelemetry.Trace.Sampler.Internal +{ + internal sealed class AlwaysParentSampler : ISampler + { + internal AlwaysParentSampler() + { + } + + public string Description => this.ToString(); + + /// + public Decision ShouldSample(SpanContext parentContext, ActivityTraceId traceId, ActivitySpanId spanId, string name, IEnumerable parentLinks) + { + if ((parentContext != null) && parentContext.TraceOptions.HasFlag(ActivityTraceFlags.Recorded)) + { + return new Decision(true); + } + else + { + return new Decision(false); + } + } + + /// + public override string ToString() + { + return nameof(AlwaysParentSampler); + } + } +} diff --git a/src/OpenTelemetry.Api/Trace/Sampler/Internal/AlwaysSampleSampler.cs b/src/OpenTelemetry/Trace/Sampler/Internal/AlwaysSampleSampler.cs similarity index 100% rename from src/OpenTelemetry.Api/Trace/Sampler/Internal/AlwaysSampleSampler.cs rename to src/OpenTelemetry/Trace/Sampler/Internal/AlwaysSampleSampler.cs diff --git a/src/OpenTelemetry.Api/Trace/Sampler/Internal/NeverSampleSampler.cs b/src/OpenTelemetry/Trace/Sampler/Internal/NeverSampleSampler.cs similarity index 100% rename from src/OpenTelemetry.Api/Trace/Sampler/Internal/NeverSampleSampler.cs rename to src/OpenTelemetry/Trace/Sampler/Internal/NeverSampleSampler.cs diff --git a/src/OpenTelemetry.Api/Trace/Sampler/Samplers.cs b/src/OpenTelemetry/Trace/Sampler/Samplers.cs similarity index 86% rename from src/OpenTelemetry.Api/Trace/Sampler/Samplers.cs rename to src/OpenTelemetry/Trace/Sampler/Samplers.cs index ea1275b40..45f5f44ec 100644 --- a/src/OpenTelemetry.Api/Trace/Sampler/Samplers.cs +++ b/src/OpenTelemetry/Trace/Sampler/Samplers.cs @@ -30,5 +30,10 @@ namespace OpenTelemetry.Trace.Sampler /// Gets the sampler than never samples. /// public static ISampler NeverSample { get; } = new Internal.NeverSampleSampler(); + + /// + /// Gets the sampler than never samples. + /// + public static ISampler AlwaysParentSampler { get; } = new Internal.AlwaysParentSampler(); } } diff --git a/src/OpenTelemetry/Trace/Span.cs b/src/OpenTelemetry/Trace/Span.cs index c8bf22e40..4f3b1b500 100644 --- a/src/OpenTelemetry/Trace/Span.cs +++ b/src/OpenTelemetry/Trace/Span.cs @@ -36,6 +36,7 @@ namespace OpenTelemetry.Trace { private static readonly ConditionalWeakTable ActivitySpanTable = new ConditionalWeakTable(); + private readonly ISampler sampler; private readonly TracerConfiguration tracerConfiguration; private readonly SpanProcessor spanProcessor; private readonly object lck = new object(); @@ -58,6 +59,7 @@ namespace OpenTelemetry.Trace bool createdFromActivity, SpanKind spanKind, SpanCreationOptions spanCreationOptions, + ISampler sampler, TracerConfiguration tracerConfiguration, SpanProcessor spanProcessor, Resource libraryResource) @@ -77,6 +79,7 @@ namespace OpenTelemetry.Trace this.startTimestamp = PreciseTimestamp.GetUtcNow(); } + this.sampler = sampler; this.tracerConfiguration = tracerConfiguration; this.spanProcessor = spanProcessor; this.Kind = spanKind; @@ -87,11 +90,10 @@ namespace OpenTelemetry.Trace this.IsRecording = MakeSamplingDecision( parentSpanContext, name, - null, links, // we'll enumerate again, but double enumeration over small collection is cheaper than allocation this.Activity.TraceId, this.Activity.SpanId, - this.tracerConfiguration); + this.sampler); this.Activity.ActivityTraceFlags = this.IsRecording @@ -383,6 +385,7 @@ namespace OpenTelemetry.Trace ISpan parentSpan, SpanKind spanKind, SpanCreationOptions spanCreationOptions, + ISampler sampler, TracerConfiguration tracerConfiguration, SpanProcessor spanProcessor, Resource libraryResource) @@ -396,6 +399,7 @@ namespace OpenTelemetry.Trace false, spanKind, spanCreationOptions, + sampler, tracerConfiguration, spanProcessor, libraryResource); @@ -411,6 +415,7 @@ namespace OpenTelemetry.Trace false, spanKind, spanCreationOptions, + sampler, tracerConfiguration, spanProcessor, libraryResource); @@ -426,6 +431,7 @@ namespace OpenTelemetry.Trace false, spanKind, spanCreationOptions, + sampler, tracerConfiguration, spanProcessor, libraryResource); @@ -436,6 +442,7 @@ namespace OpenTelemetry.Trace SpanContext parentContext, SpanKind spanKind, SpanCreationOptions spanCreationOptions, + ISampler sampler, TracerConfiguration tracerConfiguration, SpanProcessor spanProcessor, Resource libraryResource) @@ -447,6 +454,7 @@ namespace OpenTelemetry.Trace false, spanKind, spanCreationOptions, + sampler, tracerConfiguration, spanProcessor, libraryResource); @@ -456,6 +464,7 @@ namespace OpenTelemetry.Trace string name, SpanKind spanKind, SpanCreationOptions spanCreationOptions, + ISampler sampler, TracerConfiguration tracerConfiguration, SpanProcessor spanProcessor, Resource libraryResource) @@ -467,6 +476,7 @@ namespace OpenTelemetry.Trace false, spanKind, spanCreationOptions, + sampler, tracerConfiguration, spanProcessor, libraryResource); @@ -477,6 +487,7 @@ namespace OpenTelemetry.Trace Activity activity, SpanKind spanKind, IEnumerable links, + ISampler sampler, TracerConfiguration tracerConfiguration, SpanProcessor spanProcessor, Resource libraryResource) @@ -488,6 +499,7 @@ namespace OpenTelemetry.Trace true, spanKind, null, + sampler, tracerConfiguration, spanProcessor, libraryResource) @@ -523,45 +535,12 @@ namespace OpenTelemetry.Trace private static bool MakeSamplingDecision( SpanContext parent, string name, - ISampler sampler, IEnumerable parentLinks, ActivityTraceId traceId, ActivitySpanId spanId, - TracerConfiguration tracerConfiguration) + ISampler sampler) { - // If users set a specific sampler in the SpanBuilder, use it. - if (sampler != null) - { - return sampler.ShouldSample(parent, traceId, spanId, name, parentLinks).IsSampled; - } - - // Use the default sampler if this is a root Span or this is an entry point Span (has remote - // parent). - if (parent == null || !parent.IsValid) - { - return tracerConfiguration - .Sampler - .ShouldSample(parent, traceId, spanId, name, parentLinks).IsSampled; - } - - // Parent is always different than null because otherwise we use the default sampler. - return (parent.TraceOptions & ActivityTraceFlags.Recorded) != 0 || IsAnyParentLinkSampled(parentLinks); - } - - private static bool IsAnyParentLinkSampled(IEnumerable parentLinks) - { - if (parentLinks != null) - { - foreach (var parentLink in parentLinks) - { - if ((parentLink.Context.TraceOptions & ActivityTraceFlags.Recorded) != 0) - { - return true; - } - } - } - - return false; + return sampler.ShouldSample(parent, traceId, spanId, name, parentLinks).IsSampled; } private static ActivityAndTracestate FromCurrentParentActivity(string spanName, Activity current) diff --git a/src/OpenTelemetry/Trace/Tracer.cs b/src/OpenTelemetry/Trace/Tracer.cs index 97777ea03..76f830a44 100644 --- a/src/OpenTelemetry/Trace/Tracer.cs +++ b/src/OpenTelemetry/Trace/Tracer.cs @@ -29,6 +29,7 @@ namespace OpenTelemetry.Trace { private readonly SpanProcessor spanProcessor; private readonly TracerConfiguration tracerConfiguration; + private readonly ISampler sampler; static Tracer() { @@ -40,17 +41,19 @@ namespace OpenTelemetry.Trace /// Creates an instance of . /// /// Span processor. + /// Sampler to use. /// Trace configuration. /// Binary format context propagator. /// Text format context propagator. /// Resource describing the instrumentation library. - internal Tracer(SpanProcessor spanProcessor, TracerConfiguration tracerConfiguration, IBinaryFormat binaryFormat, ITextFormat textFormat, Resource libraryResource) + internal Tracer(SpanProcessor spanProcessor, ISampler sampler, TracerConfiguration tracerConfiguration, IBinaryFormat binaryFormat, ITextFormat textFormat, Resource libraryResource) { this.spanProcessor = spanProcessor ?? throw new ArgumentNullException(nameof(spanProcessor)); this.tracerConfiguration = tracerConfiguration ?? throw new ArgumentNullException(nameof(tracerConfiguration)); this.BinaryFormat = binaryFormat ?? throw new ArgumentNullException(nameof(binaryFormat)); this.TextFormat = textFormat ?? throw new ArgumentNullException(nameof(textFormat)); this.LibraryResource = libraryResource ?? throw new ArgumentNullException(nameof(libraryResource)); + this.sampler = sampler ?? throw new ArgumentNullException(nameof(sampler)); } public Resource LibraryResource { get; } @@ -87,7 +90,7 @@ namespace OpenTelemetry.Trace throw new ArgumentNullException(nameof(operationName)); } - return Span.CreateRoot(operationName, kind, options, this.tracerConfiguration, this.spanProcessor, this.LibraryResource); + return Span.CreateRoot(operationName, kind, options, this.sampler, this.tracerConfiguration, this.spanProcessor, this.LibraryResource); } /// @@ -103,7 +106,7 @@ namespace OpenTelemetry.Trace parent = this.CurrentSpan; } - return Span.CreateFromParentSpan(operationName, parent, kind, options, this.tracerConfiguration, + return Span.CreateFromParentSpan(operationName, parent, kind, options, this.sampler, this.tracerConfiguration, this.spanProcessor, this.LibraryResource); } @@ -117,11 +120,11 @@ namespace OpenTelemetry.Trace if (parent != null) { - return Span.CreateFromParentContext(operationName, parent, kind, options, this.tracerConfiguration, + return Span.CreateFromParentContext(operationName, parent, kind, options, this.sampler, this.tracerConfiguration, this.spanProcessor, this.LibraryResource); } - return Span.CreateRoot(operationName, kind, options, this.tracerConfiguration, + return Span.CreateRoot(operationName, kind, options, this.sampler, this.tracerConfiguration, this.spanProcessor, this.LibraryResource); } @@ -149,7 +152,7 @@ namespace OpenTelemetry.Trace "Current Activity is not running: it has not been started or has been stopped"); } - return Span.CreateFromActivity(operationName, activity, kind, links, this.tracerConfiguration, this.spanProcessor, this.LibraryResource); + return Span.CreateFromActivity(operationName, activity, kind, links, this.sampler, this.tracerConfiguration, this.spanProcessor, this.LibraryResource); } } } diff --git a/test/OpenTelemetry.Tests/Impl/Trace/Config/TraceParamsTest.cs b/test/OpenTelemetry.Tests/Impl/Trace/Config/TraceParamsTest.cs index 16d5b38c9..f73b9cb35 100644 --- a/test/OpenTelemetry.Tests/Impl/Trace/Config/TraceParamsTest.cs +++ b/test/OpenTelemetry.Tests/Impl/Trace/Config/TraceParamsTest.cs @@ -26,43 +26,35 @@ namespace OpenTelemetry.Trace.Config.Test public void DefaultTraceConfig() { var config = new TracerConfiguration(); - Assert.Equal(Samplers.AlwaysSample, config.Sampler); Assert.Equal(32, config.MaxNumberOfAttributes); Assert.Equal(128, config.MaxNumberOfEvents); Assert.Equal(32, config.MaxNumberOfLinks); } - [Fact] - public void UpdateTraceParams_NullSampler() - { - Assert.Throws(() => new TracerConfiguration(null)); - } - [Fact] public void UpdateTraceParams_NonPositiveMaxNumberOfAttributes() { - Assert.Throws(() => new TracerConfiguration(Samplers.AlwaysSample, 0 ,1, 1)); + Assert.Throws(() => new TracerConfiguration(0 ,1, 1)); } [Fact] public void UpdateTraceParams_NonPositiveMaxNumberOfEvents() { - Assert.Throws(() => new TracerConfiguration(Samplers.AlwaysSample, 1, 0, 1)); + Assert.Throws(() => new TracerConfiguration(1, 0, 1)); } [Fact] public void updateTraceParams_NonPositiveMaxNumberOfLinks() { - Assert.Throws(() => new TracerConfiguration(Samplers.AlwaysSample, 1, 1, 0)); + Assert.Throws(() => new TracerConfiguration(1, 1, 0)); } [Fact] public void UpdateTraceParams_All() { - var traceParams = new TracerConfiguration(Samplers.NeverSample, 8, 9, 11); + var traceParams = new TracerConfiguration(8, 9, 11); - Assert.Equal(Samplers.NeverSample, traceParams.Sampler); Assert.Equal(8, traceParams.MaxNumberOfAttributes); Assert.Equal(9, traceParams.MaxNumberOfEvents); Assert.Equal(11, traceParams.MaxNumberOfLinks); diff --git a/test/OpenTelemetry.Tests/Impl/Trace/Config/TracerBuilderTests.cs b/test/OpenTelemetry.Tests/Impl/Trace/Config/TracerBuilderTests.cs index 50a62351a..62b2d345d 100644 --- a/test/OpenTelemetry.Tests/Impl/Trace/Config/TracerBuilderTests.cs +++ b/test/OpenTelemetry.Tests/Impl/Trace/Config/TracerBuilderTests.cs @@ -62,7 +62,7 @@ namespace OpenTelemetry.Tests.Impl.Trace var sampler = ProbabilitySampler.Create(0.1); var exporter = new TestExporter(_ => { }); - var options = new TracerConfiguration(sampler, 1, 1, 1); + var options = new TracerConfiguration(1, 1, 1); var binaryFormat = new BinaryFormat(); var textFormat = new TraceContextFormat(); @@ -104,7 +104,7 @@ namespace OpenTelemetry.Tests.Impl.Trace Assert.Equal("semver:" + typeof(TestCollector).Assembly.GetName().Version, collectorFactory.Version); Assert.NotNull(collectorFactory.Factory); - collectorFactory.Factory(new Tracer(new SimpleSpanProcessor(exporter), options, binaryFormat, textFormat, + collectorFactory.Factory(new Tracer(new SimpleSpanProcessor(exporter), Samplers.AlwaysSample, options, binaryFormat, textFormat, Resource.Empty)); Assert.True(collectorFactoryCalled); diff --git a/test/OpenTelemetry.Tests/Impl/Trace/Export/SimpleSpanProcessorTests.cs b/test/OpenTelemetry.Tests/Impl/Trace/Export/SimpleSpanProcessorTests.cs index d6830f056..23c30cf00 100644 --- a/test/OpenTelemetry.Tests/Impl/Trace/Export/SimpleSpanProcessorTests.cs +++ b/test/OpenTelemetry.Tests/Impl/Trace/Export/SimpleSpanProcessorTests.cs @@ -20,6 +20,7 @@ using System.Threading; using System.Threading.Tasks; using OpenTelemetry.Testing.Export; using OpenTelemetry.Trace.Configuration; +using OpenTelemetry.Trace.Sampler; using Xunit; namespace OpenTelemetry.Trace.Export.Test @@ -36,9 +37,10 @@ namespace OpenTelemetry.Trace.Export.Test { spanExporter = new TestExporter(null); tracer = TracerFactory.Create(b => b - .AddProcessorPipeline(p => p + .AddProcessorPipeline(p => p .SetExporter(spanExporter) - .SetExportingProcessor(e => new SimpleSpanProcessor(e)))) + .SetExportingProcessor(e => new SimpleSpanProcessor(e))) + .SetSampler(Samplers.AlwaysParentSampler)) .GetTracer(null); } diff --git a/test/OpenTelemetry.Tests/Impl/Trace/SpanTest.cs b/test/OpenTelemetry.Tests/Impl/Trace/SpanTest.cs index b7e0e46ec..38776965c 100644 --- a/test/OpenTelemetry.Tests/Impl/Trace/SpanTest.cs +++ b/test/OpenTelemetry.Tests/Impl/Trace/SpanTest.cs @@ -99,30 +99,6 @@ namespace OpenTelemetry.Trace.Test } } - [Fact] - public void StartSpanFrom_NotRecorded_ParentSpan() - { - var tracer = tracerFactory.GetTracer(null); - - var grandParentContext = new SpanContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None); - var parentSpan = (Span)tracer.StartSpan(SpanName, grandParentContext); - - var startTimestamp = PreciseTimestamp.GetUtcNow(); - var span = (Span)tracer.StartSpan(SpanName, parentSpan); - - Assert.True(span.Context.IsValid); - Assert.Equal(parentSpan.Context.TraceId, span.Context.TraceId); - Assert.Equal(span.Activity.SpanId, span.Context.SpanId); - Assert.Equal(parentSpan.Context.SpanId, span.ParentSpanId); - Assert.Equal(parentSpan.Context.TraceOptions, span.Context.TraceOptions); - Assert.Empty(span.Context.Tracestate); - - Assert.False(span.IsRecording); - Assert.Equal(SpanKind.Internal, span.Kind); - AssertApproxSameTimestamp(startTimestamp, span.StartTimestamp); - Assert.Empty(span.Links); - } - [Fact] public void StartSpanFrom_Recorded_ParentSpan_Kind() { @@ -358,45 +334,6 @@ namespace OpenTelemetry.Trace.Test } } - [Fact] - public void StartSpanFrom_NotRecorded_ParentContext() - { - var tracer = tracerFactory.GetTracer(null); - - var parentContext = new SpanContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None); - - var startTimestamp = PreciseTimestamp.GetUtcNow(); - var span = (Span)tracer.StartSpan(SpanName, parentContext); - - Assert.True(span.Context.IsValid); - Assert.Equal(parentContext.TraceId, span.Context.TraceId); - Assert.Equal(span.Activity.SpanId, span.Context.SpanId); - Assert.Equal(parentContext.SpanId, span.ParentSpanId); - Assert.Equal(parentContext.TraceOptions, span.Context.TraceOptions); - Assert.Empty(span.Context.Tracestate); - - Assert.False(span.IsRecording); - Assert.Equal(SpanKind.Internal, span.Kind); - AssertApproxSameTimestamp(startTimestamp, span.StartTimestamp); - Assert.Empty(span.Links); - } - - [Fact] - public void StartSpanFrom_Recorded_ParentContext_Kind() - { - var tracer = tracerFactory.GetTracer(null); - - var parentContext = new SpanContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.Recorded); - - var startTimestamp = PreciseTimestamp.GetUtcNow(); - var span = (Span)tracer.StartSpan(SpanName, parentContext, SpanKind.Client); - - Assert.True(span.IsRecording); - Assert.Equal(SpanKind.Client, span.Kind); - AssertApproxSameTimestamp(startTimestamp, span.StartTimestamp); - Assert.Empty(span.Links); - } - [Fact] public void StartSpanFrom_Recorded_ParentContext_Kind_Timestamp() { @@ -618,61 +555,6 @@ namespace OpenTelemetry.Trace.Test parentActivity.Stop(); } - [Fact] - public void StartSpanFrom_NotRecorded_ImplicitParentActivity() - { - var tracer = tracerFactory.GetTracer(null); - - var parentActivity = new Activity("foo").SetIdFormat(ActivityIdFormat.W3C).Start(); - parentActivity.TraceStateString = "k1=v1,k2=v2"; - parentActivity.ActivityTraceFlags = ActivityTraceFlags.None; - - var startTimestamp = PreciseTimestamp.GetUtcNow(); - var span = (Span)tracer.StartSpan(SpanName); - - Assert.True(span.Context.IsValid); - Assert.Equal(parentActivity.TraceId, span.Context.TraceId); - Assert.Equal(span.Activity.SpanId, span.Context.SpanId); - Assert.Equal(parentActivity.SpanId, span.ParentSpanId); - Assert.Equal(parentActivity.ActivityTraceFlags, span.Context.TraceOptions); - Assert.Equal(2, span.Context.Tracestate.Count()); - Assert.Contains(span.Context.Tracestate, pair => pair.Key == "k1" && pair.Value == "v1"); - Assert.Contains(span.Context.Tracestate, pair => pair.Key == "k2" && pair.Value == "v2"); - - Assert.False(span.IsRecording); - Assert.Equal(SpanKind.Internal, span.Kind); - AssertApproxSameTimestamp(startTimestamp, span.StartTimestamp); - Assert.Empty(span.Links); - - parentActivity.Stop(); - } - - [Fact] - public void StartSpanFrom_NotRecorded_ImplicitParentSpan() - { - var tracer = tracerFactory.GetTracer(null); - var grandParentContext = new SpanContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None); - using (tracer.WithSpan(tracer.StartSpan(SpanName, grandParentContext))) - { - var parentSpan = tracer.CurrentSpan; - - var startTimestamp = PreciseTimestamp.GetUtcNow(); - var span = (Span)tracer.StartSpan(SpanName); - - Assert.True(span.Context.IsValid); - Assert.Equal(parentSpan.Context.TraceId, span.Context.TraceId); - Assert.Equal(span.Activity.SpanId, span.Context.SpanId); - Assert.Equal(parentSpan.Context.SpanId, span.ParentSpanId); - Assert.Equal(parentSpan.Context.TraceOptions, span.Context.TraceOptions); - Assert.Empty(span.Context.Tracestate); - - Assert.False(span.IsRecording); - Assert.Equal(SpanKind.Internal, span.Kind); - AssertApproxSameTimestamp(startTimestamp, span.StartTimestamp); - Assert.Empty(span.Links); - } - } - [Fact] public void StartSpanFrom_Recorded_ImplicitParentSpan_Kind() { diff --git a/test/OpenTelemetry.Tests/Impl/Trace/TracerTest.cs b/test/OpenTelemetry.Tests/Impl/Trace/TracerTest.cs index e87c7d8bc..799b8e850 100644 --- a/test/OpenTelemetry.Tests/Impl/Trace/TracerTest.cs +++ b/test/OpenTelemetry.Tests/Impl/Trace/TracerTest.cs @@ -52,14 +52,14 @@ namespace OpenTelemetry.Trace.Test public void BadConstructorArgumentsThrow() { var noopProc = new SimpleSpanProcessor(new TestExporter(null)); - Assert.Throws(() => new Tracer(null, new TracerConfiguration(), new BinaryFormat(), new TraceContextFormat(), Resource.Empty)); + Assert.Throws(() => new Tracer(null, Samplers.AlwaysSample, new TracerConfiguration(), new BinaryFormat(), new TraceContextFormat(), Resource.Empty)); - Assert.Throws(() => new Tracer(noopProc, null, new BinaryFormat(), new TraceContextFormat(), Resource.Empty)); + Assert.Throws(() => new Tracer(noopProc, Samplers.AlwaysSample, null, new BinaryFormat(), new TraceContextFormat(), Resource.Empty)); - Assert.Throws(() => new Tracer(noopProc, new TracerConfiguration(), null, new TraceContextFormat(), Resource.Empty)); - Assert.Throws(() => new Tracer(noopProc, new TracerConfiguration(), new BinaryFormat(), null, Resource.Empty)); + Assert.Throws(() => new Tracer(noopProc, Samplers.AlwaysSample, new TracerConfiguration(), null, new TraceContextFormat(), Resource.Empty)); + Assert.Throws(() => new Tracer(noopProc, Samplers.AlwaysSample, new TracerConfiguration(), new BinaryFormat(), null, Resource.Empty)); - Assert.Throws(() => new Tracer(noopProc, new TracerConfiguration(), new BinaryFormat(), new TraceContextFormat(), null)); + Assert.Throws(() => new Tracer(noopProc, Samplers.AlwaysSample, new TracerConfiguration(), new BinaryFormat(), new TraceContextFormat(), null)); } [Fact] @@ -159,10 +159,11 @@ namespace OpenTelemetry.Trace.Test public void DroppingAndAddingAttributes() { var maxNumberOfAttributes = 8; - var traceConfig = new TracerConfiguration(Samplers.AlwaysSample, maxNumberOfAttributes, 128, 32); + var traceConfig = new TracerConfiguration(maxNumberOfAttributes, 128, 32); var tracer = TracerFactory.Create(b => b .AddProcessorPipeline(p => p.AddProcessor(n => spanProcessor)) - .SetTracerOptions(traceConfig)) + .SetTracerOptions(traceConfig) + .SetSampler(Samplers.AlwaysSample)) .GetTracer(null); var span = (Span)tracer.StartRootSpan(SpanName); @@ -210,10 +211,11 @@ namespace OpenTelemetry.Trace.Test public async Task DroppingEvents() { var maxNumberOfEvents = 8; - var traceConfig = new TracerConfiguration(Samplers.AlwaysSample, 32, maxNumberOfEvents, 32); + var traceConfig = new TracerConfiguration(32, maxNumberOfEvents, 32); var tracer = TracerFactory.Create(b => b .AddProcessorPipeline(p => p.AddProcessor(n => spanProcessor)) - .SetTracerOptions(traceConfig)) + .SetTracerOptions(traceConfig) + .SetSampler(Samplers.AlwaysSample)) .GetTracer(null); var span = (Span)tracer.StartRootSpan(SpanName); @@ -248,10 +250,11 @@ namespace OpenTelemetry.Trace.Test ActivityTraceFlags.None); var maxNumberOfLinks = 8; - var traceConfig = new TracerConfiguration(Samplers.AlwaysSample, 32, 128, maxNumberOfLinks); + var traceConfig = new TracerConfiguration(32, 128, maxNumberOfLinks); var tracer = TracerFactory.Create(b => b .AddProcessorPipeline(p => p.AddProcessor(n => spanProcessor)) - .SetTracerOptions(traceConfig)) + .SetTracerOptions(traceConfig) + .SetSampler(Samplers.AlwaysSample)) .GetTracer(null); var overflowedLinks = new List(); @@ -289,10 +292,11 @@ namespace OpenTelemetry.Trace.Test ActivityTraceFlags.None); var maxNumberOfLinks = 8; - var traceConfig = new TracerConfiguration(Samplers.AlwaysSample, 32, 128, maxNumberOfLinks); + var traceConfig = new TracerConfiguration(32, 128, maxNumberOfLinks); var tracer = TracerFactory.Create(b => b .AddProcessorPipeline(p => p.AddProcessor(n => spanProcessor)) - .SetTracerOptions(traceConfig)) + .SetTracerOptions(traceConfig) + .SetSampler(Samplers.AlwaysSample)) .GetTracer(null); var overflowedLinks = new List(); @@ -327,10 +331,11 @@ namespace OpenTelemetry.Trace.Test public void DroppingAttributes() { var maxNumberOfAttributes = 8; - var traceConfig = new TracerConfiguration(Samplers.AlwaysSample, maxNumberOfAttributes, 128, 32); + var traceConfig = new TracerConfiguration(maxNumberOfAttributes, 128, 32); var tracer = TracerFactory.Create(b => b .AddProcessorPipeline(p => p.AddProcessor(_ => this.spanProcessor)) - .SetTracerOptions(traceConfig)) + .SetTracerOptions(traceConfig) + .SetSampler(Samplers.AlwaysSample)) .GetTracer(null); var span = (Span)tracer.StartRootSpan(SpanName); diff --git a/test/OpenTelemetry.Tests/Impl/Trace/TracingTest.cs b/test/OpenTelemetry.Tests/Impl/Trace/TracingTest.cs index b47c0087a..571d748fe 100644 --- a/test/OpenTelemetry.Tests/Impl/Trace/TracingTest.cs +++ b/test/OpenTelemetry.Tests/Impl/Trace/TracingTest.cs @@ -36,7 +36,6 @@ namespace OpenTelemetry.Trace.Test public void DefaultTraceConfig() { var options = new TracerConfiguration(); - Assert.IsType(options.Sampler); Assert.Equal(32, options.MaxNumberOfAttributes); Assert.Equal(128, options.MaxNumberOfEvents); Assert.Equal(32, options.MaxNumberOfLinks);