Skip to content

Commit 49f1403

Browse files
committed
feat: Consolidate operation name and baggage enrichment into a single ActivityDetailsEnricher
1 parent eea7447 commit 49f1403

7 files changed

Lines changed: 82 additions & 116 deletions

File tree

README.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -403,17 +403,11 @@ Precedence for `Context.Operation.Id`: `operationId` property > `TraceId` proper
403403

404404
This sink is designed to work well with Serilog's asynchronous/batched processing. To keep telemetry deterministic, adding `OperationName` and `Baggage` from the ambient `Activity` is an explicit opt-in: copy the values onto the `LogEvent` before it reaches the sink.
405405

406-
Two built-in enrichers are included:
407-
408-
- `ActivityOperationNameEnricher` — copies `Activity.OperationName` into the `OperationName` log event property.
409-
- `ActivityBaggageEnricher` — copies baggage items from the current `Activity` into the `Baggage` log event property as a `StructureValue`.
410-
411-
Enable them using the provided `Enrich` extension methods:
406+
The sink includes an enricher that adds this by default. Enable it using the provided `Enrich` extension method:
412407

413408
```csharp
414409
Log.Logger = new LoggerConfiguration()
415-
.Enrich.WithOperationName()
416-
.Enrich.WithBaggage()
410+
.Enrich.WithActivityDetails(includeOperationName: true, includeBaggage: true)
417411
.WriteTo.ApplicationInsights(telemetryConfiguration, TelemetryConverter.Traces)
418412
.CreateLogger();
419413
```
@@ -422,7 +416,7 @@ Log.Logger = new LoggerConfiguration()
422416

423417
This is a new major release (5.0). Notable changes:
424418

425-
- **OperationName and Baggage are opt-in:** they are only forwarded when present as `LogEvent` properties (use the built-in enrichers above or your own enricher).
419+
- **OperationName and Baggage are opt-in:** they are only forwarded when present as `LogEvent` properties (use the built-in enricher above or your own enricher).
426420
- **Less redundancy in custom dimensions by default:** operation-related values are set on `ITelemetry.Context` and are not duplicated into `telemetry.Properties` unless enabled.
427421

428422
### `TelemetryConverterBase` constructor flags

src/Serilog.Sinks.ApplicationInsights/LoggerEnrichmentConfigurationExtensions.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,10 @@ public static class LoggerEnrichmentConfigurationExtensions
1515
extension(LoggerEnrichmentConfiguration loggerEnrichmentConfiguration)
1616
{
1717
/// <summary>
18-
/// Enriches log events with the operation name from <see cref="Activity" />.
18+
/// Enriches log events with details from <see cref="Activity" />.
1919
/// </summary>
2020
/// <returns>Logger configuration, allowing configuration to continue.</returns>
21-
public LoggerConfiguration WithOperationName()
22-
=> loggerEnrichmentConfiguration.With<ActivityOperationNameEnricher>();
23-
24-
/// <summary>
25-
/// Enriches log events with the baggage from <see cref="Activity" />.
26-
/// </summary>
27-
/// <returns>Logger configuration, allowing configuration to continue.</returns>
28-
public LoggerConfiguration WithBaggage()
29-
=> loggerEnrichmentConfiguration.With<ActivityBaggageEnricher>();
21+
public LoggerConfiguration WithActivityDetails(bool includeOperationName = true, bool includeBaggage = true)
22+
=> loggerEnrichmentConfiguration.With(new ActivityDetailsEnricher(includeOperationName, includeBaggage));
3023
}
3124
}

src/Serilog.Sinks.ApplicationInsights/Sinks/ApplicationInsights/Enrichers/ActivityBaggageEnricher.cs renamed to src/Serilog.Sinks.ApplicationInsights/Sinks/ApplicationInsights/Enrichers/ActivityDetailsEnricher.cs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
namespace Serilog.Sinks.ApplicationInsights.Enrichers;
1010

