aws-xray

Logging background processes with AWS X-Ray?


How can I log background operations with Amazon X-Ray?

  1. I tried to create new Segment using Instance.BeginSegment
  2. I also tried to create new Recorder and create Segment in it

I am not seeing any errors, and also I am not seeing any data logged.

I am using dotnet core SDK but I guess solution will be similar with any other SDK.


Solution

  • The solution is to use AWSXRayRecorderBuilder class, like this:

    var xRayTracing = new AWSXRayRecorderBuilder()
        .WithContextMissingStrategy(Amazon.XRay.Recorder.Core.Strategies.ContextMissingStrategy.LOG_ERROR)
        .WithTraceContext(new AsyncLocalContextContainer())
        .Build();
    .... 
    

    You may also consider to fill HttmlInformation so it will be visible on Races list view in AWS Console:

    var requestInformation = new Dictionary<string, object>
    {
        ["url"] = "BackgroundProcessor",
        ["method"] = message.State
    };
    xRayTracing.AddHttpInformation("request", requestInformation);
                
    

    and an actual logging could be wrapped in try/catch/finally:

    try
    {
        // some actions here
    }
    catch (Exception ex)
    {
        xRayTracing.AddException(ex);
        throw;
    }
    finally {
        xRayTracing.EndSegment(); 
    }