Debian Codegraph @

samplers/probability/consistent/tracestate.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package consistent // import "go.opentelemetry.io/contrib/samplers/probability/consistent"
5
6 import (
7 "errors"
8 "fmt"
9 "strconv"
10 "strings"
11 )
12
13 const (
14 traceStateKey = "ot"
15 pValueSubkey = "p"
16 rValueSubkey = "r"
17 pZeroValue = 63
18 invalidValue = pZeroValue + 1 // invalid for p or r
19 traceStateSizeLimit = 256
20 )
21
22 var (
23 errTraceStateSyntax = fmt.Errorf("otel tracestate: %w", strconv.ErrSyntax)
24 errTraceStateInconsistent = errors.New("r-value and p-value are inconsistent")
25 )
26
27 type otelTraceState struct {
28 rvalue uint8 // valid in the interval [0, 62]
29 pvalue uint8 // valid in the interval [0, 63]
30 unknown []string
31 }
32
33 func newTraceState() otelTraceState {
34 return otelTraceState{
35 rvalue: invalidValue, // out-of-range => !hasRValue()
36 pvalue: invalidValue, // out-of-range => !hasPValue()
37 }
38 }
39
40 func (otts otelTraceState) serialize() string {
41 var sb strings.Builder
42 semi := func() {
43 if sb.Len() != 0 {
44 _, _ = sb.WriteString(";")
45 }
46 }
47
48 if otts.hasPValue() {
49 _, _ = fmt.Fprintf(&sb, "p:%d", otts.pvalue)
50 }
51 if otts.hasRValue() {
52 semi()
53 _, _ = fmt.Fprintf(&sb, "r:%d", otts.rvalue)
54 }
55 for _, unk := range otts.unknown {
56 ex := 0
57 if sb.Len() != 0 {
58 ex = 1
59 }
60 if sb.Len()+ex+len(unk) > traceStateSizeLimit {
61 // Note: should this generate an explicit error?
62 break
63 }
64 semi()
65 _, _ = sb.WriteString(unk)
66 }
67 return sb.String()
68 }
69
70 func isValueByte(r byte) bool {
71 if isLCAlphaNum(r) {
72 return true
73 }
74 if isUCAlpha(r) {
75 return true
76 }
77 return r == '.' || r == '_' || r == '-'
78 }
79
80 func isLCAlphaNum(r byte) bool {
81 if isLCAlpha(r) {
82 return true
83 }
84 return r >= '0' && r <= '9'
85 }
86
87 func isLCAlpha(r byte) bool {
88 return r >= 'a' && r <= 'z'
89 }
90
91 func isUCAlpha(r byte) bool {
92 return r >= 'A' && r <= 'Z'
93 }
94
95 func parseOTelTraceState(ts string, isSampled bool) (otelTraceState, error) { //nolint:revive // ignore linter
96 var pval, rval string
97 var unknown []string
98
99 if ts == "" {
100 return newTraceState(), nil
101 }
102
103 if len(ts) > traceStateSizeLimit {
104 return newTraceState(), errTraceStateSyntax
105 }
106
107 for ts != "" {
108 eqPos := 0
109 for ; eqPos < len(ts); eqPos++ {
110 if eqPos == 0 {
111 if isLCAlpha(ts[eqPos]) {
112 continue
113 }
114 } else if isLCAlphaNum(ts[eqPos]) {
115 continue
116 }
117 break
118 }
119 if eqPos == 0 || eqPos == len(ts) || ts[eqPos] != ':' {
120 return newTraceState(), errTraceStateSyntax
121 }
122
123 key := ts[0:eqPos]
124 tail := ts[eqPos+1:]
125
126 sepPos := 0
127
128 for ; sepPos < len(tail); sepPos++ {
129 if isValueByte(tail[sepPos]) {
130 continue
131 }
132 break
133 }
134
135 switch key {
136 case pValueSubkey:
137 // Note: does the spec say how to handle duplicates?
138 pval = tail[0:sepPos]
139 case rValueSubkey:
140 rval = tail[0:sepPos]
141 default:
142 unknown = append(unknown, ts[0:sepPos+eqPos+1])
143 }
144
145 if sepPos < len(tail) && tail[sepPos] != ';' {
146 return newTraceState(), errTraceStateSyntax
147 }
148
149 if sepPos == len(tail) {
150 break
151 }
152
153 ts = tail[sepPos+1:]
154
155 // test for a trailing ;
156 if ts == "" {
157 return newTraceState(), errTraceStateSyntax
158 }
159 }
160
161 otts := newTraceState()
162 otts.unknown = unknown
163
164 // Note: set R before P, so that P won't propagate if R has an error.
165 value, err := parseNumber(rValueSubkey, rval, pZeroValue-1)
166 if err != nil {
167 return otts, err
168 }
169 otts.rvalue = value
170
171 value, err = parseNumber(pValueSubkey, pval, pZeroValue)
172 if err != nil {
173 return otts, err
174 }
175 otts.pvalue = value
176
177 // Invariant checking: unset P when the values are inconsistent.
178 if otts.hasPValue() && otts.hasRValue() {
179 implied := otts.pvalue <= otts.rvalue || otts.pvalue == pZeroValue
180
181 if !isSampled || !implied {
182 // Note: the error ensures the parent-based
183 // sampler repairs the broken tracestate entry.
184 otts.pvalue = invalidValue
185 return otts, parseError(pValueSubkey, errTraceStateInconsistent)
186 }
187 }
188
189 return otts, nil
190 }
191
192 func parseNumber(key, input string, maximum uint8) (uint8, error) {
193 if input == "" {
194 return maximum + 1, nil
195 }
196 value, err := strconv.ParseUint(input, 10, 64)
197 if err != nil {
198 return maximum + 1, parseError(key, err)
199 }
200 if value > uint64(maximum) {
201 return maximum + 1, parseError(key, strconv.ErrRange)
202 }
203 return uint8(value), nil //nolint:gosec // `value` is strictly less then the uint8 maximum. This cast is safe.
204 }
205
206 func parseError(key string, err error) error {
207 return fmt.Errorf("otel tracestate: %s-value %w", key, err)
208 }
209
210 func (otts otelTraceState) hasRValue() bool {
211 return otts.rvalue < pZeroValue
212 }
213
214 func (otts otelTraceState) hasPValue() bool {
215 return otts.pvalue <= pZeroValue
216 }