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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
package solomon
import (
"bytes"
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_metrics_encode(t *testing.T) {
expectHeader := []byte{
0x53, 0x50, // magic
0x01, 0x01, // version
0x18, 0x00, // header size
0x0, // time precision
0x0, // compression algorithm
0x7, 0x0, 0x0, 0x0, // label names size
0x8, 0x0, 0x0, 0x0, // label values size
0x1, 0x0, 0x0, 0x0, // metric count
0x1, 0x0, 0x0, 0x0, // point count
// label names pool
0x73, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x0, // "sensor"
// label values pool
0x6d, 0x79, 0x67, 0x61, 0x75, 0x67, 0x65, 0x0, // "gauge"
}
testCases := []struct {
name string
metrics *Metrics
expectCommonTime []byte
expectCommonLabels []byte
expectMetrics [][]byte
expectWritten int
}{
{
"common-ts+gauge",
&Metrics{
metrics: []Metric{
func() Metric {
g := NewGauge("mygauge", 43)
return &g
}(),
},
timestamp: timeAsRef(time.Unix(1500000000, 0)),
},
[]byte{0x0, 0x2f, 0x68, 0x59}, // common time /1500000000
[]byte{0x0}, // common labels count and indexes
[][]byte{
{
0x5, // types
0x0, // flags
0x1, // labels index size
0x0, // indexes of name labels
0x0, // indexes of value labels
0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x45, 0x40, // 43 // metrics value
},
},
57,
},
{
"gauge+ts",
&Metrics{
metrics: []Metric{
func() Metric {
g := NewGauge("mygauge", 43, WithTimestamp(time.Unix(1657710476, 0)))
return &g
}(),
},
},
[]byte{0x0, 0x0, 0x0, 0x0}, // common time
[]byte{0x0}, // common labels count and indexes
[][]byte{
{
0x6, // uint8(typeGauge << 2) | uint8(valueTypeOneWithTS)
0x0, // flags
0x1, // labels index size
0x0, // indexes of name labels
0x0, // indexes of value labels
0x8c, 0xa7, 0xce, 0x62, //metric ts
0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x45, 0x40, // 43 // metrics value
},
},
61,
},
{
"common-ts+gauge+ts",
&Metrics{
metrics: []Metric{
func() Metric {
g := NewGauge("mygauge", 43, WithTimestamp(time.Unix(1657710476, 0)))
return &g
}(),
func() Metric {
g := NewGauge("mygauge", 42, WithTimestamp(time.Unix(1500000000, 0)))
return &g
}(),
},
timestamp: timeAsRef(time.Unix(1500000000, 0)),
},
[]byte{0x0, 0x2f, 0x68, 0x59}, // common time /1500000000
[]byte{0x0}, // common labels count and indexes
[][]byte{
{
0x6, // types
0x0, // flags
0x1, // labels index size
0x0, // indexes of name labels
0x0, // indexes of value labels
0x8c, 0xa7, 0xce, 0x62, //metric ts
0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x45, 0x40, // 43 // metrics value
},
{
0x6, // types
0x0, // flags
0x1, // labels index size
0x0, // indexes of name labels
0x0, // indexes of value labels
0x0, 0x2f, 0x68, 0x59, // metric ts
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x45, 0x40, //42 // metrics value
},
},
78,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
ctx := context.Background()
written, err := NewSpackEncoder(ctx, CompressionNone, tc.metrics).Encode(&buf)
assert.NoError(t, err)
assert.Equal(t, tc.expectWritten, written)
body := buf.Bytes()
setMetricsCount(expectHeader, len(tc.metrics.metrics))
require.True(t, bytes.HasPrefix(body, expectHeader))
body = body[len(expectHeader):]
require.True(t, bytes.HasPrefix(body, tc.expectCommonTime))
body = body[len(tc.expectCommonTime):]
require.True(t, bytes.HasPrefix(body, tc.expectCommonLabels))
body = body[len(tc.expectCommonLabels):]
expectButMissing := [][]byte{}
for range tc.expectMetrics {
var seen bool
var val []byte
for _, v := range tc.expectMetrics {
val = v
if bytes.HasPrefix(body, v) {
body = bytes.Replace(body, v, []byte{}, 1)
seen = true
break
}
}
if !seen {
expectButMissing = append(expectButMissing, val)
}
}
assert.Empty(t, body, "unexpected bytes seen")
assert.Empty(t, expectButMissing, "missing metrics bytes")
})
}
}
func setMetricsCount(header []byte, count int) {
header[16] = uint8(count)
header[20] = uint8(count)
}
|