X-Ray for a lambda function on Golang #2074
-
Hi there, I have some lambda functions written in Go 1.20 and aws-sdk-go-v2 to interact with services like Cognito, SQS, StepFunctions, among others. I am using SAM to deploy the functions, so I activate the tracing and with it, I can see API Gateway and Lambda in the X-Ray service map and also the transactions, but I cannot see the other AWS services. I've been researching and trying to add X-Ray to AWS service clients, but haven't succeeded yet. Should I use the xray module in aws-sdk-go-v2 or should I use github.com/aws/aws-xray-sdk-go? The following is an example of what I am trying to do:
I really appreciate your support on this. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi @jledesma-opsguru , The Xray client offered by aws-sdk-go-v2 is for managing debug traces and retrieving service maps and other data created by processing those traces. If you are trying to create traces for different clients used by your application, you need to use the Xray SDK. To instrument go sdk v2 calls youll need to use different syntax. Here is a snippet from their official docs: package main
import (
"context"
"log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-xray-sdk-go/instrumentation/awsv2"
"github.com/aws/aws-xray-sdk-go/xray"
)
func main() {
// Create a segment
ctx, root := xray.BeginSegment(context.TODO(), "AWSSDKV2_Dynamodb")
defer root.Close(nil)
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion("us-west-2"))
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
// Instrumenting AWS SDK v2
awsv2.AWSV2Instrumentor(&cfg.APIOptions)
// Using the Config value, create the DynamoDB client
svc := dynamodb.NewFromConfig(cfg)
// Build the request with its input parameters
_, err = svc.ListTables(ctx, &dynamodb.ListTablesInput{
Limit: aws.Int32(5),
})
if err != nil {
log.Fatalf("failed to list tables, %v", err)
}
} I know its kind of confusing because there are two products using the same names but for instrumentation questions, feel free to write the Xray team on their public github https://github.com/aws/aws-xray-sdk-go If you have questions about how to use the aws-sdk for go. This is the right spot :) Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
Hello! Reopening this discussion to make it searchable. |
Beta Was this translation helpful? Give feedback.
Hi @jledesma-opsguru ,
The Xray client offered by aws-sdk-go-v2 is for managing debug traces and retrieving service maps and other data created by processing those traces.
If you are trying to create traces for different clients used by your application, you need to use the Xray SDK.
Seeing your code, you are trying to instrument your calls using the syntax for v1 instrumentation.
To instrument go sdk v2 calls youll need to use different syntax. Here is a snippet from their official docs: