-
Notifications
You must be signed in to change notification settings - Fork 782
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added sampler to Activity - v1 (#683)
* Added sampler to Activity
- Loading branch information
1 parent
5ed0e45
commit 577ae6c
Showing
7 changed files
with
266 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// <copyright file="ActivitySampler.cs" company="OpenTelemetry Authors"> | ||
// Copyright The 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 | ||
{ | ||
/// <summary> | ||
/// Sampler to select data to be exported. This sampler executes before Activity object is created. | ||
/// </summary> | ||
public abstract class ActivitySampler | ||
{ | ||
/// <summary> | ||
/// Gets the sampler description. | ||
/// </summary> | ||
public abstract string Description { get; } | ||
|
||
/// <summary> | ||
/// Checks whether activity needs to be created and tracked. | ||
/// </summary> | ||
/// <param name="parentContext">Parent activity context. Typically taken from the wire.</param> | ||
/// <param name="traceId">Trace ID of a activity to be created.</param> | ||
/// <param name="spanId">Span ID of a activity to be created.</param> | ||
/// <param name="name"> Name (DisplayName) of the activity to be created. Note, that the name of the activity is settable. | ||
/// So this name can be changed later and Sampler implementation should assume that. | ||
/// Typical example of a name change is when <see cref="Activity"/> representing incoming http request | ||
/// has a name of url path and then being updated with route name when routing complete. | ||
/// </param> | ||
/// <param name="activityKind">The kind of the Activity.</param> | ||
/// <param name="tags">Initial set of Tags for the Activity being constructed.</param> | ||
/// <param name="links">Links associated with the activity.</param> | ||
/// <returns>Sampling decision on whether activity needs to be sampled or not.</returns> | ||
public abstract SamplingResult ShouldSample(in ActivityContext parentContext, in ActivityTraceId traceId, in ActivitySpanId spanId, string name, ActivityKind activityKind, IEnumerable<KeyValuePair<string, string>> tags, IEnumerable<ActivityLink> links); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/OpenTelemetry/Trace/Samplers/AlwaysOffActivitySampler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// <copyright file="AlwaysOffActivitySampler.cs" company="OpenTelemetry Authors"> | ||
// Copyright The 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.Samplers | ||
{ | ||
/// <summary> | ||
/// Sampler implementation which never samples any activity. | ||
/// </summary> | ||
public sealed class AlwaysOffActivitySampler : ActivitySampler | ||
{ | ||
/// <inheritdoc /> | ||
public override string Description { get; } = nameof(AlwaysOffActivitySampler); | ||
|
||
/// <inheritdoc /> | ||
public override SamplingResult ShouldSample(in ActivityContext parentContext, in ActivityTraceId traceId, in ActivitySpanId spanId, string name, ActivityKind activityKind, IEnumerable<KeyValuePair<string, string>> tags, IEnumerable<ActivityLink> links) | ||
{ | ||
return new SamplingResult(false); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/OpenTelemetry/Trace/Samplers/AlwaysOnActivitySampler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// <copyright file="AlwaysOnActivitySampler.cs" company="OpenTelemetry Authors"> | ||
// Copyright The 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.Samplers | ||
{ | ||
/// <summary> | ||
/// Sampler implementation which samples every activity. | ||
/// This sampler will be used as the default Sampler, if no other Sampler is configured. | ||
/// </summary> | ||
public sealed class AlwaysOnActivitySampler : ActivitySampler | ||
{ | ||
/// <inheritdoc /> | ||
public override string Description { get; } = nameof(AlwaysOnActivitySampler); | ||
|
||
/// <inheritdoc /> | ||
public override SamplingResult ShouldSample(in ActivityContext parentContext, in ActivityTraceId traceId, in ActivitySpanId spanId, string name, ActivityKind activityKind, IEnumerable<KeyValuePair<string, string>> tags, IEnumerable<ActivityLink> links) | ||
{ | ||
return new SamplingResult(true); | ||
} | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
test/OpenTelemetry.Tests/Implementation/Trace/Samplers/ActivitySamplersTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// <copyright file="ActivitySamplersTest.cs" company="OpenTelemetry Authors"> | ||
// Copyright The 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; | ||
using Xunit; | ||
|
||
namespace OpenTelemetry.Trace.Samplers.Test | ||
{ | ||
public class ActivitySamplersTest | ||
{ | ||
private static readonly ActivityKind ActivityKindServer = ActivityKind.Server; | ||
private readonly ActivityTraceId traceId; | ||
private readonly ActivitySpanId spanId; | ||
private readonly ActivitySpanId parentSpanId; | ||
|
||
public ActivitySamplersTest() | ||
{ | ||
traceId = ActivityTraceId.CreateRandom(); | ||
spanId = ActivitySpanId.CreateRandom(); | ||
parentSpanId = ActivitySpanId.CreateRandom(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(ActivityTraceFlags.Recorded)] | ||
[InlineData(ActivityTraceFlags.None)] | ||
public void AlwaysOnSampler_AlwaysReturnTrue(ActivityTraceFlags flags) | ||
{ | ||
var parentContext = new ActivityContext(traceId, parentSpanId, flags); | ||
var link = new ActivityLink(parentContext); | ||
|
||
Assert.True( | ||
new AlwaysOnActivitySampler() | ||
.ShouldSample( | ||
parentContext, | ||
traceId, | ||
spanId, | ||
"Another name", | ||
ActivityKindServer, | ||
null, | ||
new List<ActivityLink>() { link }).IsSampled); | ||
} | ||
|
||
[Fact] | ||
public void AlwaysOnSampler_GetDescription() | ||
{ | ||
// TODO: The name must be AlwaysOnSampler as per spec. | ||
// We should correct it when we replace span sampler with this. | ||
Assert.Equal("AlwaysOnActivitySampler", new AlwaysOnActivitySampler().Description); | ||
} | ||
|
||
[Theory] | ||
[InlineData(ActivityTraceFlags.Recorded)] | ||
[InlineData(ActivityTraceFlags.None)] | ||
public void AlwaysOffSampler_AlwaysReturnFalse(ActivityTraceFlags flags) | ||
{ | ||
var parentContext = new ActivityContext(traceId, parentSpanId, flags); | ||
var link = new ActivityLink(parentContext); | ||
|
||
Assert.False( | ||
new AlwaysOffActivitySampler() | ||
.ShouldSample( | ||
parentContext, | ||
traceId, | ||
spanId, | ||
"Another name", | ||
ActivityKindServer, | ||
null, | ||
new List<ActivityLink>() { link }).IsSampled); | ||
} | ||
|
||
[Fact] | ||
public void AlwaysOffSampler_GetDescription() | ||
{ | ||
// TODO: The name must be AlwaysOffSampler as per spec. | ||
// We should correct it when we replace span sampler with this. | ||
Assert.Equal("AlwaysOffActivitySampler", new AlwaysOffActivitySampler().Description); | ||
} | ||
} | ||
} |