/
big_endian_converter.go
104 lines (93 loc) · 3.34 KB
/
big_endian_converter.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2019, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tracetranslator
import (
"encoding/binary"
"errors"
)
var (
// ErrNilTraceID error returned when the TraceID is nil
ErrNilTraceID = errors.New("TraceID is nil")
// ErrWrongLenTraceID error returned when the TraceID does not have 16 bytes.
ErrWrongLenTraceID = errors.New("TraceID does not have 16 bytes")
// ErrNilSpanID error returned when the SpanID is nil
ErrNilSpanID = errors.New("SpanID is nil")
// ErrWrongLenSpanID error returned when the SpanID does not have 8 bytes.
ErrWrongLenSpanID = errors.New("SpanID does not have 8 bytes")
)
// UInt64ToByteTraceID takes a two uint64 representation of a TraceID and
// converts it to a []byte representation.
func UInt64ToByteTraceID(high, low uint64) []byte {
if high == 0 && low == 0 {
return nil
}
traceID := make([]byte, 16)
binary.BigEndian.PutUint64(traceID[:8], high)
binary.BigEndian.PutUint64(traceID[8:], low)
return traceID
}
// Int64ToByteTraceID takes a two int64 representation of a TraceID and
// converts it to a []byte representation.
func Int64ToByteTraceID(high, low int64) []byte {
return UInt64ToByteTraceID(uint64(high), uint64(low))
}
// BytesToUInt64TraceID takes a []byte representation of a TraceID and
// converts it to a two uint64 representation.
func BytesToUInt64TraceID(traceID []byte) (uint64, uint64, error) {
if traceID == nil {
return 0, 0, ErrNilTraceID
}
if len(traceID) != 16 {
return 0, 0, ErrWrongLenTraceID
}
return binary.BigEndian.Uint64(traceID[:8]), binary.BigEndian.Uint64(traceID[8:]), nil
}
// BytesToInt64TraceID takes a []byte representation of a TraceID and
// converts it to a two int64 representation.
func BytesToInt64TraceID(traceID []byte) (int64, int64, error) {
traceIDHigh, traceIDLow, err := BytesToUInt64TraceID(traceID)
return int64(traceIDHigh), int64(traceIDLow), err
}
// UInt64ToByteSpanID takes a uint64 representation of a SpanID and
// converts it to a []byte representation.
func UInt64ToByteSpanID(id uint64) []byte {
if id == 0 {
return nil
}
spanID := make([]byte, 8)
binary.BigEndian.PutUint64(spanID, id)
return spanID
}
// Int64ToByteSpanID takes a int64 representation of a SpanID and
// converts it to a []byte representation.
func Int64ToByteSpanID(id int64) []byte {
return UInt64ToByteSpanID(uint64(id))
}
// BytesToUInt64SpanID takes a []byte representation of a SpanID and
// converts it to a uint64 representation.
func BytesToUInt64SpanID(b []byte) (uint64, error) {
if b == nil {
return 0, ErrNilSpanID
}
if len(b) != 8 {
return 0, ErrWrongLenSpanID
}
return binary.BigEndian.Uint64(b), nil
}
// BytesToInt64SpanID takes a []byte representation of a SpanID and
// converts it to a int64 representation.
func BytesToInt64SpanID(b []byte) (int64, error) {
id, err := BytesToUInt64SpanID(b)
return int64(id), err
}