forked from labstack/echo-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
121 lines (104 loc) · 3.99 KB
/
Copy pathconfig.go
File metadata and controls
121 lines (104 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package otelecho
import (
"net/http"
"github.com/labstack/echo/v5"
"github.com/labstack/echo/v5/middleware"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/propagation"
oteltrace "go.opentelemetry.io/otel/trace"
)
// config is used to configure the mux middleware.
type config struct {
TracerProvider oteltrace.TracerProvider
MeterProvider metric.MeterProvider
Propagators propagation.TextMapPropagator
Skipper middleware.Skipper
MetricAttributeFn MetricAttributeFn
EchoMetricAttributeFn EchoMetricAttributeFn
OnError OnErrorFn
}
// MetricAttributeFn is used to extract additional attributes from the http.Request
// and return them as a slice of attribute.KeyValue.
type MetricAttributeFn func(*http.Request) []attribute.KeyValue
// EchoMetricAttributeFn is used to extract additional attributes from the echo.Context
// and return them as a slice of attribute.KeyValue.
type EchoMetricAttributeFn func(*echo.Context) []attribute.KeyValue
// OnErrorFn is used to specify how errors are handled in the middleware.
type OnErrorFn func(*echo.Context, error)
// defaultOnError is the default function called when an error occurs during request processing.
// In Echo v5, errors are handled by the framework's HTTPErrorHandler automatically.
var defaultOnError = func(_ *echo.Context, _ error) {
// In Echo v5, errors are propagated and handled by HTTPErrorHandler
}
// Option specifies instrumentation configuration options.
type Option interface {
apply(*config)
}
type optionFunc func(*config)
func (o optionFunc) apply(c *config) {
o(c)
}
// WithPropagators specifies propagators to use for extracting
// information from the HTTP requests. If none are specified, global
// ones will be used.
func WithPropagators(propagators propagation.TextMapPropagator) Option {
return optionFunc(func(cfg *config) {
if propagators != nil {
cfg.Propagators = propagators
}
})
}
// WithMeterProvider specifies a meter provider to use for creating a meter.
// If none is specified, the global provider is used.
func WithMeterProvider(provider metric.MeterProvider) Option {
return optionFunc(func(cfg *config) {
if provider != nil {
cfg.MeterProvider = provider
}
})
}
// WithTracerProvider specifies a tracer provider to use for creating a tracer.
// If none is specified, the global provider is used.
func WithTracerProvider(provider oteltrace.TracerProvider) Option {
return optionFunc(func(cfg *config) {
if provider != nil {
cfg.TracerProvider = provider
}
})
}
// WithSkipper specifies a skipper for allowing requests to skip generating spans.
func WithSkipper(skipper middleware.Skipper) Option {
return optionFunc(func(cfg *config) {
cfg.Skipper = skipper
})
}
// WithMetricAttributeFn specifies a function that extracts additional attributes from the http.Request
// and returns them as a slice of attribute.KeyValue.
//
// If attributes are duplicated between this method and `WithEchoMetricAttributeFn`, the attributes in this method will be overridden.
func WithMetricAttributeFn(f MetricAttributeFn) Option {
return optionFunc(func(cfg *config) {
cfg.MetricAttributeFn = f
})
}
// WithEchoMetricAttributeFn specifies a function that extracts additional attributes from the echo.Context
// and returns them as a slice of attribute.KeyValue.
//
// If attributes are duplicated between this method and `WithMetricAttributeFn`, the attributes in this method will be used.
func WithEchoMetricAttributeFn(f EchoMetricAttributeFn) Option {
return optionFunc(func(cfg *config) {
cfg.EchoMetricAttributeFn = f
})
}
// WithOnError specifies a function that is called when an error occurs during request processing.
//
// In Echo v5, errors are automatically handled by the HTTPErrorHandler after the middleware returns.
// This callback allows you to perform additional actions when an error occurs.
func WithOnError(f OnErrorFn) Option {
return optionFunc(func(cfg *config) {
if f != nil {
cfg.OnError = f
}
})
}