MCPcopy
hub / github.com/grafana/tempo / hexStringToID

Function hexStringToID

pkg/util/traceid.go:147–187  ·  view source on GitHub ↗
(id string, isSpan bool)

Source from the content-addressed store, hash-verified

145}
146
147func hexStringToID(id string, isSpan bool) ([]byte, error) {
148 // The encoding/hex package does not handle non-hex characters.
149 // Ensure the ID has only the proper characters
150 for i, c := range id {
151 if (c < 'a' || c > 'f') && (c < 'A' || c > 'F') && (c < '0' || c > '9') {
152 return nil, fmt.Errorf("trace IDs can only contain hex characters: invalid character '%c' at position %d", c, i+1)
153 }
154 }
155
156 // the encoding/hex package does not like odd length strings.
157 // just append a bit here
158 if len(id)%2 == 1 {
159 id = "0" + id
160 }
161
162 byteID, err := hex.DecodeString(id)
163 if err != nil {
164 return nil, err
165 }
166
167 size := len(byteID)
168
169 if isSpan {
170 if size > 8 {
171 return nil, errors.New("span IDs can't be larger than 64 bits")
172 }
173 // if size < 8 {
174 // byteID = append(make([]byte, 8-size), byteID...)
175 // }
176 return byteID, nil
177 }
178
179 if size > 16 {
180 return nil, errors.New("trace IDs can't be larger than 128 bits")
181 }
182 if size < 16 {
183 byteID = append(make([]byte, 16-size), byteID...)
184 }
185
186 return byteID, nil
187}

Callers 2

HexStringToTraceIDFunction · 0.85
HexStringToSpanIDFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected