-
Notifications
You must be signed in to change notification settings - Fork 999
/
log.go
396 lines (368 loc) · 10.4 KB
/
log.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Package transform provides transformation functionality from the
// sdk/log data-types into OTLP data-types.
package transform // import "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp/internal/transform"
import (
"sync"
"time"
cpb "go.opentelemetry.io/proto/slim/otlp/common/v1"
lpb "go.opentelemetry.io/proto/slim/otlp/logs/v1"
rpb "go.opentelemetry.io/proto/slim/otlp/resource/v1"
"go.opentelemetry.io/otel/attribute"
api "go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/log"
)
var resourceLogsPool = sync.Pool{
New: func() any {
return []*lpb.ResourceLogs{}
},
}
// ResourceLogs returns an slice of OTLP ResourceLogs generated from records.
func ResourceLogs(records []log.Record) (out []*lpb.ResourceLogs, free func()) {
if len(records) == 0 {
return nil, func() {}
}
resMap := resourceLogsMap(records)
out = resourceLogsPool.Get().([]*lpb.ResourceLogs)
for _, rl := range resMap {
out = append(out, rl)
}
return out, func() {
out = out[:0:0]
resourceLogsPool.Put(out)
}
}
func resourceLogsMap(records []log.Record) map[attribute.Distinct]*lpb.ResourceLogs {
out := make(map[attribute.Distinct]*lpb.ResourceLogs)
for _, r := range records {
res := r.Resource()
rl, ok := out[res.Equivalent()]
if !ok {
rl = new(lpb.ResourceLogs)
if res.Len() > 0 {
rl.Resource = &rpb.Resource{
Attributes: AttrIter(res.Iter()),
}
}
rl.SchemaUrl = res.SchemaURL()
out[res.Equivalent()] = rl
}
rl.ScopeLogs = ScopeLogs(records)
}
return out
}
// ScopeLogs returns a slice of OTLP ScopeLogs generated from recoreds.
func ScopeLogs(records []log.Record) []*lpb.ScopeLogs {
scopeMap := scopeLogsMap(records)
out := make([]*lpb.ScopeLogs, 0, len(scopeMap))
for _, sl := range scopeMap {
out = append(out, sl)
}
return out
}
func scopeLogsMap(records []log.Record) map[instrumentation.Scope]*lpb.ScopeLogs {
out := make(map[instrumentation.Scope]*lpb.ScopeLogs)
for _, r := range records {
scope := r.InstrumentationScope()
sl, ok := out[scope]
if !ok {
sl = new(lpb.ScopeLogs)
var emptyScope instrumentation.Scope
if scope != emptyScope {
sl.Scope = &cpb.InstrumentationScope{
Name: scope.Name,
Version: scope.Version,
}
sl.SchemaUrl = scope.SchemaURL
}
out[scope] = sl
}
sl.LogRecords = append(sl.LogRecords, LogRecord(r))
}
return out
}
// LogRecord returns an OTLP LogRecord generated from record.
func LogRecord(record log.Record) *lpb.LogRecord {
r := &lpb.LogRecord{
TimeUnixNano: timeUnixNano(record.Timestamp()),
ObservedTimeUnixNano: timeUnixNano(record.ObservedTimestamp()),
SeverityNumber: SeverityNumber(record.Severity()),
SeverityText: record.SeverityText(),
Body: LogAttrValue(record.Body()),
Attributes: make([]*cpb.KeyValue, 0, record.AttributesLen()),
Flags: uint32(record.TraceFlags()),
// TODO: DroppedAttributesCount: /* ... */,
}
record.WalkAttributes(func(kv api.KeyValue) bool {
r.Attributes = append(r.Attributes, LogAttr(kv))
return true
})
if tID := record.TraceID(); tID.IsValid() {
r.TraceId = tID[:]
}
if sID := record.SpanID(); sID.IsValid() {
r.SpanId = sID[:]
}
return r
}
// timeUnixNano returns t as a Unix time, the number of nanoseconds elapsed
// since January 1, 1970 UTC as uint64. The result is undefined if the Unix
// time in nanoseconds cannot be represented by an int64 (a date before the
// year 1678 or after 2262). timeUnixNano on the zero Time returns 0. The
// result does not depend on the location associated with t.
func timeUnixNano(t time.Time) uint64 {
if t.IsZero() {
return 0
}
return uint64(t.UnixNano())
}
// AttrIter transforms an [attribute.Iterator] into OTLP key-values.
func AttrIter(iter attribute.Iterator) []*cpb.KeyValue {
l := iter.Len()
if l == 0 {
return nil
}
out := make([]*cpb.KeyValue, 0, l)
for iter.Next() {
out = append(out, Attr(iter.Attribute()))
}
return out
}
// Attrs transforms a slice of [attribute.KeyValue] into OTLP key-values.
func Attrs(attrs []attribute.KeyValue) []*cpb.KeyValue {
if len(attrs) == 0 {
return nil
}
out := make([]*cpb.KeyValue, 0, len(attrs))
for _, kv := range attrs {
out = append(out, Attr(kv))
}
return out
}
// Attr transforms an [attribute.KeyValue] into an OTLP key-value.
func Attr(kv attribute.KeyValue) *cpb.KeyValue {
return &cpb.KeyValue{Key: string(kv.Key), Value: AttrValue(kv.Value)}
}
// AttrValue transforms an [attribute.Value] into an OTLP AnyValue.
func AttrValue(v attribute.Value) *cpb.AnyValue {
av := new(cpb.AnyValue)
switch v.Type() {
case attribute.BOOL:
av.Value = &cpb.AnyValue_BoolValue{
BoolValue: v.AsBool(),
}
case attribute.BOOLSLICE:
av.Value = &cpb.AnyValue_ArrayValue{
ArrayValue: &cpb.ArrayValue{
Values: boolSliceValues(v.AsBoolSlice()),
},
}
case attribute.INT64:
av.Value = &cpb.AnyValue_IntValue{
IntValue: v.AsInt64(),
}
case attribute.INT64SLICE:
av.Value = &cpb.AnyValue_ArrayValue{
ArrayValue: &cpb.ArrayValue{
Values: int64SliceValues(v.AsInt64Slice()),
},
}
case attribute.FLOAT64:
av.Value = &cpb.AnyValue_DoubleValue{
DoubleValue: v.AsFloat64(),
}
case attribute.FLOAT64SLICE:
av.Value = &cpb.AnyValue_ArrayValue{
ArrayValue: &cpb.ArrayValue{
Values: float64SliceValues(v.AsFloat64Slice()),
},
}
case attribute.STRING:
av.Value = &cpb.AnyValue_StringValue{
StringValue: v.AsString(),
}
case attribute.STRINGSLICE:
av.Value = &cpb.AnyValue_ArrayValue{
ArrayValue: &cpb.ArrayValue{
Values: stringSliceValues(v.AsStringSlice()),
},
}
default:
av.Value = &cpb.AnyValue_StringValue{
StringValue: "INVALID",
}
}
return av
}
func boolSliceValues(vals []bool) []*cpb.AnyValue {
converted := make([]*cpb.AnyValue, len(vals))
for i, v := range vals {
converted[i] = &cpb.AnyValue{
Value: &cpb.AnyValue_BoolValue{
BoolValue: v,
},
}
}
return converted
}
func int64SliceValues(vals []int64) []*cpb.AnyValue {
converted := make([]*cpb.AnyValue, len(vals))
for i, v := range vals {
converted[i] = &cpb.AnyValue{
Value: &cpb.AnyValue_IntValue{
IntValue: v,
},
}
}
return converted
}
func float64SliceValues(vals []float64) []*cpb.AnyValue {
converted := make([]*cpb.AnyValue, len(vals))
for i, v := range vals {
converted[i] = &cpb.AnyValue{
Value: &cpb.AnyValue_DoubleValue{
DoubleValue: v,
},
}
}
return converted
}
func stringSliceValues(vals []string) []*cpb.AnyValue {
converted := make([]*cpb.AnyValue, len(vals))
for i, v := range vals {
converted[i] = &cpb.AnyValue{
Value: &cpb.AnyValue_StringValue{
StringValue: v,
},
}
}
return converted
}
// Attrs transforms a slice of [api.KeyValue] into OTLP key-values.
func LogAttrs(attrs []api.KeyValue) []*cpb.KeyValue {
if len(attrs) == 0 {
return nil
}
out := make([]*cpb.KeyValue, 0, len(attrs))
for _, kv := range attrs {
out = append(out, LogAttr(kv))
}
return out
}
// LogAttr transforms an [api.KeyValue] into an OTLP key-value.
func LogAttr(attr api.KeyValue) *cpb.KeyValue {
return &cpb.KeyValue{
Key: attr.Key,
Value: LogAttrValue(attr.Value),
}
}
// LogAttrValues transforms a slice of [api.Value] into an OTLP []AnyValue.
func LogAttrValues(vals []api.Value) []*cpb.AnyValue {
if len(vals) == 0 {
return nil
}
out := make([]*cpb.AnyValue, 0, len(vals))
for _, v := range vals {
out = append(out, LogAttrValue(v))
}
return out
}
// LogAttrValue transforms an [api.Value] into an OTLP AnyValue.
func LogAttrValue(v api.Value) *cpb.AnyValue {
av := new(cpb.AnyValue)
switch v.Kind() {
case api.KindBool:
av.Value = &cpb.AnyValue_BoolValue{
BoolValue: v.AsBool(),
}
case api.KindInt64:
av.Value = &cpb.AnyValue_IntValue{
IntValue: v.AsInt64(),
}
case api.KindFloat64:
av.Value = &cpb.AnyValue_DoubleValue{
DoubleValue: v.AsFloat64(),
}
case api.KindString:
av.Value = &cpb.AnyValue_StringValue{
StringValue: v.AsString(),
}
case api.KindBytes:
av.Value = &cpb.AnyValue_BytesValue{
BytesValue: v.AsBytes(),
}
case api.KindSlice:
av.Value = &cpb.AnyValue_ArrayValue{
ArrayValue: &cpb.ArrayValue{
Values: LogAttrValues(v.AsSlice()),
},
}
case api.KindMap:
av.Value = &cpb.AnyValue_KvlistValue{
KvlistValue: &cpb.KeyValueList{
Values: LogAttrs(v.AsMap()),
},
}
default:
av.Value = &cpb.AnyValue_StringValue{
StringValue: "INVALID",
}
}
return av
}
// SeverityNumber transforms a [log.Severity] into an OTLP SeverityNumber.
func SeverityNumber(s api.Severity) lpb.SeverityNumber {
switch s {
case api.SeverityTrace:
return lpb.SeverityNumber_SEVERITY_NUMBER_TRACE
case api.SeverityTrace2:
return lpb.SeverityNumber_SEVERITY_NUMBER_TRACE2
case api.SeverityTrace3:
return lpb.SeverityNumber_SEVERITY_NUMBER_TRACE3
case api.SeverityTrace4:
return lpb.SeverityNumber_SEVERITY_NUMBER_TRACE4
case api.SeverityDebug:
return lpb.SeverityNumber_SEVERITY_NUMBER_DEBUG
case api.SeverityDebug2:
return lpb.SeverityNumber_SEVERITY_NUMBER_DEBUG2
case api.SeverityDebug3:
return lpb.SeverityNumber_SEVERITY_NUMBER_DEBUG3
case api.SeverityDebug4:
return lpb.SeverityNumber_SEVERITY_NUMBER_DEBUG4
case api.SeverityInfo:
return lpb.SeverityNumber_SEVERITY_NUMBER_INFO
case api.SeverityInfo2:
return lpb.SeverityNumber_SEVERITY_NUMBER_INFO2
case api.SeverityInfo3:
return lpb.SeverityNumber_SEVERITY_NUMBER_INFO3
case api.SeverityInfo4:
return lpb.SeverityNumber_SEVERITY_NUMBER_INFO4
case api.SeverityWarn:
return lpb.SeverityNumber_SEVERITY_NUMBER_WARN
case api.SeverityWarn2:
return lpb.SeverityNumber_SEVERITY_NUMBER_WARN2
case api.SeverityWarn3:
return lpb.SeverityNumber_SEVERITY_NUMBER_WARN3
case api.SeverityWarn4:
return lpb.SeverityNumber_SEVERITY_NUMBER_WARN4
case api.SeverityError:
return lpb.SeverityNumber_SEVERITY_NUMBER_ERROR
case api.SeverityError2:
return lpb.SeverityNumber_SEVERITY_NUMBER_ERROR2
case api.SeverityError3:
return lpb.SeverityNumber_SEVERITY_NUMBER_ERROR3
case api.SeverityError4:
return lpb.SeverityNumber_SEVERITY_NUMBER_ERROR4
case api.SeverityFatal:
return lpb.SeverityNumber_SEVERITY_NUMBER_FATAL
case api.SeverityFatal2:
return lpb.SeverityNumber_SEVERITY_NUMBER_FATAL2
case api.SeverityFatal3:
return lpb.SeverityNumber_SEVERITY_NUMBER_FATAL3
case api.SeverityFatal4:
return lpb.SeverityNumber_SEVERITY_NUMBER_FATAL4
}
return lpb.SeverityNumber_SEVERITY_NUMBER_UNSPECIFIED
}