-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
111 lines (96 loc) · 3.43 KB
/
option.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
package otelfox
import (
"github.com/tigerwill90/fox"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"net/http"
)
type Option interface {
apply(*config)
}
type optionFunc func(*config)
func (o optionFunc) apply(c *config) {
o(c)
}
// Filter is a function that determines whether a given HTTP request should be traced.
// It returns true to indicate the request should not be traced.
type Filter func(c fox.Context) bool
// SpanNameFormatter is a function that formats the span name given the HTTP request.
// This allows for dynamic naming of spans based on attributes of the request.
type SpanNameFormatter func(c fox.Context) string
// SpanAttributesFunc is a function type that can be used to dynamically
// generate span attributes for a given HTTP request. It is used in
// conjunction with the [WithSpanAttributes] middleware option.
type SpanAttributesFunc func(c fox.Context) []attribute.KeyValue
type config struct {
provider trace.TracerProvider
propagator propagation.TextMapPropagator
carrier func(r *http.Request) propagation.TextMapCarrier
spanFmt SpanNameFormatter
filters []Filter
attrsFn SpanAttributesFunc
}
func defaultConfig() *config {
return &config{
provider: otel.GetTracerProvider(),
propagator: otel.GetTextMapPropagator(),
carrier: func(r *http.Request) propagation.TextMapCarrier {
return propagation.HeaderCarrier(r.Header)
},
}
}
// 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(c *config) {
if propagators != nil {
c.propagator = propagators
}
})
}
// WithTracerProvider specifies a tracer provider to use for creating a tracer.
// If none is specified, the global provider is used.
func WithTracerProvider(provider trace.TracerProvider) Option {
return optionFunc(func(c *config) {
if provider != nil {
c.provider = provider
}
})
}
// WithTextMapCarrier specify a carrier to use for extracting information from http request.
// If none is specified, [propagation.HeaderCarrier] is used.
func WithTextMapCarrier(fn func(r *http.Request) propagation.TextMapCarrier) Option {
return optionFunc(func(c *config) {
if fn != nil {
c.carrier = fn
}
})
}
// WithSpanNameFormatter takes a function that will be called on every request
// and the returned string will become the Span Name.
func WithSpanNameFormatter(fn SpanNameFormatter) Option {
return optionFunc(func(c *config) {
c.spanFmt = fn
})
}
// WithFilter appends the provided filters to the middleware's filter list.
// A filter returning true will exclude the request from being traced. If no filters
// are provided, all requests will be traced. Keep in mind that filters are invoked for each request,
// so they should be simple and efficient.
func WithFilter(f ...Filter) Option {
return optionFunc(func(c *config) {
c.filters = append(c.filters, f...)
})
}
// WithSpanAttributes specifies a function for generating span attributes.
// The function will be invoked for each request, and its return attributes
// will be added to the span. For example, you can use this option to add
// the http.target attribute to the span.
func WithSpanAttributes(fn SpanAttributesFunc) Option {
return optionFunc(func(c *config) {
c.attrsFn = fn
})
}