aws / aws-xray-sdk-go

AWS X-Ray SDK for the Go programming language.
Apache License 2.0
278 stars 117 forks source link

Unable to trace AWS sdk clients in lambda #252

Closed MatteoInfi closed 3 years ago

MatteoInfi commented 4 years ago

I have enabled tracing for my lambda functions and I would like to instrument all the downstream calls done by the AWS sdk. Following the docs: https://docs.aws.amazon.com/lambda/latest/dg/golang-tracing.html, I am not able to see any AWS service on the service map (API Gateway and Lambda are showing normally). Instead I get this errors from CloudWatch:


Suppressing AWS X-Ray context missing panic: failed to begin subsegment named 'dynamodb': segment cannot be found
Suppressing AWS X-Ray context missing panic: failed to begin subsegment named 'attempt': segment cannot be found.
Suppressing AWS X-Ray context missing panic: failed to begin subsegment named 'unmarshal': segment cannot be found.

This is an example of how I have instrumented DynamoDB


func NewDynamoDB() *dynamodb.DynamoDB {
    newSession := session.Must(session.NewSessionWithOptions(session.Options{
        Config: aws.Config{
            Region: aws.String(os.Getenv("MASTER_REGION")),
        },
    }))

    dynamoDBClient := dynamodb.New(newSession)
    xray.AWS(dynamoDBClient.Client)
    return dynamoDBClient
}

and then I simply call dynamodb.GetItem(...).

Any help is appreciated thanks.


Go runtime: go1.x XRay SDK: v1.1.0 AWS SDK: v1.32.0

shogo82148 commented 4 years ago

You need to use dynamoDBClient.GetItemWithContext https://docs.aws.amazon.com/sdk-for-go/api/service/dynamodb/#DynamoDB.GetItemWithContext with an AWS Lambda context. The document of the AWS Lambda context is here. https://docs.aws.amazon.com/lambda/latest/dg/golang-context.html

package main

import (
        "fmt"
        "context"

        "github.com/aws/aws-lambda-go/lambda"
        "github.com/aws/aws-sdk-go/aws"
        "github.com/aws/aws-sdk-go/aws/session"
        "github.com/aws/aws-sdk-go/service/dynamodb"
        "github.com/aws/aws-xray-sdk-go/xray"
)

var dynamoDBClient *dynamodb.DynamoDB

type MyEvent struct {
        Name string `json:"name"`
}

func HandleRequest(ctx context.Context, name MyEvent) (string, error) {
        output, err := dynamoDBClient.GetItemWithContext(ctx, &dynamodb.GetItemInput{
             // (snip)
        })
        if err != nil {
            return "", err
        }
        // use output here
        return fmt.Sprintf("Hello %s!", name.Name ), nil
}

func main() {
        newSession := session.Must(session.NewSession())
        dynamoDBClient = dynamodb.New(newSession)
        xray.AWS(dynamoDBClient.Client)
        lambda.Start(HandleRequest)
}
MatteoInfi commented 4 years ago

@shogo82148 thanks for the heads up. I just would like to say that this is undocumented in here: https://docs.aws.amazon.com/lambda/latest/dg/golang-tracing.html. There is no mention of using context.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs in next 7 days. Thank you for your contributions.

bhautikpip commented 3 years ago

Hi @MatteoInfi ,

I think this link has provided an example of using ListBucketsWithContext API to trace s3 calls and you can reference the similar example to trace dynamo db calls with context. Also, @shogo82148 has pointed the right API documentation for dynamodb. I feel the example is covered in the docs so resolving this issue. Feel free to open new one for any issues.

MatteoInfi commented 3 years ago

@bhautikpip yea, thanks, however that was added after I have opened this issue