| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otelrestful |
| 5 |
|
| 6 |
import ( |
| 7 |
"net/http" |
| 8 |
|
| 9 |
"go.opentelemetry.io/otel/propagation" |
| 10 |
oteltrace "go.opentelemetry.io/otel/trace" |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
type config struct { |
| 15 |
TracerProvider oteltrace.TracerProvider |
| 16 |
Propagators propagation.TextMapPropagator |
| 17 |
PublicEndpoint bool |
| 18 |
PublicEndpointFn func(*http.Request) bool |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
type Option interface { |
| 23 |
apply(*config) |
| 24 |
} |
| 25 |
|
| 26 |
type optionFunc func(*config) |
| 27 |
|
| 28 |
func (o optionFunc) apply(c *config) { |
| 29 |
o(c) |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
func WithPublicEndpoint() Option { |
| 36 |
return optionFunc(func(c *config) { |
| 37 |
c.PublicEndpoint = true |
| 38 |
}) |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
func WithPropagators(propagators propagation.TextMapPropagator) Option { |
| 45 |
return optionFunc(func(cfg *config) { |
| 46 |
if propagators != nil { |
| 47 |
cfg.Propagators = propagators |
| 48 |
} |
| 49 |
}) |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
func WithTracerProvider(provider oteltrace.TracerProvider) Option { |
| 55 |
return optionFunc(func(cfg *config) { |
| 56 |
if provider != nil { |
| 57 |
cfg.TracerProvider = provider |
| 58 |
} |
| 59 |
}) |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
func WithPublicEndpointFn(fn func(*http.Request) bool) Option { |
| 68 |
return optionFunc(func(c *config) { |
| 69 |
c.PublicEndpointFn = fn |
| 70 |
}) |
| 71 |
} |