Skip to content

Commit eea7447

Browse files
committed
feat: Support different casing of OperationId
1 parent ad2bb9e commit eea7447

7 files changed

Lines changed: 52 additions & 22 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,9 @@ private class CustomConverter : TraceTelemetryConverter
254254
telemetry.Context.User.Id = logEvent.Properties["UserId"].ToString();
255255
}
256256
// post-process the telemetry's context to contain the operation id
257-
if (logEvent.Properties.ContainsKey("operationId"))
257+
if (logEvent.Properties.ContainsKey("OperationId"))
258258
{
259-
telemetry.Context.Operation.Id = logEvent.Properties["operationId"].ToString();
259+
telemetry.Context.Operation.Id = logEvent.Properties["OperationId"].ToString();
260260
}
261261
// post-process the telemetry's context to contain the operation parent id
262262
if (logEvent.Properties.ContainsKey("ParentSpanId"))
@@ -267,7 +267,7 @@ private class CustomConverter : TraceTelemetryConverter
267267
ISupportProperties propTelemetry = (ISupportProperties)telemetry;
268268

269269
// find redundant properties
270-
var removeProps = new[] { "UserId", "ParentSpanId", "operationId" };
270+
var removeProps = new[] { "UserId", "ParentSpanId", "OperationId" };
271271
removeProps = removeProps.Where(prop => propTelemetry.Properties.ContainsKey(prop)).ToArray();
272272

273273
foreach (var prop in removeProps)
@@ -369,7 +369,7 @@ public class OperationIdEnricher : ILogEventEnricher
369369
{
370370
if (logEvent.Properties.TryGetValue("RequestId", out var requestId))
371371
{
372-
logEvent.AddPropertyIfAbsent(new LogEventProperty("operationId", requestId));
372+
logEvent.AddPropertyIfAbsent(new LogEventProperty("OperationId", requestId));
373373
}
374374
}
375375
}

src/Serilog.Sinks.ApplicationInsights/Sinks/ApplicationInsights/TelemetryConverters/TelemetryConverterBase.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public abstract class TelemetryConverterBase : ITelemetryConverter
3333
/// <summary>
3434
/// Property that is included when in log context, will be pushed out as AI operation ID.
3535
/// </summary>
36-
public const string OperationIdProperty = "operationId";
36+
public const string OperationIdProperty = "OperationId";
3737

