How to initialize AWS XRay in Lambda C# project

441 Views Asked by At

I have a c# lambda project and would like to now set up the project for AWS XRay.

My project contains a startup class and within ConfigureServices, Ive added

    private static IServiceCollection ConfigureServices(IConfigurationRoot root)
    {
        AWSXRayRecorder.InitializeInstance(root);
....
    }

Unfortunately, since this is a lambda project, there is no

Configure(IApplicationBuilder app, IWebHostEnvironment env)

What other code changes to Startup.cs need to be made in order to get the XRay working?

1

There are 1 best solutions below

0
On

If you're missing Configure method in Startup file, you may try configure app.UseXRay() in CreateHostBuilder() in Program file.

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                .Configure(app =>
                {
                    app.UseXRay("SampleApp")
                });
            });
        });

Please note, the order of AWSXRayMiddleware in the HTTP request pipeline matters, so you may need to make sure you've configured the pipeline properly.