Skip to content

Commit

Permalink
Renaming ProbabilitySampler to TraceIdRatioBasedSampler (#1174)
Browse files Browse the repository at this point in the history
* Renaming ProbabilitySampler to TraceIdRatioBasedSampler

* updating changelog

Co-authored-by: Cijo Thomas <[email protected]>
  • Loading branch information
eddynaka and cijothomas authored Aug 27, 2020
1 parent 9cb4bc6 commit 351c788
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
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);
}
}
}

0 comments on commit 351c788

Please sign in to comment.