Debian Codegraph @

propagators/b3/b3_config.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package b3 // import "go.opentelemetry.io/contrib/propagators/b3"
5
6 type config struct {
7 // InjectEncoding are the B3 encodings used when injecting trace
8 // information. If no encoding is specified (i.e. `B3Unspecified`)
9 // `B3SingleHeader` will be used as the default.
10 InjectEncoding Encoding
11 }
12
13 // Option interface used for setting optional config properties.
14 type Option interface {
15 apply(*config)
16 }
17
18 type optionFunc func(*config)
19
20 func (o optionFunc) apply(c *config) {
21 o(c)
22 }
23
24 // newConfig creates a new config struct and applies opts to it.
25 func newConfig(opts ...Option) *config {
26 c := &config{}
27 for _, opt := range opts {
28 opt.apply(c)
29 }
30 return c
31 }
32
33 // Encoding is a bitmask representation of the B3 encoding type.
34 type Encoding uint8
35
36 // supports returns if e has o bit(s) set.
37 func (e Encoding) supports(o Encoding) bool {
38 return e&o == o
39 }
40
41 const (
42 // B3Unspecified is an unspecified B3 encoding.
43 B3Unspecified Encoding = 0
44 // B3MultipleHeader is a B3 encoding that uses multiple headers to
45 // transmit tracing information all prefixed with `x-b3-`.
46 // x-b3-traceid: {TraceId}
47 // x-b3-parentspanid: {ParentSpanId}
48 // x-b3-spanid: {SpanId}
49 // x-b3-sampled: {SamplingState}
50 // x-b3-flags: {DebugFlag}
51 B3MultipleHeader Encoding = 1 << iota
52 // B3SingleHeader is a B3 encoding that uses a single header named `b3`
53 // to transmit tracing information.
54 // b3: {TraceId}-{SpanId}-{SamplingState}-{ParentSpanId}
55 B3SingleHeader
56 )
57
58 // WithInjectEncoding sets the encoding the propagator will inject.
59 // The encoding is interpreted as a bitmask. Therefore
60 //
61 // WithInjectEncoding(B3SingleHeader | B3MultipleHeader)
62 //
63 // means the propagator will inject both single and multi B3 headers.
64 func WithInjectEncoding(encoding Encoding) Option {
65 return optionFunc(func(c *config) {
66 c.InjectEncoding = encoding
67 })
68 }