-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Extensions] Update BaggageActivityProcessor to use baggage key predi…
…cate (#1816) Co-authored-by: Piotr Kiełkowicz <pkiekowicz@splunk.com> Co-authored-by: Cijo Thomas <cithomas@microsoft.com> Co-authored-by: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com>
- Loading branch information
1 parent
7d03e00
commit d4d00a5
Showing
7 changed files
with
219 additions
and
11 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
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
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
139 changes: 139 additions & 0 deletions
139
test/OpenTelemetry.Extensions.Tests/Trace/BaggageActivityProcessorTests.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,139 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using System.Text.RegularExpressions; | ||
using OpenTelemetry.Trace; | ||
using Xunit; | ||
|
||
namespace OpenTelemetry.Extensions.Tests.Trace; | ||
|
||
public class BaggageActivityProcessorTests | ||
{ | ||
[Fact] | ||
public void BaggageActivityProcessor_CanAddAllowAllBaggageKeysPredicate() | ||
{ | ||
var sourceName = GetTestMethodName(); | ||
|
||
using var provider = Sdk.CreateTracerProviderBuilder() | ||
.AddBaggageActivityProcessor(BaggageActivityProcessor.AllowAllBaggageKeys) | ||
.AddSource(sourceName) | ||
.Build(); | ||
|
||
Baggage.SetBaggage("key", "value"); | ||
Baggage.SetBaggage("other_key", "other_value"); | ||
|
||
using var source = new ActivitySource(sourceName); | ||
using var activity = source.StartActivity("name", ActivityKind.Server); | ||
Assert.NotNull(activity); | ||
activity.Stop(); | ||
|
||
Assert.Contains(activity.Tags, kv => kv.Key == "key" && kv.Value == "value"); | ||
Assert.Contains(activity.Tags, kv => kv.Key == "other_key" && kv.Value == "other_value"); | ||
} | ||
|
||
[Fact] | ||
public void BaggageActivityProcessor_CanUseCustomPredicate() | ||
{ | ||
var sourceName = GetTestMethodName(); | ||
|
||
using var provider = Sdk.CreateTracerProviderBuilder() | ||
.AddBaggageActivityProcessor((baggageKey) => baggageKey.StartsWith("key", StringComparison.Ordinal)) | ||
.AddSource(sourceName) | ||
.Build(); | ||
|
||
Baggage.SetBaggage("key", "value"); | ||
Baggage.SetBaggage("other_key", "other_value"); | ||
|
||
using var source = new ActivitySource(sourceName); | ||
using var activity = source.StartActivity("name", ActivityKind.Client); | ||
Assert.NotNull(activity); | ||
activity.Stop(); | ||
|
||
Assert.Contains(activity.Tags, kv => kv.Key == "key" && kv.Value == "value"); | ||
Assert.DoesNotContain(activity.Tags, kv => kv.Key == "other_key" && kv.Value == "other_value"); | ||
} | ||
|
||
[Fact] | ||
public void BaggageActivityProcessor_CanUseRegex() | ||
{ | ||
var sourceName = GetTestMethodName(); | ||
|
||
var regex = new Regex("^mykey", RegexOptions.Compiled); | ||
using var provider = Sdk.CreateTracerProviderBuilder() | ||
.AddBaggageActivityProcessor((baggageKey) => regex.IsMatch(baggageKey)) | ||
.AddSource(sourceName) | ||
.Build(); | ||
|
||
Baggage.SetBaggage("mykey", "value"); | ||
Baggage.SetBaggage("other_key", "other_value"); | ||
|
||
using var source = new ActivitySource(sourceName); | ||
using var activity = source.StartActivity("name", ActivityKind.Client); | ||
Assert.NotNull(activity); | ||
activity.Stop(); | ||
|
||
Assert.Contains(activity.Tags, kv => kv.Key == "mykey" && kv.Value == "value"); | ||
Assert.DoesNotContain(activity.Tags, kv => kv.Key == "other_key" && kv.Value == "other_value"); | ||
} | ||
|
||
[Fact] | ||
public void BaggageActivityProcessor_PredicateThrows_DoesNothing() | ||
{ | ||
var sourceName = GetTestMethodName(); | ||
|
||
using var provider = Sdk.CreateTracerProviderBuilder() | ||
.AddBaggageActivityProcessor(_ => throw new Exception("Predicate throws an exception.")) | ||
.AddSource(sourceName) | ||
.Build(); | ||
|
||
Baggage.SetBaggage("key", "value"); | ||
|
||
using var source = new ActivitySource(sourceName); | ||
using var activity = source.StartActivity("name", ActivityKind.Server); | ||
Assert.NotNull(activity); | ||
activity.Stop(); | ||
|
||
Assert.DoesNotContain(activity.Tags, kv => kv.Key == "key" && kv.Value == "value"); | ||
} | ||
|
||
[Fact] | ||
public void BaggageActivityProcessor_PredicateThrows_OnlyDropsEntriesThatThrow() | ||
{ | ||
var sourceName = GetTestMethodName(); | ||
|
||
// First call to predicate should not throw, second call should. | ||
using var provider = Sdk.CreateTracerProviderBuilder() | ||
.AddBaggageActivityProcessor(key => | ||
{ | ||
if (key == "key") | ||
{ | ||
throw new Exception("Predicate throws an exception."); | ||
} | ||
|
||
return true; | ||
}) | ||
.AddSource(sourceName) | ||
.Build(); | ||
|
||
Baggage.SetBaggage("key", "value"); | ||
Baggage.SetBaggage("other_key", "other_value"); | ||
Baggage.SetBaggage("another_key", "another_value"); | ||
|
||
using var source = new ActivitySource(sourceName); | ||
using var activity = source.StartActivity("name", ActivityKind.Server); | ||
Assert.NotNull(activity); | ||
activity.Stop(); | ||
|
||
// Only keys that do not throw should be added. | ||
Assert.DoesNotContain(activity.Tags, kv => kv.Key == "key" && kv.Value == "value"); | ||
Assert.Contains(activity.Tags, kv => kv.Key == "other_key" && kv.Value == "other_value"); | ||
Assert.Contains(activity.Tags, kv => kv.Key == "another_key" && kv.Value == "another_value"); | ||
} | ||
|
||
private static string GetTestMethodName([CallerMemberName] string callingMethodName = "") | ||
{ | ||
return callingMethodName; | ||
} | ||
} |