Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renaming ProbabilitySampler to TraceIdRatioBasedSampler #1174

Merged
merged 4 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
[#1135](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1135))
* Renamed `ParentOrElseSampler` to `ParentBasedSampler`
([#1173](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1173))
* Renamed `ProbabilitySampler` to `TraceIdRatioBasedSampler`
([#1174](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1174))

## 0.4.0-beta.2

Expand Down
4 changes: 2 additions & 2 deletions src/OpenTelemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ reducing the number of samples of traces collected and sent to the backend. If
no sampler is explicitly specified, the default is to use
[AlwaysOnSampler](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/sdk.md#alwayson).
The following example shows how to change it to
[ProbabilitySampler](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/sdk.md#probability)
[TraceIdRatioBasedSampler](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/sdk.md#traceidratiobased)
with sampling probability of 25%.

```csharp
Expand All @@ -67,7 +67,7 @@ using OpenTelemetry.Trace;

using var otel = Sdk.CreateTracerProvider(b => b
.AddActivitySource("MyCompany.MyProduct.MyLibrary")
.SetSampler(new ProbabilitySampler(0.25))
.SetSampler(new TraceIdRatioBasedSampler(0.25))
.UseConsoleExporter());
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="ProbabilitySampler.cs" company="OpenTelemetry Authors">
// <copyright file="TraceIdRatioBasedSampler.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -21,18 +21,19 @@ namespace OpenTelemetry.Trace
/// <summary>
/// Samples traces according to the specified probability.
/// </summary>
public sealed class ProbabilitySampler : Sampler
public sealed class TraceIdRatioBasedSampler
: Sampler
{
private readonly long idUpperBound;
private readonly double probability;

/// <summary>
/// Initializes a new instance of the <see cref="ProbabilitySampler"/> class.
/// Initializes a new instance of the <see cref="TraceIdRatioBasedSampler"/> class.
/// </summary>
/// <param name="probability">The desired probability of sampling. This must be between 0.0 and 1.0.
/// Higher the value, higher is the probability of a given Activity to be sampled in.
/// </param>
public ProbabilitySampler(double probability)
public TraceIdRatioBasedSampler(double probability)
{
if (probability < 0.0 || probability > 1.0)
{
Expand All @@ -41,8 +42,8 @@ public ProbabilitySampler(double probability)

this.probability = probability;

// The expected description is like ProbabilitySampler{0.000100}
this.Description = "ProbabilitySampler{" + this.probability.ToString("F6", CultureInfo.InvariantCulture) + "}";
// The expected description is like TraceIdRatioBasedSampler{0.000100}
this.Description = "TraceIdRatioBasedSampler{" + this.probability.ToString("F6", CultureInfo.InvariantCulture) + "}";

// Special case the limits, to avoid any possible issues with lack of precision across
// double/long boundaries. For probability == 0.0, we use Long.MIN_VALUE as this guarantees
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="ProbabilitySamplerTest.cs" company="OpenTelemetry Authors">
// <copyright file="TraceIdRatioBasedSamplerTest.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -19,29 +19,29 @@

namespace OpenTelemetry.Trace.Tests
{
public class ProbabilitySamplerTest
public class TraceIdRatioBasedSamplerTest
{
private const string ActivityDisplayName = "MyActivityName";
private static readonly ActivityKind ActivityKindServer = ActivityKind.Server;

[Fact]
public void ProbabilitySampler_OutOfRangeHighProbability()
public void OutOfRangeHighProbability()
{
Assert.Throws<ArgumentOutOfRangeException>(() => new ProbabilitySampler(1.01));
Assert.Throws<ArgumentOutOfRangeException>(() => new TraceIdRatioBasedSampler(1.01));
}

[Fact]
public void ProbabilitySampler_OutOfRangeLowProbability()
public void OutOfRangeLowProbability()
{
Assert.Throws<ArgumentOutOfRangeException>(() => new ProbabilitySampler(-0.00001));
Assert.Throws<ArgumentOutOfRangeException>(() => new TraceIdRatioBasedSampler(-0.00001));
}

[Fact]
public void ProbabilitySampler_SampleBasedOnTraceId()
public void SampleBasedOnTraceId()
{
Sampler defaultProbability = new ProbabilitySampler(0.0001);
Sampler defaultProbability = new TraceIdRatioBasedSampler(0.0001);

// This traceId will not be sampled by the ProbabilitySampler because the first 8 bytes as long
// This traceId will not be sampled by the TraceIdRatioBasedSampler because the first 8 bytes as long
// is not less than probability * Long.MAX_VALUE;
var notSampledtraceId =
ActivityTraceId.CreateFromBytes(
Expand All @@ -68,7 +68,7 @@ public void ProbabilitySampler_SampleBasedOnTraceId()
SamplingDecision.NotRecord,
defaultProbability.ShouldSample(new SamplingParameters(default, notSampledtraceId, ActivityDisplayName, ActivityKindServer, null, null)).Decision);

// This traceId will be sampled by the ProbabilitySampler because the first 8 bytes as long
// This traceId will be sampled by the TraceIdRatioBasedSampler because the first 8 bytes as long
// is less than probability * Long.MAX_VALUE;
var sampledtraceId =
ActivityTraceId.CreateFromBytes(
Expand Down Expand Up @@ -97,10 +97,10 @@ public void ProbabilitySampler_SampleBasedOnTraceId()
}

[Fact]
public void ProbabilitySampler_GetDescription()
public void GetDescription()
{
var expectedDescription = "ProbabilitySampler{0.500000}";
Assert.Equal(expectedDescription, new ProbabilitySampler(0.5).Description);
var expectedDescription = "TraceIdRatioBasedSampler{0.500000}";
Assert.Equal(expectedDescription, new TraceIdRatioBasedSampler(0.5).Description);
}
}
}