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

# README

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

OptionsInput TypeDescriptionDefault
WithTracerProvidertrace.TracerProviderProvide a custom TracerProvider for creating spans. Consider using the AWS Lambda Resource Detector with your tracer provider to improve tracing information.otel.GetTracerProvider()
WithFlusherotellambda.FlusherThis 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
WithEventToCarrierfunc(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
WithPropagatorpropagation.PropagatorThe 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 propagation.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{})))
}

Useful links

License

Apache 2.0 - See LICENSE for more information.

# Packages

No description provided by the author
Package test validates AWS Lambda instrumentation with the default SDK.
No description provided by the author

# Functions

InstrumentHandler Provides a lambda handler which wraps customer lambda handler with OTel Tracing.
SemVersion is the semantic version to be supplied to tracer/meter creation.
Version is the current release version of the AWS Lambda instrumentation.
WithEventToCarrier sets the used EventToCarrier.
WithFlusher sets the used flusher.
WithPropagator configures the propagator used by the instrumentation.
WithTracerProvider configures the TracerProvider used by the instrumentation.
WrapHandler Provides a Handler which wraps customer Handler with OTel Tracing.

# Constants

ScopeName is the instrumentation scope name.

# Interfaces

A Flusher dictates how the instrumentation will attempt to flush unexported spans at the end of each Lambda innovation.
Option applies a configuration option.

# Type aliases

An EventToCarrier function defines how the instrumentation should prepare a TextMapCarrier for the configured propagator to read from.