You can use the X-Ray SDK for Go with your Lambda function. If your handler includes AWS Lambda Context Object in Go as its first argument, that object can be passed to the X-Ray SDK. Lambda passes values through this context that the SDK can use to attach subsegments to the Lambda invoke service segment. Subsegments created with the SDK will appear as a part of your Lambda traces.
Use the following command to install the X-Ray SDK for Go. (The SDK's non-testing dependencies will be included).
go get -u github.com/aws/aws-xray-sdk-go/...
If you want to include the test dependencies, use the following command:
go get -u -t github.com/aws/aws-xray-sdk-go/...
You can also use Glide to manage dependencies.
glide install
The following code sample illustrates how to configure the X-Ray SDK for Go in your Lambda function:
import (
"github.com/aws/aws-xray-sdk-go/xray"
)
func myHandlerFunction(ctx context.Context, sample string) {
xray.Configure(xray.Config{
LogLevel: "info", // default
ServiceVersion: "1.2.3",
})
... //remaining handler code
}
The following code illustrates how to start a subsegment:
// Start a subsegment
ctx, subSeg := xray.BeginSubsegment(ctx, "subsegment-name")
// ...
// Add metadata or annotation here if necessary
// ...
subSeg.Close(nil)
The following code illustrates how to trace and capture a critical code path:
func criticalSection(ctx context.Context) {
// This example traces a critical code path using a custom subsegment
xray.Capture(ctx, "MyService.criticalSection", func(ctx1 context.Context) error {
var err error
section.Lock()
result := someLockedResource.Go()
section.Unlock()
xray.AddMetadata(ctx1, "ResourceResult", result)
})
}
You can also use the xray.Client()
method if you want to trace an HTTP client, as shown below:
func myFunction (ctx context.Context) ([]byte, error) {
resp, err := ctxhttp.Get(ctx, xray.Client(nil), "https://aws.amazon.com")
if err != nil {
return nil, err
}
return ioutil.ReadAll(resp.Body), nil
}