-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
spanid.go
79 lines (62 loc) · 1.83 KB
/
spanid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package data // import "go.opentelemetry.io/collector/pdata/internal/data"
import (
"errors"
"github.com/gogo/protobuf/proto"
)
const spanIDSize = 8
var (
errMarshalSpanID = errors.New("marshal: invalid buffer length for SpanID")
errUnmarshalSpanID = errors.New("unmarshal: invalid SpanID length")
)
// SpanID is a custom data type that is used for all span_id fields in OTLP
// Protobuf messages.
type SpanID [spanIDSize]byte
var _ proto.Sizer = (*SpanID)(nil)
// Size returns the size of the data to serialize.
func (sid SpanID) Size() int {
if sid.IsEmpty() {
return 0
}
return spanIDSize
}
// IsEmpty returns true if id contains at least one non-zero byte.
func (sid SpanID) IsEmpty() bool {
return sid == [spanIDSize]byte{}
}
// MarshalTo converts trace ID into a binary representation. Called by Protobuf serialization.
func (sid SpanID) MarshalTo(data []byte) (n int, err error) {
if sid.IsEmpty() {
return 0, nil
}
if len(data) < spanIDSize {
return 0, errMarshalSpanID
}
return copy(data, sid[:]), nil
}
// Unmarshal inflates this trace ID from binary representation. Called by Protobuf serialization.
func (sid *SpanID) Unmarshal(data []byte) error {
if len(data) == 0 {
*sid = [spanIDSize]byte{}
return nil
}
if len(data) != spanIDSize {
return errUnmarshalSpanID
}
copy(sid[:], data)
return nil
}
// MarshalJSON converts SpanID into a hex string enclosed in quotes.
func (sid SpanID) MarshalJSON() ([]byte, error) {
if sid.IsEmpty() {
return []byte(`""`), nil
}
return marshalJSON(sid[:])
}
// UnmarshalJSON decodes SpanID from hex string, possibly enclosed in quotes.
// Called by Protobuf JSON deserialization.
func (sid *SpanID) UnmarshalJSON(data []byte) error {
*sid = [spanIDSize]byte{}
return unmarshalJSON(sid[:], data)
}