OpenTelemetry
Echo OpenTelemetry es un middleware que proporciona instrumentación OpenTelemetry para requests HTTP.
OpenTelemetry es un conjunto de herramientas open-source que proporcionan instrumentación para aplicaciones cloud-native.
Agrega la dependencia del middleware OpenTelemetry con Go modules:
go get github.com/labstack/echo-otel/v5Importa el middleware y la API de tracing de OpenTelemetry:
import ( echootel "github.com/labstack/echo-otel/v5" "go.opentelemetry.io/otel/trace")Regístralo con configuración completa:
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,}))Para opciones de configuración, consulta el struct
Config.
Agrega el middleware en forma simplificada proporcionando solo el nombre del servidor:
e.Use(echootel.NewMiddleware("app.example.com"))Agrega el middleware con opciones de configuración:
e.Use(echootel.NewMiddlewareWithConfig(echootel.Config{ TracerProvider: tp,}))Obtén el tracer desde el contexto de Echo:
tracer, err := echo.ContextGet[trace.Tracer](c, echootel.TracerKey)Ejemplo
Sección titulada «Ejemplo»El ejemplo exporta spans a stdout, pero puedes usar cualquier exporter (OTLP, etc.). Consulta la documentación de OpenTelemetry exporters.
Referencia de la API
Sección titulada «Referencia de la API»Config · github.com/labstack/echo-otel/[email protected]
Las descripciones de los campos se generan a partir de comentarios del código fuente en inglés.
| Campo | Tipo | Descripción | Fuente |
|---|---|---|---|
ServerName | string | ServerName is set as `server.address` and `server.port` span attributes. They are not added to metrics by default (Opt-In in the semantic conventions); add them with MetricAttributes if needed. Example: "api.example.com" or "example.com:8080". Without a port, `server.port` is not set. If known, this value must be set to the server’s canonical (primary) name. For example, in Apache this corresponds to the ServerName directive (https://httpd.apache.org/docs/2.4/mod/core.html#servername), and in NGINX to the server_name directive (http://nginx.org/en/docs/http/ngx_http_core_module.html#server_name). More generally, the primary server name is the host header value that maps to the HTTP server’s default virtual host. It must include the hostname, and if the server is accessed via a non-default port, the port must be appended using the standard ":port" suffix. If the primary server name is unknown, this field should be set to an empty string. In that case, Request.Host will be used to resolve the effective server name and port. Request.Host is chosen by the client. | L50 |
Skipper | middleware.Skipper | Skipper defines a function to skip middleware. | L53 |
OnNextError | OnErrorFunc | OnNextError is used to specify how errors returned from the next middleware / handler are handled. | L56 |
OnExtractionError | OnErrorFunc | OnExtractionError is used to specify how errors returned from request extraction are handled. | L59 |
TracerProvider | oteltrace.TracerProvider | TracerProvider allows overriding the default tracer provider. | L62 |
MeterProvider | metric.MeterProvider | MeterProvider allows overriding the default meter provider. | L65 |
Propagators | propagation.TextMapPropagator | Propagators allow overriding the default propagators. | L68 |
SpanStartOptions | []oteltrace.SpanStartOption | SpanStartOptions configures an additional set of trace.SpanStartOptions, which are applied to each new span. | L71 |
SpanStartAttributes | AttributesFunc | SpanStartAttributes is used to extract additional attributes from the echo.Context and return them as a slice of attribute.KeyValue. | L75 |
SpanEndAttributes | AttributesFunc | SpanEndAttributes is used to extract additional attributes from the echo.Context and return them as a slice of attribute.KeyValue. | L79 |
MetricAttributes | MetricAttributesFunc | MetricAttributes is used to compose attributes just before Metrics.Record call. | L82 |
Metrics | MetricsRecorder | Metrics is used to record custom metrics instead of default. | L85 |