Skip to content

Commit 29b9360

Browse files
committed
fix: tracing subsegment when using fluent api Tracing.WithSubsegment
1 parent b060344 commit 29b9360

File tree

3 files changed

+557
-2
lines changed

3 files changed

+557
-2
lines changed

examples/Tracing/src/HelloWorld/Function.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyReques
9191
gatewaySegment.AddAnnotation("AccountId", apigwProxyEvent.RequestContext.AccountId);
9292
gatewaySegment.AddMetadata("LookupRecord", lookupRecord);
9393

94+
Tracing.WithSubsegment("LoggingResponse",
95+
subsegment =>
96+
{
97+
subsegment.AddAnnotation("AccountId", apigwProxyEvent.RequestContext.AccountId);
98+
subsegment.AddMetadata("LookupRecord", lookupRecord);
99+
});
100+
101+
94102
try
95103
{
96104
await SaveRecordInDynamo(lookupRecord);

libraries/src/AWS.Lambda.Powertools.Tracing/Tracing.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,20 @@ public static void WithSubsegment(string nameSpace, string name, Action<TracingS
154154
XRayRecorder.Instance.SetNamespace(GetNamespaceOrDefault(nameSpace));
155155
try
156156
{
157-
var entity = XRayRecorder.Instance.GetEntity() as TracingSubsegment;
158-
subsegment?.Invoke(entity);
157+
var entity = XRayRecorder.Instance.GetEntity();
158+
var tracingSubsegment = new TracingSubsegment("## " + name);
159+
160+
// Copy properties from the current entity
161+
if (entity != null)
162+
{
163+
if (entity is Subsegment subsegmentEntity)
164+
{
165+
tracingSubsegment.Namespace = subsegmentEntity.Namespace;
166+
}
167+
tracingSubsegment.Sampled = entity.Sampled;
168+
}
169+
170+
subsegment?.Invoke(tracingSubsegment);
159171
}
160172
finally
161173
{

0 commit comments

Comments
 (0)