Debian Codegraph @

instrumentation/github.com/labstack/echo/otelecho/config.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otelecho // import "go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho"
5
6 import (
7 "net/http"
8
9 "github.com/labstack/echo/v4"
10 "github.com/labstack/echo/v4/middleware"
11 "go.opentelemetry.io/otel/attribute"
12 "go.opentelemetry.io/otel/metric"
13 "go.opentelemetry.io/otel/propagation"
14 oteltrace "go.opentelemetry.io/otel/trace"
15 )
16
17 // config is used to configure the mux middleware.
18 type config struct {
19 TracerProvider oteltrace.TracerProvider
20 MeterProvider metric.MeterProvider
21 Propagators propagation.TextMapPropagator
22 Skipper middleware.Skipper
23 MetricAttributeFn MetricAttributeFn
24 EchoMetricAttributeFn EchoMetricAttributeFn
25 OnError OnErrorFn
26 }
27
28 // MetricAttributeFn is used to extract additional attributes from the http.Request
29 // and return them as a slice of attribute.KeyValue.
30 type MetricAttributeFn func(*http.Request) []attribute.KeyValue
31
32 // EchoMetricAttributeFn is used to extract additional attributes from the echo.Context
33 // and return them as a slice of attribute.KeyValue.
34 type EchoMetricAttributeFn func(echo.Context) []attribute.KeyValue
35
36 // OnErrorFn is used to specify how errors are handled in the middleware.
37 type OnErrorFn func(echo.Context, error)
38
39 // defaultOnError is the default function called when an error occurs during request processing.
40 // Note: it makes the global error handler run twice.
41 var defaultOnError = func(c echo.Context, err error) { c.Error(err) }
42
43 // Option specifies instrumentation configuration options.
44 type Option interface {
45 apply(*config)
46 }
47
48 type optionFunc func(*config)
49
50 func (o optionFunc) apply(c *config) {
51 o(c)
52 }
53
54 // WithPropagators specifies propagators to use for extracting
55 // information from the HTTP requests. If none are specified, global
56 // ones will be used.
57 func WithPropagators(propagators propagation.TextMapPropagator) Option {
58 return optionFunc(func(cfg *config) {
59 if propagators != nil {
60 cfg.Propagators = propagators
61 }
62 })
63 }
64
65 // WithMeterProvider specifies a meter provider to use for creating a meter.
66 // If none is specified, the global provider is used.
67 func WithMeterProvider(provider metric.MeterProvider) Option {
68 return optionFunc(func(cfg *config) {
69 if provider != nil {
70 cfg.MeterProvider = provider
71 }
72 })
73 }
74
75 // WithTracerProvider specifies a tracer provider to use for creating a tracer.
76 // If none is specified, the global provider is used.
77 func WithTracerProvider(provider oteltrace.TracerProvider) Option {
78 return optionFunc(func(cfg *config) {
79 if provider != nil {
80 cfg.TracerProvider = provider
81 }
82 })
83 }
84
85 // WithSkipper specifies a skipper for allowing requests to skip generating spans.
86 func WithSkipper(skipper middleware.Skipper) Option {
87 return optionFunc(func(cfg *config) {
88 cfg.Skipper = skipper
89 })
90 }
91
92 // WithMetricAttributeFn specifies a function that extracts additional attributes from the http.Request
93 // and returns them as a slice of attribute.KeyValue.
94 //
95 // If attributes are duplicated between this method and `WithEchoMetricAttributeFn`, the attributes in this method will be overridden.
96 func WithMetricAttributeFn(f MetricAttributeFn) Option {
97 return optionFunc(func(cfg *config) {
98 cfg.MetricAttributeFn = f
99 })
100 }
101
102 // WithEchoMetricAttributeFn specifies a function that extracts additional attributes from the echo.Context
103 // and returns them as a slice of attribute.KeyValue.
104 //
105 // If attributes are duplicated between this method and `WithMetricAttributeFn`, the attributes in this method will be used.
106 func WithEchoMetricAttributeFn(f EchoMetricAttributeFn) Option {
107 return optionFunc(func(cfg *config) {
108 cfg.EchoMetricAttributeFn = f
109 })
110 }
111
112 // WithOnError specifies a function that is called when an error occurs during request processing.
113 //
114 // WARNING: If the passed function doesn't call `c.Error` and the global HTTPErrorHandler modifies the response,
115 // the tracing span can contain invalid data.
116 // If it calls `c.Error`, `HTTPErrorHandler` will be executed twice, but the span will have the actual response data.
117 // To fix this, check the response commitment status with `c.Response().Committed` before modifying the response.
118 func WithOnError(f OnErrorFn) Option {
119 return optionFunc(func(cfg *config) {
120 if f != nil {
121 cfg.OnError = f
122 }
123 })
124 }