Debian Codegraph @

instrumentation/github.com/emicklei/go-restful/otelrestful/config.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otelrestful // import "go.opentelemetry.io/contrib/instrumentation/github.com/emicklei/go-restful/otelrestful"
5
6 import (
7 "net/http"
8
9 "go.opentelemetry.io/otel/propagation"
10 oteltrace "go.opentelemetry.io/otel/trace"
11 )
12
13 // config is used to configure the go-restful middleware.
14 type config struct {
15 TracerProvider oteltrace.TracerProvider
16 Propagators propagation.TextMapPropagator
17 PublicEndpoint bool
18 PublicEndpointFn func(*http.Request) bool
19 }
20
21 // Option applies a configuration value.
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 // WithPublicEndpoint configures the Handler to link the span with an incoming
33 // span context. If this option is not provided, then the association is a child
34 // association instead of a link.
35 func WithPublicEndpoint() Option {
36 return optionFunc(func(c *config) {
37 c.PublicEndpoint = true
38 })
39 }
40
41 // WithPropagators specifies propagators to use for extracting
42 // information from the HTTP requests. If none are specified, global
43 // ones will be used.
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 // WithTracerProvider specifies a tracer provider to use for creating a tracer.
53 // If none is specified, the global provider is used.
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 // WithPublicEndpointFn runs with every request, and allows conditionally
63 // configuring the Handler to link the span with an incoming span context. If
64 // this option is not provided or returns false, then the association is a
65 // child association instead of a link.
66 // Note: [WithPublicEndpoint] takes precedence over WithPublicEndpointFn.
67 func WithPublicEndpointFn(fn func(*http.Request) bool) Option {
68 return optionFunc(func(c *config) {
69 c.PublicEndpointFn = fn
70 })
71 }