Pular para o conteúdo

OpenTelemetry

Echo OpenTelemetry é um middleware que fornece instrumentação OpenTelemetry para requests HTTP.

OpenTelemetry é um conjunto de ferramentas open-source que fornecem instrumentação para aplicações cloud-native.

Adicione a dependência do middleware OpenTelemetry com Go modules:

Janela do terminal
go get github.com/labstack/echo-otel/v5

Importe o middleware e a API de trace do OpenTelemetry:

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

Registre-o com configuração 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 opções de configuração, veja a struct Config.

Adicione o middleware de forma simplificada fornecendo apenas o nome do servidor:

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

Adicione o middleware com opções de configuração:

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

Recupere o tracer a partir do contexto do Echo:

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

O exemplo exporta spans para stdout, mas você pode usar qualquer exporter (OTLP etc.). Veja a documentação de OpenTelemetry exporters.

Config · github.com/labstack/echo-otel/[email protected]

As descrições dos campos são geradas dos comentários do código-fonte em inglês.

Campos do código-fonte
CampoTipoDescriçãoCódigo
ServerNamestringServerName 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
Skippermiddleware.SkipperSkipper defines a function to skip middleware.L53
OnNextErrorOnErrorFuncOnNextError is used to specify how errors returned from the next middleware / handler are handled.L56
OnExtractionErrorOnErrorFuncOnExtractionError is used to specify how errors returned from request extraction are handled.L59
TracerProvideroteltrace.TracerProviderTracerProvider allows overriding the default tracer provider.L62
MeterProvidermetric.MeterProviderMeterProvider allows overriding the default meter provider.L65
Propagatorspropagation.TextMapPropagatorPropagators allow overriding the default propagators.L68
SpanStartOptions[]oteltrace.SpanStartOptionSpanStartOptions configures an additional set of trace.SpanStartOptions, which are applied to each new span.L71
SpanStartAttributesAttributesFuncSpanStartAttributes is used to extract additional attributes from the echo.Context and return them as a slice of attribute.KeyValue.L75
SpanEndAttributesAttributesFuncSpanEndAttributes is used to extract additional attributes from the echo.Context and return them as a slice of attribute.KeyValue.L79
MetricAttributesMetricAttributesFuncMetricAttributes is used to compose attributes just before Metrics.Record call.L82
MetricsMetricsRecorderMetrics is used to record custom metrics instead of default.L85
Funções do código-fonte
TipoCódigo
func NewMiddleware(serverName string) echo.MiddlewareFuncL105
func NewMiddlewareWithConfig(config Config) echo.MiddlewareFuncL110