AWS.Lambda.Powertools.Tracing
v0.0.2-previewPre-release
net6.0
AWS Lambda Powertools for .NET - Tracing package.
Published
Jan 18, 2023
3y ago
Total Downloads
1.3M
1,272,210
Install
dotnet add package AWS.Lambda.Powertools.Tracing --version 0.0.2-preview
| Product | Versions |
|---|---|
| .NET |
README
AWS.Lambda.Powertools.Tracing
Powertools tracing is an opinionated thin wrapper for AWS X-Ray .NET SDK a provides functionality to reduce the overhead of performing common tracing tasks.
Key Features
- Helper methods to improve the developer experience for creating custom AWS X-Ray subsegments.
- Capture cold start as annotation.
- Capture function responses and full exceptions as metadata.
- Better experience when developing with multiple threads.
- Auto-patch supported modules by AWS X-Ray
Read the docs
For a full list of features go to awslabs.github.io/aws-lambda-powertools-dotnet/core/tracing/
GitHub: https://github.com/awslabs/aws-lambda-powertools-dotnet/
Sample Function
View the full example here: https://github.com/awslabs/aws-lambda-powertools-dotnet/tree/develop/examples/Tracing
public class Function
{
/// <summary>
/// Lambda Handler
/// </summary>
/// <param name="apigwProxyEvent">API Gateway Proxy event</param>
/// <param name="context">AWS Lambda context</param>
/// <returns>API Gateway Proxy response</returns>
[Logging(LogEvent = true)]
[Tracing(CaptureMode = TracingCaptureMode.ResponseAndError)]
public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest apigwProxyEvent,
ILambdaContext context)
{
var requestContextRequestId = apigwProxyEvent.RequestContext.RequestId;
Logger.LogInformation("Getting ip address from external service");
var location = await GetCallingIp().ConfigureAwait(false);
var lookupRecord = new LookupRecord(lookupId: requestContextRequestId,
greeting: "Hello AWS Lambda Powertools for .NET", ipAddress: location);
// Trace Fluent API
Tracing.WithSubsegment("LoggingResponse",
subsegment =>
{
subsegment.AddAnnotation("AccountId", apigwProxyEvent.RequestContext.AccountId);
subsegment.AddMetadata("LookupRecord", lookupRecord);
});
try
{
await SaveRecordInDynamo(lookupRecord);
return new APIGatewayProxyResponse
{
Body = JsonSerializer.Serialize(lookupRecord),
StatusCode = 200,
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
};
}
catch (Exception e)
{
Logger.LogError(e.Message);
return new APIGatewayProxyResponse
{
Body = e.Message,
StatusCode = 500,
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
};
}
}
/// <summary>
/// Calls location api to return IP address
/// </summary>
/// <returns>IP address string</returns>
[Tracing(SegmentName = "Location service")]
private static async Task<string?> GetCallingIp()
{
if (_httpClient == null) return "0.0.0.0";
_httpClient.DefaultRequestHeaders.Accept.Clear();
_httpClient.DefaultRequestHeaders.Add("User-Agent", "AWS Lambda .Net Client");
try
{
Logger.LogInformation("Calling Check IP API");
var response = await _httpClient.GetStringAsync("https://checkip.amazonaws.com/").ConfigureAwait(false);
var ip = response.Replace("\n", "");
Logger.LogInformation($"API response returned {ip}");
return ip;
}
catch (Exception ex)
{
Logger.LogError(ex);
throw;
}
}
/// <summary>
/// Saves the lookup record in DynamoDB
/// </summary>
/// <param name="lookupRecord">Instance of LookupRecord</param>
/// <returns>A Task that can be used to poll or wait for results, or both.</returns>
[Tracing(SegmentName = "DynamoDB")]
private static async Task SaveRecordInDynamo(LookupRecord lookupRecord)
{
try
{
Logger.LogInformation($"Saving record with id {lookupRecord.LookupId}");
await _dynamoDbContext?.SaveAsync(lookupRecord)!;
}
catch (AmazonDynamoDBException e)
{
Logger.LogCritical(e.Message);
throw;
}
}
}
Sample output

Changes from Previous Version
No structural changes between v0.0.1-preview.1 and v0.0.2-preview.
Adoption Assessment
A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.
This version is marked prerelease. Validate its API and upgrade path before production use.Compatibility metadata lists 1 target framework; confirm the exact target used by your application.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.