Search code examples
springamazon-web-servicesaws-lambdaaws-api-gatewayaws-xray

Spring rest template with aws x-ray


I would like to trace calls between spring micro services deployed as aws lambdas using aws x-ray.

The set up is as follows:

  1. Microservice A with api endpoint deployed as aws lambda

  2. Microservice B with api endpoint deployed as aws lambda calling microservice A via https

Both microservices include the aws dependencies for xray:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-xray-recorder-sdk-bom</artifactId>
            <version>1.2.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-xray-recorder-sdk-core</artifactId>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-xray-recorder-sdk-apache-http</artifactId>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-xray-recorder-sdk-aws-sdk</artifactId>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-xray-recorder-sdk-aws-sdk-instrumentor</artifactId>
    </dependency>
</dependencies>

For both microservices, tracing has been enabled via the serverless application model sam.yaml file:

Resources:
  FunctionA:
    Type: AWS::Serverless::Function
    Properties:
      Handler: example.HandlerA::handleRequest
      Runtime: java8
      CodeUri: target/foo.jar
      MemorySize: 512
      Tracing: Active
      Policies:
        - AWSLambdaBasicExecutionRole
        - AWSXrayWriteOnlyAccess 
      Timeout: 20
      Events:
        GetResource:
          Type: Api
          Properties:
            Path: /{proxy+}
            Method: any

While I can see the traces for the individual calls for the services A and B in the x-ray web interface, a call of B over A does not appear as a compound trace.

Any ideas? Probably I need to instantiate a servlet filter. Just including the dependencies will not be enough, correct?


Solution

  • At this time, Amazon API Gateway is not propagating x-amzn-trace-id headers.

    It sounds as if your AWS Lambda functions may be deployed behind API Gateway endpoints. Because of this limitation in API Gateway's integration with AWS X-Ray, tracing this operation as one continuous trace will not be possible today.

    One approach for tracing this operation in a single continuous trace is having microservice B invoke microservice A directly using the AWS Lambda Invoke API. This API does respect the x-amzn-trace-id header (which would be automatically added to the Invoke request, as you've included the aws-xray-recorder-sdk-aws-sdk-instrumentor artifact in your project).