| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otelhttp |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"net/http" |
| 9 |
"net/http/httptrace" |
| 10 |
|
| 11 |
"go.opentelemetry.io/otel" |
| 12 |
"go.opentelemetry.io/otel/attribute" |
| 13 |
"go.opentelemetry.io/otel/metric" |
| 14 |
"go.opentelemetry.io/otel/propagation" |
| 15 |
"go.opentelemetry.io/otel/trace" |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
const ScopeName = "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
type config struct { |
| 24 |
ServerName string |
| 25 |
Tracer trace.Tracer |
| 26 |
Meter metric.Meter |
| 27 |
Propagators propagation.TextMapPropagator |
| 28 |
SpanStartOptions []trace.SpanStartOption |
| 29 |
PublicEndpointFn func(*http.Request) bool |
| 30 |
ReadEvent bool |
| 31 |
WriteEvent bool |
| 32 |
Filters []Filter |
| 33 |
SpanNameFormatter func(string, *http.Request) string |
| 34 |
ClientTrace func(context.Context) *httptrace.ClientTrace |
| 35 |
|
| 36 |
TracerProvider trace.TracerProvider |
| 37 |
MeterProvider metric.MeterProvider |
| 38 |
MetricAttributesFn func(*http.Request) []attribute.KeyValue |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
type Option interface { |
| 43 |
apply(*config) |
| 44 |
} |
| 45 |
|
| 46 |
type optionFunc func(*config) |
| 47 |
|
| 48 |
func (o optionFunc) apply(c *config) { |
| 49 |
o(c) |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
func newConfig(opts ...Option) *config { |
| 54 |
c := &config{ |
| 55 |
Propagators: otel.GetTextMapPropagator(), |
| 56 |
MeterProvider: otel.GetMeterProvider(), |
| 57 |
} |
| 58 |
for _, opt := range opts { |
| 59 |
opt.apply(c) |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
if c.TracerProvider != nil { |
| 64 |
c.Tracer = newTracer(c.TracerProvider) |
| 65 |
} |
| 66 |
|
| 67 |
c.Meter = c.MeterProvider.Meter( |
| 68 |
ScopeName, |
| 69 |
metric.WithInstrumentationVersion(Version), |
| 70 |
) |
| 71 |
|
| 72 |
return c |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
func WithTracerProvider(provider trace.TracerProvider) Option { |
| 78 |
return optionFunc(func(cfg *config) { |
| 79 |
if provider != nil { |
| 80 |
cfg.TracerProvider = provider |
| 81 |
} |
| 82 |
}) |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
func WithMeterProvider(provider metric.MeterProvider) Option { |
| 88 |
return optionFunc(func(cfg *config) { |
| 89 |
if provider != nil { |
| 90 |
cfg.MeterProvider = provider |
| 91 |
} |
| 92 |
}) |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
func WithPublicEndpointFn(fn func(*http.Request) bool) Option { |
| 100 |
return optionFunc(func(c *config) { |
| 101 |
c.PublicEndpointFn = fn |
| 102 |
}) |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
func WithPropagators(ps propagation.TextMapPropagator) Option { |
| 108 |
return optionFunc(func(c *config) { |
| 109 |
if ps != nil { |
| 110 |
c.Propagators = ps |
| 111 |
} |
| 112 |
}) |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
func WithSpanOptions(opts ...trace.SpanStartOption) Option { |
| 118 |
return optionFunc(func(c *config) { |
| 119 |
c.SpanStartOptions = append(c.SpanStartOptions, opts...) |
| 120 |
}) |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
func WithFilter(f Filter) Option { |
| 130 |
return optionFunc(func(c *config) { |
| 131 |
c.Filters = append(c.Filters, f) |
| 132 |
}) |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
type Event int |
| 137 |
|
| 138 |
|
| 139 |
const ( |
| 140 |
unspecifiedEvents Event = iota |
| 141 |
ReadEvents |
| 142 |
WriteEvents |
| 143 |
) |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
func WithMessageEvents(events ...Event) Option { |
| 155 |
return optionFunc(func(c *config) { |
| 156 |
for _, e := range events { |
| 157 |
switch e { |
| 158 |
case ReadEvents: |
| 159 |
c.ReadEvent = true |
| 160 |
case WriteEvents: |
| 161 |
c.WriteEvent = true |
| 162 |
} |
| 163 |
} |
| 164 |
}) |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
func WithSpanNameFormatter(f func(operation string, r *http.Request) string) Option { |
| 174 |
return optionFunc(func(c *config) { |
| 175 |
c.SpanNameFormatter = f |
| 176 |
}) |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
func WithClientTrace(f func(context.Context) *httptrace.ClientTrace) Option { |
| 182 |
return optionFunc(func(c *config) { |
| 183 |
c.ClientTrace = f |
| 184 |
}) |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
func WithServerName(server string) Option { |
| 190 |
return optionFunc(func(c *config) { |
| 191 |
c.ServerName = server |
| 192 |
}) |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
func WithMetricAttributesFn(metricAttributesFn func(r *http.Request) []attribute.KeyValue) Option { |
| 201 |
return optionFunc(func(c *config) { |
| 202 |
c.MetricAttributesFn = metricAttributesFn |
| 203 |
}) |
| 204 |
} |