opentelemetry-go-contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda
opentelemetrybot d79a098204 dependabot updates Sun Nov 19 17:53:56 UTC 2023
build(deps): bump github.com/aws/aws-sdk-go from 1.47.9 to 1.48.0 in /detectors/aws/ec2
build(deps): bump github.com/aws/aws-sdk-go-v2/service/s3 from 1.42.1 to 1.43.0 in /instrumentation/github.com/aws/aws-lambda-go/otellambda/example
build(deps): bump github.com/aws/aws-sdk-go-v2/config from 1.23.0 to 1.25.3 in /instrumentation/github.com/aws/aws-lambda-go/otellambda/example
build(deps): bump k8s.io/client-go from 0.28.3 to 0.28.4 in /detectors/aws/eks
build(deps): bump k8s.io/apimachinery from 0.28.3 to 0.28.4 in /detectors/aws/eks
build(deps): bump github.com/aws/smithy-go from 1.16.0 to 1.17.0 in /instrumentation/github.com/aws/aws-sdk-go-v2/otelaws
build(deps): bump github.com/aws/aws-sdk-go-v2/service/sqs from 1.28.0 to 1.28.1 in /instrumentation/github.com/aws/aws-sdk-go-v2/otelaws
build(deps): bump github.com/aws/aws-sdk-go-v2 from 1.22.2 to 1.23.0 in /instrumentation/github.com/aws/aws-sdk-go-v2/otelaws
build(deps): bump github.com/aws/aws-sdk-go-v2/config from 1.23.0 to 1.25.3 in /instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/example
build(deps): bump github.com/aws/aws-sdk-go-v2/service/dynamodb from 1.25.1 to 1.25.2 in /instrumentation/github.com/aws/aws-sdk-go-v2/otelaws
build(deps): bump github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp from 1.20.0 to 1.21.0 in /detectors/gcp
build(deps): bump github.com/aws/aws-sdk-go-v2/service/s3 from 1.42.1 to 1.43.0 in /instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/example
build(deps): bump github.com/aws/aws-sdk-go-v2 from 1.22.2 to 1.23.0 in /instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/example
build(deps): bump github.com/aws/aws-sdk-go-v2/service/dynamodb from 1.25.1 to 1.25.2 in /instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/example
2023-11-19 17:53:56 +00:00
..
example dependabot updates Sun Nov 19 17:53:56 UTC 2023 2023-11-19 17:53:56 +00:00
test Release 1.21.1/0.46.1/0.15.1/0.1.1 (#4582) 2023-11-16 22:51:55 +01:00
xrayconfig Release 1.21.1/0.46.1/0.15.1/0.1.1 (#4582) 2023-11-16 22:51:55 +01:00
README.md AWS X-Ray config module for Lambda instrumentation (#984) 2021-10-26 19:49:47 -04:00
config.go Mirror golangci-lint config from opentelemetry-go (#2297) 2022-05-24 07:01:50 -07:00
doc.go End to end tracing support - Lambda (#983) 2021-10-25 12:50:58 -04:00
go.mod Release 1.21.1/0.46.1/0.15.1/0.1.1 (#4582) 2023-11-16 22:51:55 +01:00
go.sum Release 1.21.1/0.46.1/0.15.1/0.1.1 (#4582) 2023-11-16 22:51:55 +01:00
lambda.go Expose instrumentation scope name (#4448) 2023-11-08 18:24:06 +01:00
lambda_test.go Add missing recommended resource attributes to AWS Lambda detector (#3148) 2023-01-20 08:21:16 -08:00
version.go Release 1.21.1/0.46.1/0.15.1/0.1.1 (#4582) 2023-11-16 22:51:55 +01:00
wrapHandler.go Mirror golangci-lint config from opentelemetry-go (#2297) 2022-05-24 07:01:50 -07:00
wrapLambdaHandler.go Mirror golangci-lint config from opentelemetry-go (#2297) 2022-05-24 07:01:50 -07:00

README.md

OpenTelemetry AWS Lambda Instrumentation for Golang

Go Reference Apache License

This module provides instrumentation for AWS Lambda.

Installation

go get -u go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda

example

See ./example

Usage

Create a sample Lambda Go application such as below.

package main

import (
	"context"
	"fmt"
	"github.com/aws/aws-lambda-go/lambda"
)

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

func HandleRequest(ctx context.Context, name MyEvent) (string, error) {
	return fmt.Sprintf("Hello %s!", name.Name ), nil
}

func main() {
	lambda.Start(HandleRequest)
}

Now use the provided wrapper to instrument your basic Lambda function:

// Add import
import "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda"

// wrap lambda handler function
func main() {
	lambda.Start(otellambda.InstrumentHandler(HandleRequest))
}

AWS Lambda Instrumentation Options

Options Input Type Description Default
WithTracerProvider trace.TracerProvider Provide a custom TracerProvider for creating spans. Consider using the AWS Lambda Resource Detector with your tracer provider to improve tracing information. otel.GetTracerProvider()
WithFlusher otellambda.Flusher This instrumentation will call the ForceFlush method of its Flusher at the end of each invocation. Should you be using asynchronous logic (such as sddktrace's BatchSpanProcessor) it is very import for spans to be ForceFlush'ed before Lambda freezes to avoid data delays. Flusher with noop ForceFlush
WithEventToCarrier func(eventJSON []byte) propagation.TextMapCarrier{} Function for providing custom logic to support retrieving trace header from different event types that are handled by AWS Lambda (e.g., SQS, CloudWatch, Kinesis, API Gateway) and returning them in a propagation.TextMapCarrier which a Propagator can use to extract the trace header into the context. Function which returns an empty TextMapCarrier - new spans will be part of a new Trace and have no parent past Lambda instrumentation span
WithPropagator propagation.Propagator The Propagator the instrumentation will use to extract trace information into the context. otel.GetTextMapPropagator()

Usage With Options Example

var someHeaderKey = "Key" // used by propagator and EventToCarrier function to identify trace header

type mockHTTPRequest struct {
	Headers map[string][]string
	Body string
}

func mockEventToCarrier(eventJSON []byte) propagation.TextMapCarrier{
	var request mockHTTPRequest
	_ = json.unmarshal(eventJSON, &request)
	return propogation.HeaderCarrier{someHeaderKey: []string{request.Headers[someHeaderKey]}}
}

type mockPropagator struct{}
// Extract - read from `someHeaderKey`
// Inject
// Fields

func HandleRequest(ctx context.Context, request mockHTTPRequest) error {
	return fmt.Sprintf("Hello %s!", request.Body ), nil
}

func main() {
	exp, _ := stdouttrace.New()
    
	tp := sdktrace.NewTracerProvider(
		    sdktrace.WithBatcher(exp))
	
	lambda.Start(otellambda.InstrumentHandler(HandleRequest,
		                                    otellambda.WithTracerProvider(tp),
		                                    otellambda.WithFlusher(tp),
		                                    otellambda.WithEventToCarrier(mockEventToCarrier),
		                                    otellambda.WithPropagator(mockPropagator{})))
}

License

Apache 2.0 - See LICENSE for more information.