fix sampling setters (#327)
* fix sampling setters * Update src/OpenTelemetry/Trace/Configuration/TracerFactory.cs Co-Authored-By: Bruno Garcia <bruno@brunogarcia.com>
This commit is contained in:
parent
a7449e619f
commit
9223282da8
|
|
@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the sampler.
|
||||
/// </summary>
|
||||
public ISampler Sampler { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the maximum number of attributes on span.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
// <copyright file="AlwaysParentSampler.cs" company="OpenTelemetry Authors">
|
||||
// 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.
|
||||
// </copyright>
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace OpenTelemetry.Trace.Sampler.Internal
|
||||
{
|
||||
internal sealed class AlwaysParentSampler : ISampler
|
||||
{
|
||||
internal AlwaysParentSampler()
|
||||
{
|
||||
}
|
||||
|
||||
public string Description => this.ToString();
|
||||
|
||||
/// <inheritdoc />
|
||||
public Decision ShouldSample(SpanContext parentContext, ActivityTraceId traceId, ActivitySpanId spanId, string name, IEnumerable<Link> parentLinks)
|
||||
{
|
||||
if ((parentContext != null) && parentContext.TraceOptions.HasFlag(ActivityTraceFlags.Recorded))
|
||||
{
|
||||
return new Decision(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Decision(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string ToString()
|
||||
{
|
||||
return nameof(AlwaysParentSampler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -30,5 +30,10 @@ namespace OpenTelemetry.Trace.Sampler
|
|||
/// Gets the sampler than never samples.
|
||||
/// </summary>
|
||||
public static ISampler NeverSample { get; } = new Internal.NeverSampleSampler();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the sampler than never samples.
|
||||
/// </summary>
|
||||
public static ISampler AlwaysParentSampler { get; } = new Internal.AlwaysParentSampler();
|
||||
}
|
||||
}
|
||||
|
|
@ -36,6 +36,7 @@ namespace OpenTelemetry.Trace
|
|||
{
|
||||
private static readonly ConditionalWeakTable<Activity, Span> ActivitySpanTable = new ConditionalWeakTable<Activity, Span>();
|
||||
|
||||
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<Link> links,
|
||||
ISampler sampler,
|
||||
TracerConfiguration tracerConfiguration,
|
||||
SpanProcessor spanProcessor,
|
||||
Resource libraryResource)
|
||||
|
|
@ -488,6 +499,7 @@ namespace OpenTelemetry.Trace
|
|||
true,
|
||||
spanKind,
|
||||
null,
|
||||
sampler,
|
||||
tracerConfiguration,
|
||||
spanProcessor,
|
||||
libraryResource)
|
||||
|
|
@ -523,47 +535,14 @@ namespace OpenTelemetry.Trace
|
|||
private static bool MakeSamplingDecision(
|
||||
SpanContext parent,
|
||||
string name,
|
||||
ISampler sampler,
|
||||
IEnumerable<Link> parentLinks,
|
||||
ActivityTraceId traceId,
|
||||
ActivitySpanId spanId,
|
||||
TracerConfiguration tracerConfiguration)
|
||||
{
|
||||
// If users set a specific sampler in the SpanBuilder, use it.
|
||||
if (sampler != null)
|
||||
ISampler sampler)
|
||||
{
|
||||
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<Link> parentLinks)
|
||||
{
|
||||
if (parentLinks != null)
|
||||
{
|
||||
foreach (var parentLink in parentLinks)
|
||||
{
|
||||
if ((parentLink.Context.TraceOptions & ActivityTraceFlags.Recorded) != 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static ActivityAndTracestate FromCurrentParentActivity(string spanName, Activity current)
|
||||
{
|
||||
var activity = new Activity(spanName);
|
||||
|
|
|
|||
|
|
@ -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 <see cref="Tracer"/>.
|
||||
/// </summary>
|
||||
/// <param name="spanProcessor">Span processor.</param>
|
||||
/// <param name="sampler">Sampler to use.</param>
|
||||
/// <param name="tracerConfiguration">Trace configuration.</param>
|
||||
/// <param name="binaryFormat">Binary format context propagator.</param>
|
||||
/// <param name="textFormat">Text format context propagator.</param>
|
||||
/// <param name="libraryResource">Resource describing the instrumentation library.</param>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<ArgumentNullException>(() => new TracerConfiguration(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateTraceParams_NonPositiveMaxNumberOfAttributes()
|
||||
{
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => new TracerConfiguration(Samplers.AlwaysSample, 0 ,1, 1));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => new TracerConfiguration(0 ,1, 1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateTraceParams_NonPositiveMaxNumberOfEvents()
|
||||
{
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => new TracerConfiguration(Samplers.AlwaysSample, 1, 0, 1));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => new TracerConfiguration(1, 0, 1));
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void updateTraceParams_NonPositiveMaxNumberOfLinks()
|
||||
{
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => new TracerConfiguration(Samplers.AlwaysSample, 1, 1, 0));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => 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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -38,7 +39,8 @@ namespace OpenTelemetry.Trace.Export.Test
|
|||
tracer = TracerFactory.Create(b => b
|
||||
.AddProcessorPipeline(p => p
|
||||
.SetExporter(spanExporter)
|
||||
.SetExportingProcessor(e => new SimpleSpanProcessor(e))))
|
||||
.SetExportingProcessor(e => new SimpleSpanProcessor(e)))
|
||||
.SetSampler(Samplers.AlwaysParentSampler))
|
||||
.GetTracer(null);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -52,14 +52,14 @@ namespace OpenTelemetry.Trace.Test
|
|||
public void BadConstructorArgumentsThrow()
|
||||
{
|
||||
var noopProc = new SimpleSpanProcessor(new TestExporter(null));
|
||||
Assert.Throws<ArgumentNullException>(() => new Tracer(null, new TracerConfiguration(), new BinaryFormat(), new TraceContextFormat(), Resource.Empty));
|
||||
Assert.Throws<ArgumentNullException>(() => new Tracer(null, Samplers.AlwaysSample, new TracerConfiguration(), new BinaryFormat(), new TraceContextFormat(), Resource.Empty));
|
||||
|
||||
Assert.Throws<ArgumentNullException>(() => new Tracer(noopProc, null, new BinaryFormat(), new TraceContextFormat(), Resource.Empty));
|
||||
Assert.Throws<ArgumentNullException>(() => new Tracer(noopProc, Samplers.AlwaysSample, null, new BinaryFormat(), new TraceContextFormat(), Resource.Empty));
|
||||
|
||||
Assert.Throws<ArgumentNullException>(() => new Tracer(noopProc, new TracerConfiguration(), null, new TraceContextFormat(), Resource.Empty));
|
||||
Assert.Throws<ArgumentNullException>(() => new Tracer(noopProc, new TracerConfiguration(), new BinaryFormat(), null, Resource.Empty));
|
||||
Assert.Throws<ArgumentNullException>(() => new Tracer(noopProc, Samplers.AlwaysSample, new TracerConfiguration(), null, new TraceContextFormat(), Resource.Empty));
|
||||
Assert.Throws<ArgumentNullException>(() => new Tracer(noopProc, Samplers.AlwaysSample, new TracerConfiguration(), new BinaryFormat(), null, Resource.Empty));
|
||||
|
||||
Assert.Throws<ArgumentNullException>(() => new Tracer(noopProc, new TracerConfiguration(), new BinaryFormat(), new TraceContextFormat(), null));
|
||||
Assert.Throws<ArgumentNullException>(() => 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<Link>();
|
||||
|
|
@ -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<Link>();
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -36,7 +36,6 @@ namespace OpenTelemetry.Trace.Test
|
|||
public void DefaultTraceConfig()
|
||||
{
|
||||
var options = new TracerConfiguration();
|
||||
Assert.IsType<AlwaysSampleSampler>(options.Sampler);
|
||||
Assert.Equal(32, options.MaxNumberOfAttributes);
|
||||
Assert.Equal(128, options.MaxNumberOfEvents);
|
||||
Assert.Equal(32, options.MaxNumberOfLinks);
|
||||
|
|
|
|||
Loading…
Reference in New Issue