Monitor AWS Lambda Golang with OpenTelemetry

Vladimir Mihailenco
July 30, 2024
3 min read

AWS Lambda is a serverless, event-driven compute service that lets you run code without provisioning or managing servers.

You can use OpenTelemetry with AWS Lambda to enable distributed tracing and observability for serverless applications.

What is OpenTelemetry?

OpenTelemetry is an open-source observability framework that aims to standardize and simplify the collection, processing, and export of telemetry data from applications and systems.

OpenTelemetry supports multiple programming languages and platforms, making it suitable for a wide range of applications and environments.

OpenTelemetry enables developers to instrument their code and collect telemetry data, which can then be exported to various OpenTelemetry backends or observability platforms for analysis and visualization. For Kubernetes-based deployments, the OpenTelemetry Operator automates the deployment and management of OpenTelemetry components, simplifying instrumentation at scale. Use the OpenTelemetry Kubernetes guide to align Lambda-produced telemetry with workloads running inside your clusters.

AWS Lambda containers

Lambda runs the code in isolated containers dynamically scaling the number of containers as needed. When there are no new requests, Lambda freezes idle containers.

When the container is frozen, all processes in the container will be frozen as well. If the process uses a timer to flush the buffered data, the timer won’t fire until the container thaws. The interval between the frozen and the thaw state is unpredictable, ranging from seconds to hours.

To mitigate that problem, the instrumentation has to flush data out before the Lambda function returns.

Installation

To install OpenTelemetry AWS Lambda instrumentation:

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

Usage

You can instrument your AWS Lambda function with OpenTelemetry like this:

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

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

To use otellambda with OpenTelemetry Go, you need to configure otellambda.WithFlusher with the tracer provider used by Uptrace.

go
import (
    "go.opentelemetry.io/contrib/propagators/aws/xray"
    "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda"
    "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda/xrayconfig"
    "github.com/uptrace/uptrace-go/uptrace"
)

func main() {
    ctx := context.Background()

    uptrace.ConfigureOpentelemetry(
        // copy your project DSN here or use UPTRACE_DSN env var
        //uptrace.WithDSN("<FIXME>"),

        uptrace.WithServiceName("myservice"),
        uptrace.WithServiceVersion("v1.0.0"),
    )
    defer uptrace.Shutdown(ctx)

    tp := uptrace.TracerProvider()
    lambda.Start(otellambda.InstrumentHandler(
        lambdaHandler(ctx),
        // Flush buffered spans to Uptrace.
        otellambda.WithFlusher(tp),
    ))
}

See aws-lambda example for details.

OpenTelemetry Lambda

Alternatively, you can add opentelemetry-lambda as a Lambda layer, but it does not offer any advantages at the moment. See the example for details.

What is Uptrace?

Uptrace is a OpenTelemetry APM that supports distributed tracing, metrics, and logs. You can use it to monitor applications and troubleshoot issues. For Go instrumentation, see the OpenTelemetry Go guide and compare with top APM tools.

Uptrace Overview

Uptrace comes with an intuitive query builder, rich dashboards, alerting rules with notifications, and integrations for most languages and frameworks.

Uptrace can process billions of spans and metrics on a single server and allows you to monitor your applications at 10x lower cost.

In just a few minutes, you can try Uptrace by visiting the cloud demo (no login required) or running it locally with Docker. The source code is available on GitHub.

What's next?

Your Go Lambda functions now have full observability with OpenTelemetry tracing for serverless architectures. Monitor cold starts, execution durations, and downstream service calls. For Node.js Lambda, check out Node Lambda instrumentation.