Friday 30 April 2021

How to use AWS XRAY in asp.netcore?

This blog is for those who already know about distributed app development and its debugging and tracing.

And here will share simple steps for adding Xray in the Dotnet core application.

  • Install NuGet package  AWSXRayRecorder.Handlers.AwsSdk
  • Modify Startup implementation 
    public Startup(IConfiguration configuration)
    {
        AWSXRayRecorder.InitializeInstance(configuration: configuration);
        AWSSDKHandler.RegisterXRayForAllServices(); }
  • To trace incoming request, add the following in Configure method on top
    app.UseXRay("ApplicationName");
    
  • To trace HTTP client add
    services.AddTransient<HttpClientXRayTracingHandler>();
                services.AddHttpClient<ICustomClient, CustomClient>(client =>
                {
                    //..
                })
                .AddHttpMessageHandler<HttpClientXRayTracingHandler>();
  • To add metadata annotation
    AWSXRayRecorder.Instance.AddAnnotation("request_id", "R001");
    
  • If you are using a serverless.template for stack formation, to auto-enable tracing add
    • "Tracing": "Active" in AWS::Serverless::Function > Properties

There are more you can play with x-ray and dotnetcore app, reference .