Skip to content

Commit 01a8eec

Browse files
authored
Merge pull request #428 from amirkaws/automatic-xray-register
feat: Automatic Register XRay for all services
2 parents e241ca9 + 6d7a757 commit 01a8eec

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

docs/core/tracing.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,5 +244,36 @@ under a subsegment, or you are doing multithreaded programming. Refer examples b
244244

245245
## Instrumenting SDK clients and HTTP calls
246246

247-
User should make sure to instrument the SDK clients explicitly based on the function dependency. Refer details on
248-
[how to instrument SDK client with Xray](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-dotnet-sdkclients.html) and [outgoing http calls](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-dotnet-httpclients.html).
247+
You should make sure to instrument the SDK clients explicitly based on the function dependency. You can instrument all of your AWS SDK for .NET clients by calling RegisterForAllServices before you create them.
248+
249+
=== "Function.cs"
250+
251+
```c# hl_lines="14"
252+
using Amazon.DynamoDBv2;
253+
using Amazon.DynamoDBv2.Model;
254+
using AWS.Lambda.Powertools.Tracing;
255+
256+
public class Function
257+
{
258+
private static IAmazonDynamoDB _dynamoDb;
259+
260+
/// <summary>
261+
/// Function constructor
262+
/// </summary>
263+
public Function()
264+
{
265+
Tracing.RegisterForAllServices();
266+
267+
_dynamoDb = new AmazonDynamoDBClient();
268+
}
269+
}
270+
```
271+
272+
To instrument clients for some services and not others, call Register instead of RegisterForAllServices. Replace the highlighted text with the name of the service's client interface.
273+
274+
```c#
275+
Tracing.Register<IAmazonDynamoDB>()
276+
```
277+
278+
This functionality is a thin wrapper for AWS X-Ray .NET SDK. Refer details on [how to instrument SDK client with Xray](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-dotnet-sdkclients.html) and [outgoing http calls](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-dotnet-httpclients.html).
279+

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
</ItemGroup>
3131

3232
<ItemGroup>
33-
<PackageReference Include="AWSSDK.XRay" Version="3.7.102.25" />
33+
<PackageReference Include="AWSSDK.XRay" Version="3.7.200.34" />
3434
<PackageReference Include="AWSXRayRecorder.Core" Version="2.14.0" />
35+
<PackageReference Include="AWSXRayRecorder.Handlers.AwsSdk" Version="2.12.0" />
3536
</ItemGroup>
3637
<ItemGroup>
3738
<None Include="README.md" Pack="true" PackagePath="\" />

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,15 @@ public void OnEntry(AspectEventArgs eventArgs)
117117
if (_captureAnnotations)
118118
{
119119
_xRayRecorder.AddAnnotation("ColdStart", _isColdStart);
120-
121-
_isColdStart = false;
120+
122121
_captureAnnotations = false;
123122
_isAnnotationsCaptured = true;
124123

125124
if (_powertoolsConfigurations.IsServiceDefined)
126125
_xRayRecorder.AddAnnotation("Service", _powertoolsConfigurations.Service);
127126
}
127+
128+
_isColdStart = false;
128129
}
129130

130131
/// <summary>

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System;
1717
using Amazon.XRay.Recorder.Core.Internal.Entities;
18+
using Amazon.XRay.Recorder.Handlers.AwsSdk;
1819
using AWS.Lambda.Powertools.Common;
1920
using AWS.Lambda.Powertools.Tracing.Internal;
2021

@@ -239,4 +240,20 @@ private static string GetNamespaceOrDefault(string nameSpace)
239240

240241
return PowertoolsConfigurations.Instance.Service;
241242
}
243+
244+
/// <summary>
245+
/// Registers X-Ray for all instances of <see cref="Amazon.Runtime.AmazonServiceClient"/>.
246+
/// </summary>
247+
public static void RegisterForAllServices()
248+
{
249+
AWSSDKHandler.RegisterXRayForAllServices();
250+
}
251+
252+
/// <summary>
253+
/// Registers X-Ray for the given type of <see cref="Amazon.Runtime.AmazonServiceClient"/>.
254+
/// </summary>
255+
public static void Register<T>()
256+
{
257+
AWSSDKHandler.RegisterXRay<T>();
258+
}
242259
}

0 commit comments

Comments
 (0)