-
Notifications
You must be signed in to change notification settings - Fork 53
/
otel_test.go
177 lines (148 loc) · 3.8 KB
/
otel_test.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
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
package ch
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/ClickHouse/ch-go/proto"
)
type MapPair[V comparable] struct {
Keys *proto.ColArr[string]
Values *proto.ColArr[V]
}
func (m MapPair[V]) Row(i int) (keys []string, values []V) {
return m.Keys.Row(i), m.Values.Row(i)
}
func (m MapPair[V]) Append(k []string, v []V) {
m.Keys.Append(k)
m.Values.Append(v)
}
func NewMapPair[X comparable](v proto.ColumnOf[X]) MapPair[X] {
return MapPair[X]{
Keys: new(proto.ColStr).LowCardinality().Array(),
Values: proto.NewArray(v),
}
}
type MapPairs struct {
Strings MapPair[string]
Ints MapPair[int64]
}
func NewMapPairs() MapPairs {
return MapPairs{
Strings: NewMapPair[string](&proto.ColStr{}),
Ints: NewMapPair[int64](&proto.ColInt64{}),
}
}
// OTEL is OpenTelemetry log model.
type OTEL struct {
Body proto.ColStr
Timestamp proto.ColDateTime64
SevText proto.ColEnum
SevNumber proto.ColUInt8
TraceID proto.ColFixedStr
SpanID proto.ColFixedStr
// K(String):V(String) for attributes.
Attr MapPairs
Res MapPairs
}
func (t *OTEL) Input() proto.Input {
return proto.Input{
{Name: "body", Data: t.Body},
{Name: "timestamp", Data: t.Timestamp},
{Name: "trace_id", Data: t.TraceID},
{Name: "span_id", Data: t.SpanID},
{Name: "severity_text", Data: &t.SevText},
{Name: "severity_number", Data: t.SevNumber},
{Name: "attr_str_keys", Data: t.Attr.Strings.Keys},
{Name: "attr_str_values", Data: t.Attr.Strings.Values},
{Name: "res_str_keys", Data: t.Res.Strings.Keys},
{Name: "res_str_values", Data: t.Res.Strings.Values},
}
}
type OTELRow struct {
Body []byte
Timestamp int64
SeverityNumber byte
SeverityText string
AttrKeys []string
AttrValues []string
ResKeys []string
ResValues []string
TraceID [16]byte
SpanID [8]byte
}
func (t *OTEL) Append(row OTELRow) {
t.Body.AppendBytes(row.Body)
t.Timestamp.AppendRaw(proto.DateTime64(row.Timestamp))
t.SevNumber.Append(row.SeverityNumber)
t.SevText.Append(row.SeverityText)
t.TraceID.Append(row.TraceID[:])
t.SpanID.Append(row.SpanID[:])
t.Res.Strings.Append(row.ResKeys, row.ResValues)
t.Attr.Strings.Append(row.AttrKeys, row.AttrValues)
}
func NewOTEL() *OTEL {
t := &OTEL{
TraceID: proto.ColFixedStr{Size: 16},
SpanID: proto.ColFixedStr{Size: 8},
Res: NewMapPairs(),
Attr: NewMapPairs(),
}
if err := t.SevText.Infer(`Enum8('TRACE'=1, 'DEBUG'=2, 'INFO'=3, 'WARN'=4, 'ERROR'=5, 'FATAL'=6)`); err != nil {
panic(err)
}
return t
}
func BenchmarkOTEL(b *testing.B) {
b.Run("Encode", func(b *testing.B) {
buf := new(proto.Buffer)
const rows = 1000
buf.PutString("") // no temp table
data := NewOTEL()
for i := uint64(0); i < rows; i++ {
data.Append(OTELRow{
Body: []byte("20200415T072306-0700 INFO I like donuts"),
SeverityNumber: 9,
SeverityText: "INFO",
Timestamp: 1586960586000000000,
TraceID: [16]byte{12, 34},
SpanID: [8]byte{56, 78},
AttrKeys: []string{
"http.status",
"http.url",
"my.custom.application.tag",
},
AttrValues: []string{
"Internal Server Error",
"https://example.com",
"hello",
},
ResKeys: []string{
"service.name",
"service.version",
"k8s.pod.uid",
},
ResValues: []string{
"donut_shop",
"2.0.0",
"1138528c-c36e-11e9-a1a7-42010a800198",
},
})
}
block := proto.Block{
Info: proto.BlockInfo{BucketNum: -1},
Columns: len(data.Input()),
Rows: rows,
}
input := data.Input()
require.NoError(b, block.EncodeBlock(buf, proto.Version, input))
b.ReportAllocs()
b.ResetTimer()
b.SetBytes(int64(len(buf.Buf)))
for i := 0; i < b.N; i++ {
buf.Reset()
buf.PutString("")
if err := block.EncodeBlock(buf, proto.Version, input); err != nil {
b.Fatal(err)
}
}
})
}