Documentation
¶
Overview ¶
Package tracer adds OpenTelemetry route spans to the Lambda HTTP router.
New adapts a core Tracer instance to HTTP middleware; Options controls response capture. Register it before compression middleware to observe the headers left by compression. Handler code receives the route span context.
Span and provider ownership ¶
The middleware ends route spans but leaves flushing and provider shutdown to the Lambda wrapper or application. It skips HTTP streaming; compose the core Tracer WrapHandler inside Streamify for the complete streaming invocation. Use an OpenTelemetry backend and a collector's awsxray exporter for X-Ray.
This separate module keeps tracing dependencies out of the core HTTP router.
For usage and compatibility details, see the user guide.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
func New(t *powertracer.Tracer, options ...Options) httpapi.Middleware
New records an internal route span using the supplied OTel-backed Tracer. It skips HTTP streaming, matching the reference. Use WrapHandler inside Streamify for a span covering the entire streaming invocation instead. Register it before Compress to observe headers left by that middleware. The Lambda wrapper or application owns flushing and provider shutdown.
Example ¶
package main
import (
"context"
"encoding/json"
"fmt"
httpapi "github.com/rambow-cloud/powertools-lambda-go/eventhandler/http"
httptracer "github.com/rambow-cloud/powertools-lambda-go/eventhandler/http/tracer"
"github.com/rambow-cloud/powertools-lambda-go/tracer"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
func main() {
provider := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
defer func() { _ = provider.Shutdown(context.Background()) }()
t, err := tracer.New(tracer.WithServiceName("orders"),
tracer.WithBackend(tracer.NewOTelBackend(provider)), tracer.WithLocalTracing(true))
if err != nil {
panic(err)
}
app := httpapi.New(httpapi.Options{})
app.Use(httptracer.New(t))
// Environment flags can disable tracing without changing the HTTP response.
if err := app.Get("/health", func(*httpapi.RequestContext) (any, error) {
return "ok", nil
}); err != nil {
panic(err)
}
event := json.RawMessage(`{"version":"2.0","routeKey":"$default","rawPath":"/health","rawQueryString":"","headers":{},"requestContext":{"http":{"method":"GET"},"domainName":"api.example.test"},"isBase64Encoded":false}`)
response, err := app.Resolve(context.Background(), event)
fmt.Println(response.StatusCode, response.Body, err)
}
Output: 200 "ok" <nil>