Skip to content

Commit 00c0e26

Browse files
committed
Updated the CaptureCorrelationId method to try three matching strategies in order:
Original path (case-sensitive) - tries the exact path you specified Output case transformation - applies the configured logger output case (for backward compatibility) Case-insensitive match - falls back to case-insensitive property matching
1 parent 958d519 commit 00c0e26

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

libraries/src/AWS.Lambda.Powertools.Logging/Internal/LoggingAspect.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,32 @@ private void CaptureCorrelationId(object eventArg, string correlationIdPath)
133133
// TODO: For casing parsing to be removed from Logging v2 when we get rid of outputcase without this CorrelationIdPaths.ApiGatewayRest would not work
134134
// TODO: This will be removed and replaced by JMesPath
135135

136-
var pathWithOutputCase = correlationIdPaths[i].ToCase(_currentConfig.LoggerOutputCase);
137-
if (!element.TryGetProperty(pathWithOutputCase, out var childElement))
138-
break;
136+
var pathSegment = correlationIdPaths[i];
137+
JsonElement childElement;
138+
139+
// Try original path first (case-sensitive)
140+
if (!element.TryGetProperty(pathSegment, out childElement))
141+
{
142+
// Try with output case transformation
143+
var pathWithOutputCase = pathSegment.ToCase(_currentConfig.LoggerOutputCase);
144+
if (!element.TryGetProperty(pathWithOutputCase, out childElement))
145+
{
146+
// Try case-insensitive match as last resort
147+
var found = false;
148+
foreach (var property in element.EnumerateObject())
149+
{
150+
if (string.Equals(property.Name, pathSegment, StringComparison.OrdinalIgnoreCase))
151+
{
152+
childElement = property.Value;
153+
found = true;
154+
break;
155+
}
156+
}
157+
158+
if (!found)
159+
break;
160+
}
161+
}
139162

140163
element = childElement;
141164
if (i == correlationIdPaths.Length - 1)

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Attributes/LoggingAttributeTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using Amazon.Lambda.APIGatewayEvents;
66
using Amazon.Lambda.ApplicationLoadBalancerEvents;
7+
using Amazon.Lambda.CloudWatchEvents;
78
using Amazon.Lambda.CloudWatchEvents.S3Events;
89
using Amazon.Lambda.TestUtilities;
910
using AWS.Lambda.Powertools.Common;
@@ -172,6 +173,7 @@ public void OnExit_WhenHandler_ClearState_Enabled_ClearKeys()
172173
[InlineData(CorrelationIdPaths.ApplicationLoadBalancer)]
173174
[InlineData(CorrelationIdPaths.EventBridge)]
174175
[InlineData("/headers/my_request_id_header")]
176+
[InlineData("/detail/correlationId")]
175177
public void OnEntry_WhenEventArgExists_CapturesCorrelationId(string correlationIdPath)
176178
{
177179
// Arrange
@@ -213,6 +215,15 @@ public void OnEntry_WhenEventArgExists_CapturesCorrelationId(string correlationI
213215
}
214216
});
215217
break;
218+
case "/detail/correlationId":
219+
_testHandlers.CorrelationCloudWatchEventCustomPath(new CloudWatchEvent<CwEvent>
220+
{
221+
Detail = new CwEvent
222+
{
223+
CorrelationId = correlationId
224+
}
225+
});
226+
break;
216227
}
217228

218229
// Assert

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Handlers/TestHandlers.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public void CorrelationCloudWatchEvent(CloudWatchEvent<S3ObjectCreate> cwEvent)
6666
{
6767
}
6868

69+
[Logging(CorrelationIdPath = "/detail/correlationId")]
70+
public void CorrelationCloudWatchEventCustomPath(CloudWatchEvent<CwEvent> cwEvent)
71+
{
72+
}
73+
6974
[Logging(CorrelationIdPath = "/headers/my_request_id_header")]
7075
public void CorrelationIdFromString(TestObject testObject)
7176
{
@@ -172,6 +177,21 @@ public enum Pet
172177
}
173178
}
174179

180+
public class CwEvent
181+
{
182+
[JsonPropertyName("rideId")]
183+
public string RideId { get; set; } = string.Empty;
184+
185+
[JsonPropertyName("riderId")]
186+
public string RiderId { get; set; } = string.Empty;
187+
188+
[JsonPropertyName("riderName")]
189+
public string RiderName { get; set; } = string.Empty;
190+
191+
[JsonPropertyName("correlationId")]
192+
public string? CorrelationId { get; set; }
193+
}
194+
175195
public class TestServiceHandler
176196
{
177197
public void LogWithEnv()

0 commit comments

Comments
 (0)