-
Notifications
You must be signed in to change notification settings - Fork 121
/
config.go
116 lines (101 loc) · 3.61 KB
/
config.go
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
package otelfiber
import (
"github.com/gofiber/fiber/v2"
"go.opentelemetry.io/otel/attribute"
otelmetric "go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/propagation"
oteltrace "go.opentelemetry.io/otel/trace"
)
// config is used to configure the Fiber middleware.
type config struct {
Next func(*fiber.Ctx) bool
TracerProvider oteltrace.TracerProvider
MeterProvider otelmetric.MeterProvider
Port *int
Propagators propagation.TextMapPropagator
ServerName *string
SpanNameFormatter func(*fiber.Ctx) string
CustomAttributes func(*fiber.Ctx) []attribute.KeyValue
CustomMetricAttributes func(*fiber.Ctx) []attribute.KeyValue
collectClientIP bool
}
// Option specifies instrumentation configuration options.
type Option interface {
apply(*config)
}
type optionFunc func(*config)
func (o optionFunc) apply(c *config) {
o(c)
}
// WithNext takes a function that will be called on every
// request, the middleware will be skipped if returning true
func WithNext(f func(ctx *fiber.Ctx) bool) Option {
return optionFunc(func(cfg *config) {
cfg.Next = f
})
}
// 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) {
cfg.Propagators = propagators
})
}
// 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) {
cfg.TracerProvider = provider
})
}
// WithMeterProvider specifies a meter provider to use for reporting.
// If none is specified, the global provider is used.
func WithMeterProvider(provider otelmetric.MeterProvider) Option {
return optionFunc(func(cfg *config) {
cfg.MeterProvider = provider
})
}
// WithSpanNameFormatter takes a function that will be called on every
// request and the returned string will become the Span Name
func WithSpanNameFormatter(f func(ctx *fiber.Ctx) string) Option {
return optionFunc(func(cfg *config) {
cfg.SpanNameFormatter = f
})
}
// WithServerName specifies the value to use when setting the `http.server_name`
// attribute on metrics/spans.
func WithServerName(serverName string) Option {
return optionFunc(func(cfg *config) {
cfg.ServerName = &serverName
})
}
// WithPort specifies the value to use when setting the `net.host.port`
// attribute on metrics/spans. Attribute is "Conditionally Required: If not
// default (`80` for `http`, `443` for `https`).
func WithPort(port int) Option {
return optionFunc(func(cfg *config) {
cfg.Port = &port
})
}
// WithCustomAttributes specifies a function that will be called on every
// request and the returned attributes will be added to the span.
func WithCustomAttributes(f func(ctx *fiber.Ctx) []attribute.KeyValue) Option {
return optionFunc(func(cfg *config) {
cfg.CustomAttributes = f
})
}
// WithCustomMetricAttributes specifies a function that will be called on every
// request and the returned attributes will be added to the metrics.
func WithCustomMetricAttributes(f func(ctx *fiber.Ctx) []attribute.KeyValue) Option {
return optionFunc(func(cfg *config) {
cfg.CustomMetricAttributes = f
})
}
// WithCollectClientIP specifies whether to collect the client's IP address
// from the request. This is enabled by default.
func WithCollectClientIP(collect bool) Option {
return optionFunc(func(cfg *config) {
cfg.collectClientIP = collect
})
}