1111
/// <summary>
12-
/// Enriches log events with the baggage from <see cref="Activity"/>.
12+
/// Enriches log events with details from <see cref="Activity"/>.
1313
/// </summary>
14-
public class ActivityBaggageEnricher : ILogEventEnricher
14+
public class ActivityDetailsEnricher(bool includeOperationName, bool includeBaggage) : ILogEventEnricher
1515
{
16+
readonly bool _includeOperationName = includeOperationName;
17+
readonly bool _includeBaggage = includeBaggage;
18+
1619
/// <inheritdoc/>
1720
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
1821
{
@@ -26,7 +29,34 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
2629
throw new ArgumentNullException(nameof(propertyFactory));
2730
}
2831

29-
if (Activity.Current is not {} activity)
32+
if (Activity.Current is not { } activity)
33+
{
34+
return;
35+
}
36+
37+
EnrichOperationName(logEvent, propertyFactory, activity);
38+
EnrichBaggage(logEvent, propertyFactory, activity);
39+
}
40+
41+
private void EnrichOperationName(LogEvent logEvent, ILogEventPropertyFactory propertyFactory, Activity activity)
42+
{
43+
if (!_includeOperationName)
44+
{
45+
return;
46+
}
47+
48+
if (activity.OperationName is not string operationName)
49+
{
50+
return;
51+
}
52+
53+
LogEventProperty operationNameProperty = propertyFactory.CreateProperty(TelemetryConverterBase.OperationNameProperty, operationName);
54+
logEvent.AddPropertyIfAbsent(operationNameProperty);
55+
}
56+
57+
private void EnrichBaggage(LogEvent logEvent, ILogEventPropertyFactory propertyFactory, Activity activity)
58+
{
59+
if (!_includeBaggage)
3060
{
3161
return;
3262
}

src/Serilog.Sinks.ApplicationInsights/Sinks/ApplicationInsights/Enrichers/ActivityOperationNameEnricher.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.

test/Serilog.Sinks.ApplicationInsights.Tests/ApplicationInsightsTest.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,7 @@ protected ApplicationInsightsTest(ITelemetryConverter converter = null, bool add
2020
.MinimumLevel.Debug()
2121
.Enrich.FromLogContext();
2222

23-
if (addOperationNameEnricher)
24-
{
25-
loggerConfiguration = loggerConfiguration.Enrich.WithOperationName();
26-
}
27-
28-
if (addBaggageEnricher)
29-
{
30-
loggerConfiguration = loggerConfiguration.Enrich.WithBaggage();
31-
}
32-
23+
loggerConfiguration = loggerConfiguration.Enrich.WithActivityDetails(addOperationNameEnricher, addBaggageEnricher);
3324
Logger = loggerConfiguration.CreateLogger();
3425
}
3526

test/Serilog.Sinks.ApplicationInsights.Tests/Enrichers/ActivityBaggageEnricherTests.cs renamed to test/Serilog.Sinks.ApplicationInsights.Tests/Enrichers/ActivityDetailsEnricherTests.cs

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,47 @@
88

99
namespace Serilog.Sinks.ApplicationInsights.Tests.Enrichers;
1010

11-
public class ActivityBaggageEnricherTests
11+
public class ActivityDetailsEnricherTests
1212
{
1313
[Fact]
14-
public void Single_baggage_value_is_enriched()
14+
public void Operation_name_is_enriched_if_enabled()
1515
{
16-
ActivityBaggageEnricher enricher = new();
16+
ActivityDetailsEnricher enricher = new(includeOperationName: true, includeBaggage: false);
17+
string operationName = Guid.NewGuid().ToString("N");
18+
using Activity activity = new(operationName);
19+
activity.Start();
20+
LogEvent logEvent = new(DateTimeOffset.Now, LogEventLevel.Information, null, MessageTemplate.Empty, []);
21+
22+
enricher.Enrich(logEvent, TestLogEventPropertyFactory.Instance);
23+
24+
bool hasProperty = logEvent.Properties.TryGetValue("OperationName", out LogEventPropertyValue property);
25+
Assert.True(hasProperty);
26+
Assert.NotNull(property);
27+
Assert.IsType<ScalarValue>(property);
28+
ScalarValue scalarValue = (ScalarValue)property;
29+
Assert.Equal(operationName, scalarValue.Value);
30+
}
31+
32+
[Fact]
33+
public void Operation_name_is_not_enriched_if_disabled()
34+
{
35+
ActivityDetailsEnricher enricher = new(includeOperationName: false, includeBaggage: false);
36+
string operationName = Guid.NewGuid().ToString("N");
37+
using Activity activity = new(operationName);
38+
activity.Start();
39+
LogEvent logEvent = new(DateTimeOffset.Now, LogEventLevel.Information, null, MessageTemplate.Empty, []);
40+
41+
enricher.Enrich(logEvent, TestLogEventPropertyFactory.Instance);
42+
43+
bool hasProperty = logEvent.Properties.TryGetValue("OperationName", out LogEventPropertyValue property);
44+
Assert.False(hasProperty);
45+
Assert.Null(property);
46+
}
47+
48+
[Fact]
49+
public void Single_baggage_value_is_not_enriched_if_disabled()
50+
{
51+
ActivityDetailsEnricher enricher = new(includeOperationName: false, includeBaggage: false);
1752
using Activity activity = new("TestActivity");
1853
string baggageName = Guid.NewGuid().ToString("N");
1954
string baggageValue = Guid.NewGuid().ToString("N");
@@ -24,25 +59,17 @@ public void Single_baggage_value_is_enriched()
2459
enricher.Enrich(logEvent, TestLogEventPropertyFactory.Instance);
2560

2661
bool hasProperty = logEvent.Properties.TryGetValue("Baggage", out LogEventPropertyValue property);
27-
Assert.True(hasProperty);
28-
Assert.NotNull(property);
29-
Assert.IsType<StructureValue>(property);
30-
StructureValue scalarValue = (StructureValue)property;
31-
Assert.Single(scalarValue.Properties);
32-
LogEventProperty valueProperty = scalarValue.Properties[0];
33-
Assert.Equal(baggageName, valueProperty.Name);
34-
Assert.IsType<ScalarValue>(valueProperty.Value);
35-
ScalarValue scalarValueInner = (ScalarValue)valueProperty.Value;
36-
Assert.Equal(baggageValue, scalarValueInner.Value);
62+
Assert.False(hasProperty);
63+
Assert.Null(property);
3764
}
3865

3966
[Fact]
40-
public void Multiple_baggage_values_are_enriched()
67+
public void Multiple_baggage_values_are_enriched_if_enabled()
4168
{
4269
Random random = new();
4370
Dictionary<string, string> baggageItems = GenerateRandomBaggageItems(10);
4471

45-
ActivityBaggageEnricher enricher = new();
72+
ActivityDetailsEnricher enricher = new(includeOperationName: false, includeBaggage: true);
4673
using Activity activity = new("TestActivity");
4774
foreach (var item in baggageItems)
4875
{

test/Serilog.Sinks.ApplicationInsights.Tests/Enrichers/ActivityOperationNameEnricherTests.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)