Skip to content

OpenTelemetry

Echo OpenTelemetry is a middleware that provides OpenTelemetry instrumentation for HTTP requests.

OpenTelemetry is a set of open-source tools that provide instrumentation for cloud-native applications.

Add the OpenTelemetry middleware dependency with Go modules:

Terminal window
go get github.com/labstack/echo-opentelemetry

Import the middleware and the OpenTelemetry trace API:

import (
echootel "github.com/labstack/echo-opentelemetry"
"go.opentelemetry.io/otel/trace"
)

Register it with full configuration:

e.Use(echootel.NewMiddlewareWithConfig(echootel.Config{
ServerName: "my-server",
TracerProvider: tp,
//Skipper: nil,
//OnNextError: nil,
//OnExtractionError: nil,
//MeterProvider: nil,
//Propagators: nil,
//SpanStartOptions: nil,
//SpanStartAttributes: nil,
//SpanEndAttributes: nil,
//MetricAttributes: nil,
//Metrics: nil,
}))

For configuration options, see the Config struct.

Add the middleware in simplified form by providing only the server name:

e.Use(echootel.NewMiddleware("app.example.com"))

Add the middleware with configuration options:

e.Use(echootel.NewMiddlewareWithConfig(echootel.Config{
TracerProvider: tp,
}))

Retrieve the tracer from the Echo context:

tracer, err := echo.ContextGet[trace.Tracer](c, echootel.TracerKey)

The example exports metrics and spans to stdout, but you can use any exporter (OTLP, etc.). See the OpenTelemetry exporters documentation.