Closed MatteoInfi closed 3 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)
}
@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.
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.
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.
@bhautikpip yea, thanks, however that was added after I have opened this issue
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:
This is an example of how I have instrumented DynamoDB
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