3838
/// <summary>
3939
/// Property that is included when in log context, will be pushed out as AI parent span id.
@@ -208,8 +208,8 @@ public void ForwardPropertiesToTelemetryProperties(LogEvent logEvent,
208208
private static void PopulateTelemetryFromLogEvent(LogEvent logEvent, ITelemetry telemetry)
209209
{
210210
// Operation.Id (TraceId)
211-
if (logEvent.Properties.TryGetValue(OperationIdProperty, out var operationIdProp))
212-
telemetry.Context.Operation.Id = operationIdProp.ToString().Trim('"');
211+
if (TrySetOperationIdFromLogEvent(logEvent, telemetry, out var operationId))
212+
telemetry.Context.Operation.Id = operationId;
213213
else if (logEvent.TraceId is ActivityTraceId traceId)
214214
telemetry.Context.Operation.Id = traceId.ToHexString();
215215

@@ -231,6 +231,28 @@ private static void PopulateTelemetryFromLogEvent(LogEvent logEvent, ITelemetry
231231
}
232232
}
233233

234+
private static bool TrySetOperationIdFromLogEvent(LogEvent logEvent, ITelemetry telemetry, out string operationId)
235+
{
236+
operationId = null;
237+
if (logEvent.Properties.TryGetValue(OperationIdProperty, out var operationIdProp))
238+
{
239+
operationId = operationIdProp.ToString();
240+
}
241+
else
242+
{
243+
operationId = logEvent.Properties
244+
.FirstOrDefault(p => string.Equals(p.Key, OperationIdProperty, StringComparison.OrdinalIgnoreCase))
245+
.Value?
246+
.ToString();
247+
}
248+
249+
if (string.IsNullOrEmpty(operationId))
250+
return false;
251+
252+
operationId = operationId.Trim('\"');
253+
return true;
254+
}
255+
234256
private static bool ForwardActivityBaggage(LogEvent logEvent, ISupportProperties telemetryProperties, IFormatProvider formatProvider)
235257
{
236258
if (!logEvent.Properties.TryGetValue(BaggageProperty, out var baggageProp)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void Json_parameter_is_compact()
5353
[Fact]
5454
public void OperationId_from_logContext_is_included()
5555
{
56-
using (LogContext.PushProperty("operationId", "myId1"))
56+
using (LogContext.PushProperty("OperationId", "myId1"))
5757
{
5858
Logger.Information("capture id?");
5959

test/Serilog.Sinks.ApplicationInsights.Tests/TelemetryConverters/Event/EventTelemetryConverterTest.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ public void TraceIdIsSet()
5454
Assert.Equal(activity.TraceId.ToHexString(), LastSubmittedEventTelemetry.Context.Operation.Id);
5555
}
5656

57-
[Fact]
58-
public void OperationIdTakesPrecedenceOverTraceId()
57+
[Theory]
58+
[InlineData("Hello, {operationId}!")]
59+
[InlineData("Hello, {OperationId}!")]
60+
public void OperationIdTakesPrecedenceOverTraceId(string messageTemplate)
5961
{
6062
using Activity activity = new("TestActivity");
6163
activity.Start();
6264
string operationId = Guid.NewGuid().ToString("N");
63-
Logger.Information("Hello, {operationId}!", operationId);
65+
Logger.Information(messageTemplate, operationId);
6466
Assert.Equal(operationId, LastSubmittedEventTelemetry.Context.Operation.Id);
6567
Assert.Null(LastSubmittedEventTelemetry.Context.Operation.ParentId);
6668
}

test/Serilog.Sinks.ApplicationInsights.Tests/TelemetryConverters/Event/IncludeOperationIdTest.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ public IncludeOperationIdTest()
1010
{
1111
}
1212

13-
[Fact]
14-
public void OperationIdIsSetAsTraceProperty()
13+
[Theory]
14+
[InlineData("Hello, {operationId}!", "operationId", "foo-operation-id")]
15+
[InlineData("Hello, {OperationId}!", "OperationId", "bar-operation-id")]
16+
public void OperationIdIsSetAsTraceProperty(string pattern, string operationIdKey, string expectedOperationId)
1517
{
1618
using var activity = new System.Diagnostics.Activity("TestActivity");
1719
activity.Start();
1820

19-
Logger.Information("Hello, {operationId}!", "foo-operation-id");
21+
Logger.Information(pattern, expectedOperationId);
2022

21-
Assert.Equal("foo-operation-id", LastSubmittedEventTelemetry.Properties["operationId"]);
23+
Assert.Equal(expectedOperationId, LastSubmittedEventTelemetry.Properties[operationIdKey]);
2224
}
2325
}

test/Serilog.Sinks.ApplicationInsights.Tests/TelemetryConverters/Trace/IncludeOperationIdTest.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ public IncludeOperationIdTest()
1010
{
1111
}
1212

13-
[Fact]
14-
public void OperationIdIsSetAsTraceProperty()
13+
[Theory]
14+
[InlineData("Hello, {operationId}!", "operationId", "foo-operation-id")]
15+
[InlineData("Hello, {OperationId}!", "OperationId", "bar-operation-id")]
16+
public void OperationIdIsSetAsTraceProperty(string pattern, string operationIdKey, string expectedOperationId)
1517
{
1618
using var activity = new System.Diagnostics.Activity("TestActivity");
1719
activity.Start();
1820

19-
Logger.Information("Hello, {operationId}!", "foo-operation-id");
21+
Logger.Information(pattern, expectedOperationId);
2022

21-
Assert.Equal("foo-operation-id", LastSubmittedTraceTelemetry.Properties["operationId"]);
23+
Assert.Equal(expectedOperationId, LastSubmittedTraceTelemetry.Properties[operationIdKey]);
2224
}
2325
}

test/Serilog.Sinks.ApplicationInsights.Tests/TelemetryConverters/Trace/TraceTelemetryConverterTest.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ public void TraceIdIsSet()
5454
Assert.Equal(activity.TraceId.ToHexString(), LastSubmittedTraceTelemetry.Context.Operation.Id);
5555
}
5656

57-
[Fact]
58-
public void OperationIdTakesPrecedenceOverTraceId()
57+
[Theory]
58+
[InlineData("Hello, {operationId}!")]
59+
[InlineData("Hello, {OperationId}!")]
60+
public void OperationIdTakesPrecedenceOverTraceId(string messageTemplate)
5961
{
6062
using Activity activity = new("TestActivity");
6163
activity.Start();
6264
string operationId = Guid.NewGuid().ToString("N");
63-
Logger.Information("Hello, {operationId}!", operationId);
65+
Logger.Information(messageTemplate, operationId);
6466
Assert.Equal(operationId, LastSubmittedTraceTelemetry.Context.Operation.Id);
6567
Assert.Null(LastSubmittedTraceTelemetry.Context.Operation.ParentId);
6668
}

0 commit comments

Comments
 (0)