-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into instrumentationServiceFabricRemoting
- Loading branch information
Showing
12 changed files
with
476 additions
and
3 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
46 changes: 46 additions & 0 deletions
46
src/OpenTelemetry.Extensions/Internal/BaggageLogRecordProcessor.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,46 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using OpenTelemetry.Logs; | ||
|
||
namespace OpenTelemetry.Extensions.Internal; | ||
|
||
internal sealed class BaggageLogRecordProcessor : BaseProcessor<LogRecord> | ||
{ | ||
private readonly Predicate<string> baggageKeyPredicate; | ||
|
||
public BaggageLogRecordProcessor(Predicate<string> baggageKeyPredicate) | ||
{ | ||
this.baggageKeyPredicate = baggageKeyPredicate ?? throw new ArgumentNullException(nameof(baggageKeyPredicate)); | ||
} | ||
|
||
public static Predicate<string> AllowAllBaggageKeys => (_) => true; | ||
|
||
public override void OnEnd(LogRecord data) | ||
{ | ||
var baggage = Baggage.Current; | ||
|
||
if (data != null && baggage.Count > 0) | ||
{ | ||
var capacity = (data.Attributes?.Count ?? 0) + baggage.Count; | ||
var attributes = new List<KeyValuePair<string, object?>>(capacity); | ||
|
||
foreach (var entry in baggage) | ||
{ | ||
if (this.baggageKeyPredicate(entry.Key)) | ||
{ | ||
attributes.Add(new(entry.Key, entry.Value)); | ||
} | ||
} | ||
|
||
if (data.Attributes != null) | ||
{ | ||
attributes.AddRange(data.Attributes); | ||
} | ||
|
||
data.Attributes = attributes; | ||
} | ||
|
||
base.OnEnd(data!); | ||
} | ||
} |
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
135 changes: 135 additions & 0 deletions
135
test/OpenTelemetry.Extensions.Tests/Logs/LoggerFactoryBaggageLogRecordProcessorTests.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,135 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System.Runtime.CompilerServices; | ||
using System.Text.RegularExpressions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using OpenTelemetry.Logs; | ||
using OpenTelemetry.Trace; | ||
using Xunit; | ||
|
||
namespace OpenTelemetry.Extensions.Tests.Logs; | ||
|
||
public class LoggerFactoryBaggageLogRecordProcessorTests | ||
{ | ||
[Fact] | ||
public void BaggageLogRecordProcessor_CanAddAllowAllBaggageKeysPredicate() | ||
{ | ||
var logRecordList = new List<LogRecord>(); | ||
using var loggerFactory = LoggerFactory.Create(builder => builder | ||
.AddOpenTelemetry(options => | ||
{ | ||
options.AddBaggageProcessor(); | ||
options.AddInMemoryExporter(logRecordList); | ||
}) | ||
.AddFilter("*", LogLevel.Trace)); // Enable all LogLevels | ||
Baggage.SetBaggage("allow", "value"); | ||
|
||
var logger = loggerFactory.CreateLogger(GetTestMethodName()); | ||
logger.LogError("this does not matter"); | ||
var logRecord = Assert.Single(logRecordList); | ||
Assert.NotNull(logRecord); | ||
Assert.NotNull(logRecord.Attributes); | ||
Assert.Contains(logRecord.Attributes, kv => kv.Key == "allow"); | ||
} | ||
|
||
[Fact] | ||
public void BaggageLogRecordProcessor_CanUseCustomPredicate() | ||
{ | ||
var logRecordList = new List<LogRecord>(); | ||
using var loggerFactory = LoggerFactory.Create(builder => builder | ||
.AddOpenTelemetry(options => | ||
{ | ||
options.AddBaggageProcessor((baggageKey) => baggageKey.StartsWith("allow", StringComparison.Ordinal)); | ||
options.AddInMemoryExporter(logRecordList); | ||
}) | ||
.AddFilter("*", LogLevel.Trace)); // Enable all LogLevels | ||
Baggage.SetBaggage("allow", "value"); | ||
Baggage.SetBaggage("deny", "other_value"); | ||
|
||
var logger = loggerFactory.CreateLogger(GetTestMethodName()); | ||
logger.LogError("this does not matter"); | ||
var logRecord = Assert.Single(logRecordList); | ||
Assert.NotNull(logRecord); | ||
Assert.NotNull(logRecord.Attributes); | ||
Assert.Contains(logRecord.Attributes, kv => kv.Key == "allow"); | ||
Assert.DoesNotContain(logRecord.Attributes, kv => kv.Key == "deny"); | ||
} | ||
|
||
[Fact] | ||
public void BaggageLogRecordProcessor_CanUseRegex() | ||
{ | ||
var regex = new Regex("^allow", RegexOptions.Compiled); | ||
var logRecordList = new List<LogRecord>(); | ||
using var loggerFactory = LoggerFactory.Create(builder => builder | ||
.AddOpenTelemetry(options => | ||
{ | ||
options.AddBaggageProcessor(regex.IsMatch); | ||
options.AddInMemoryExporter(logRecordList); | ||
}) | ||
.AddFilter("*", LogLevel.Trace)); // Enable all LogLevels | ||
Baggage.SetBaggage("allow", "value"); | ||
Baggage.SetBaggage("deny", "other_value"); | ||
|
||
var logger = loggerFactory.CreateLogger(GetTestMethodName()); | ||
logger.LogError("this does not matter"); | ||
var logRecord = Assert.Single(logRecordList); | ||
Assert.NotNull(logRecord); | ||
Assert.NotNull(logRecord.Attributes); | ||
Assert.Contains(logRecord.Attributes, kv => kv.Key == "allow"); | ||
Assert.DoesNotContain(logRecord.Attributes, kv => kv.Key == "deny"); | ||
} | ||
|
||
[Fact] | ||
public void BaggageLogRecordProcessor_PredicateThrows_DoesNothing() | ||
{ | ||
var logRecordList = new List<LogRecord>(); | ||
using var loggerFactory = LoggerFactory.Create(builder => builder | ||
.AddOpenTelemetry(options => | ||
{ | ||
options.AddBaggageProcessor(_ => throw new Exception("Predicate throws an exception.")); | ||
options.AddInMemoryExporter(logRecordList); | ||
}) | ||
.AddFilter("*", LogLevel.Trace)); // Enable all LogLevels | ||
Baggage.SetBaggage("deny", "value"); | ||
|
||
var logger = loggerFactory.CreateLogger(GetTestMethodName()); | ||
logger.LogError("this does not matter"); | ||
var logRecord = Assert.Single(logRecordList); | ||
Assert.NotNull(logRecord); | ||
Assert.DoesNotContain(logRecord?.Attributes ?? [], kv => kv.Key == "deny"); | ||
} | ||
|
||
[Fact] | ||
public void BaggageLogRecordProcessor_PredicateThrows_OnlyDropsEntriesThatThrow() | ||
{ | ||
var logRecordList = new List<LogRecord>(); | ||
using var loggerFactory = LoggerFactory.Create(builder => builder | ||
.AddOpenTelemetry(options => | ||
{ | ||
options.AddBaggageProcessor(key => | ||
{ | ||
return key != "allow" ? throw new Exception("Predicate throws an exception.") : true; | ||
}); | ||
options.AddInMemoryExporter(logRecordList); | ||
}) | ||
.AddFilter("*", LogLevel.Trace)); // Enable all LogLevels | ||
Baggage.SetBaggage("allow", "value"); | ||
Baggage.SetBaggage("deny", "value"); | ||
Baggage.SetBaggage("deny_2", "value"); | ||
|
||
var logger = loggerFactory.CreateLogger(GetTestMethodName()); | ||
logger.LogError("this does not matter"); | ||
var logRecord = Assert.Single(logRecordList); | ||
Assert.NotNull(logRecord); | ||
Assert.NotNull(logRecord.Attributes); | ||
Assert.Contains(logRecord.Attributes, kv => kv.Key == "allow"); | ||
Assert.DoesNotContain(logRecord?.Attributes ?? [], kv => kv.Key == "deny"); | ||
} | ||
|
||
private static string GetTestMethodName([CallerMemberName] string callingMethodName = "") | ||
{ | ||
return callingMethodName; | ||
} | ||
} |
Oops, something went wrong.