/
logs.go
542 lines (486 loc) · 16.1 KB
/
logs.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package qrynexporter
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/go-logfmt/logfmt"
"github.com/prometheus/common/model"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/otel/metric"
"go.uber.org/zap"
)
const (
hintAttributes = "loki.attribute.labels"
hintResources = "loki.resource.labels"
hintTenant = "loki.tenant"
hintFormat = "loki.format"
levelAttributeName = "level"
formatRaw string = "raw"
formatJSON string = "json"
formatLogfmt string = "logfmt"
)
type logsExporter struct {
logger *zap.Logger
meter metric.Meter
db clickhouse.Conn
attributeLabels string
resourceLabels string
format string
cluster bool
}
func newLogsExporter(logger *zap.Logger, cfg *Config, set *exporter.CreateSettings) (*logsExporter, error) {
opts, err := clickhouse.ParseDSN(cfg.DSN)
if err != nil {
return nil, err
}
db, err := clickhouse.Open(opts)
if err != nil {
return nil, err
}
exp := &logsExporter{
logger: logger,
meter: set.MeterProvider.Meter(typeStr),
db: db,
format: cfg.Logs.Format,
attributeLabels: cfg.Logs.AttributeLabels,
resourceLabels: cfg.Logs.ResourceLabels,
cluster: cfg.ClusteredClickhouse,
}
if err := initMetrics(exp.meter); err != nil {
exp.logger.Error(fmt.Sprintf("failed to init metrics: %s", err.Error()))
return exp, err
}
return exp, nil
}
// Shutdown will shutdown the exporter.
func (e *logsExporter) Shutdown(_ context.Context) error {
if e.db != nil {
e.db.Close()
}
return nil
}
func hasLokiHint(attrs pcommon.Map) bool {
for _, hint := range []string{hintAttributes, hintResources, hintTenant} {
if _, ok := attrs.Get(hint); ok {
return true
}
}
return false
}
func (e *logsExporter) convertAttributesAndMerge(logAttrs pcommon.Map, resAttrs pcommon.Map) model.LabelSet {
out := model.LabelSet{}
if resourcesToLabel, found := resAttrs.Get(hintResources); found {
labels := convertSelectedAttributesToLabels(resAttrs, resourcesToLabel)
out = out.Merge(labels)
}
// get the hint from the log attributes, not from the resource
// the value can be a single resource name to use as label
// or a slice of string values
if resourcesToLabel, found := logAttrs.Get(hintResources); found {
labels := convertSelectedAttributesToLabels(resAttrs, resourcesToLabel)
out = out.Merge(labels)
}
if attributesToLabel, found := logAttrs.Get(hintAttributes); found {
labels := convertSelectedAttributesToLabels(logAttrs, attributesToLabel)
out = out.Merge(labels)
}
// get tenant hint from resource attributes, fallback to record attributes
// if it is not found
if resourcesToLabel, found := resAttrs.Get(hintTenant); !found {
if attributesToLabel, found := logAttrs.Get(hintTenant); found {
labels := convertSelectedAttributesToLabels(logAttrs, attributesToLabel)
out = out.Merge(labels)
}
} else {
labels := convertSelectedAttributesToLabels(resAttrs, resourcesToLabel)
out = out.Merge(labels)
}
if e.attributeLabels != "" {
labels := convertSelectedAttributesToLabels(logAttrs, pcommon.NewValueStr(e.resourceLabels))
out = out.Merge(labels)
}
if e.resourceLabels != "" {
labels := convertSelectedAttributesToLabels(resAttrs, pcommon.NewValueStr(e.attributeLabels))
out = out.Merge(labels)
}
// if no any hint found, just mergedLabels
if !hasLokiHint(logAttrs) && !hasLokiHint(resAttrs) {
out = out.Merge(convertAttributesToLabels(logAttrs))
out = out.Merge(convertAttributesToLabels(resAttrs))
}
return out
}
func parseAttributeNames(attrsToSelect pcommon.Value) []string {
var out []string
switch attrsToSelect.Type() {
case pcommon.ValueTypeStr:
out = strings.Split(attrsToSelect.AsString(), ",")
case pcommon.ValueTypeSlice:
as := attrsToSelect.Slice().AsRaw()
for _, a := range as {
out = append(out, fmt.Sprintf("%v", a))
}
default:
// trying to make the most of bad data
out = append(out, attrsToSelect.AsString())
}
return out
}
func convertSelectedAttributesToLabels(attributes pcommon.Map, attrsToSelect pcommon.Value) model.LabelSet {
out := model.LabelSet{}
attrs := parseAttributeNames(attrsToSelect)
for _, attr := range attrs {
attr = strings.TrimSpace(attr)
av, ok := attributes.Get(attr) // do we need to trim this?
if ok {
out[model.LabelName(attr)] = model.LabelValue(av.AsString())
}
}
return out
}
func getFormatFromFormatHint(logAttr pcommon.Map, resourceAttr pcommon.Map) string {
format := formatRaw
formatVal, found := resourceAttr.Get(hintFormat)
if !found {
formatVal, found = logAttr.Get(hintFormat)
}
if found {
format = formatVal.AsString()
}
return format
}
func removeAttributes(attrs pcommon.Map, labels model.LabelSet) {
attrs.RemoveIf(func(s string, _ pcommon.Value) bool {
if s == hintAttributes || s == hintResources || s == hintTenant || s == hintFormat {
return true
}
_, exists := labels[model.LabelName(s)]
return exists
})
}
func timestampFromLogRecord(lr plog.LogRecord) time.Time {
if lr.Timestamp() != 0 {
return lr.Timestamp().AsTime()
}
if lr.ObservedTimestamp() != 0 {
return lr.ObservedTimestamp().AsTime()
}
return time.Now()
}
// if given key:value pair already exists in keyvals, replace value. Otherwise append
func keyvalsReplaceOrAppend(keyvals []interface{}, key string, value interface{}) []interface{} {
for i := 0; i < len(keyvals); i += 2 {
if keyvals[i] == key {
keyvals[i+1] = value
return keyvals
}
}
return append(keyvals, key, value)
}
func parseLogfmtLine(line string) (*[]interface{}, error) {
var keyvals []interface{}
decoder := logfmt.NewDecoder(strings.NewReader(line))
decoder.ScanRecord()
for decoder.ScanKeyval() {
keyvals = append(keyvals, decoder.Key(), decoder.Value())
}
err := decoder.Err()
if err != nil {
return nil, err
}
return &keyvals, nil
}
func bodyToKeyvals(body pcommon.Value) []interface{} {
switch body.Type() {
case pcommon.ValueTypeEmpty:
return nil
case pcommon.ValueTypeStr:
// try to parse record body as logfmt, but failing that assume it's plain text
value := body.Str()
keyvals, err := parseLogfmtLine(value)
if err != nil {
return []interface{}{"msg", body.Str()}
}
return *keyvals
case pcommon.ValueTypeMap:
return valueToKeyvals("", body)
case pcommon.ValueTypeSlice:
return valueToKeyvals("body", body)
default:
return []interface{}{"msg", body.AsRaw()}
}
}
func valueToKeyvals(key string, value pcommon.Value) []interface{} {
switch value.Type() {
case pcommon.ValueTypeEmpty:
return nil
case pcommon.ValueTypeStr:
return []interface{}{key, value.Str()}
case pcommon.ValueTypeBool:
return []interface{}{key, value.Bool()}
case pcommon.ValueTypeInt:
return []interface{}{key, value.Int()}
case pcommon.ValueTypeDouble:
return []interface{}{key, value.Double()}
case pcommon.ValueTypeMap:
var keyvals []interface{}
prefix := ""
if key != "" {
prefix = key + "_"
}
value.Map().Range(func(k string, v pcommon.Value) bool {
keyvals = append(keyvals, valueToKeyvals(prefix+k, v)...)
return true
})
return keyvals
case pcommon.ValueTypeSlice:
prefix := ""
if key != "" {
prefix = key + "_"
}
var keyvals []interface{}
for i := 0; i < value.Slice().Len(); i++ {
v := value.Slice().At(i)
keyvals = append(keyvals, valueToKeyvals(fmt.Sprintf("%s%d", prefix, i), v)...)
}
return keyvals
default:
return []interface{}{key, value.AsRaw()}
}
}
type logRecord struct {
Body json.RawMessage `json:"body,omitempty"`
TraceID string `json:"traceid,omitempty"`
SpanID string `json:"spanid,omitempty"`
Severity string `json:"severity,omitempty"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
Resources map[string]interface{} `json:"resources,omitempty"`
}
func convertLogToLine(log plog.LogRecord, res pcommon.Resource, format string) (string, error) {
switch format {
case formatJSON:
var bodyData []byte
var err error
body := log.Body()
switch log.Body().Type() {
case pcommon.ValueTypeEmpty:
case pcommon.ValueTypeStr:
bodyData, err = json.Marshal(body.Str())
case pcommon.ValueTypeInt:
bodyData, err = json.Marshal(body.Int())
case pcommon.ValueTypeDouble:
bodyData, err = json.Marshal(body.Double())
case pcommon.ValueTypeBool:
bodyData, err = json.Marshal(body.Bool())
case pcommon.ValueTypeMap:
bodyData, err = json.Marshal(body.Map().AsRaw())
case pcommon.ValueTypeSlice:
bodyData, err = json.Marshal(body.Slice().AsRaw())
case pcommon.ValueTypeBytes:
bodyData, err = json.Marshal(body.Bytes().AsRaw())
default:
err = fmt.Errorf("unsuported body type to marshal json")
}
if err != nil {
return "", err
}
logRecord := logRecord{
Body: bodyData,
TraceID: log.TraceID().String(),
SpanID: log.SpanID().String(),
Severity: log.SeverityText(),
Attributes: log.Attributes().AsRaw(),
Resources: log.Attributes().AsRaw(),
}
jsonRecord, err := json.Marshal(logRecord)
if err != nil {
return "", err
}
return string(jsonRecord), nil
case formatLogfmt:
keyvals := bodyToKeyvals(log.Body())
if traceID := log.TraceID(); !traceID.IsEmpty() {
keyvals = keyvalsReplaceOrAppend(keyvals, "traceID", log.TraceID().String())
}
if spanID := log.SpanID(); !spanID.IsEmpty() {
keyvals = keyvalsReplaceOrAppend(keyvals, "spanID", log.SpanID().String())
}
severity := log.SeverityText()
if severity != "" {
keyvals = keyvalsReplaceOrAppend(keyvals, "severity", severity)
}
log.Attributes().Range(func(k string, v pcommon.Value) bool {
keyvals = append(keyvals, valueToKeyvals(fmt.Sprintf("attribute_%s", k), v)...)
return true
})
res.Attributes().Range(func(k string, v pcommon.Value) bool {
keyvals = append(keyvals, valueToKeyvals(fmt.Sprintf("resource_%s", k), v)...)
return true
})
logfmtLine, err := logfmt.MarshalKeyvals(keyvals...)
if err != nil {
return "", err
}
return string(logfmtLine), nil
case formatRaw:
return log.Body().AsString(), nil
default:
return "", fmt.Errorf("invalid format %s. Expected one of: %s, %s, %s", format, formatJSON, formatLogfmt, formatRaw)
}
}
func convertLogToSample(fingerprint model.Fingerprint, log plog.LogRecord, res pcommon.Resource, format string) (Sample, error) {
line, err := convertLogToLine(log, res, format)
if err != nil {
return Sample{}, err
}
return Sample{
Fingerprint: uint64(fingerprint),
TimestampNs: timestampFromLogRecord(log).UnixNano(),
String: line,
}, nil
}
func convertLogToTimeSerie(fingerprint model.Fingerprint, log plog.LogRecord, labelSet model.LabelSet) (TimeSerie, error) {
labelsJSON, err := json.Marshal(labelSet)
if err != nil {
return TimeSerie{}, fmt.Errorf("marshal mergedLabels err: %w", err)
}
timeSerie := TimeSerie{
Date: timestampFromLogRecord(log),
Fingerprint: uint64(fingerprint),
Labels: string(labelsJSON),
Name: string(labelSet[model.MetricNameLabel]),
}
return timeSerie, nil
}
func (e *logsExporter) pushLogsData(ctx context.Context, ld plog.Logs) error {
start := time.Now()
var (
samples []Sample
timeSeries []TimeSerie
)
for i := 0; i < ld.ResourceLogs().Len(); i++ {
logs := ld.ResourceLogs().At(i)
for j := 0; j < logs.ScopeLogs().Len(); j++ {
rs := logs.ScopeLogs().At(j).LogRecords()
for k := 0; k < rs.Len(); k++ {
resource := pcommon.NewResource()
logs.Resource().CopyTo(resource)
log := plog.NewLogRecord()
rs.At(k).CopyTo(log)
// adds level attribute from log.severityNumber
addLogLevelAttributeAndHint(log)
format := getFormatFromFormatHint(log.Attributes(), resource.Attributes())
if e.format != "" {
format = e.format
}
mergedLabels := e.convertAttributesAndMerge(log.Attributes(), resource.Attributes())
removeAttributes(log.Attributes(), mergedLabels)
removeAttributes(resource.Attributes(), mergedLabels)
fingerprint := mergedLabels.Fingerprint()
sample, err := convertLogToSample(fingerprint, log, resource, format)
if err != nil {
return fmt.Errorf("convertLogToSample error: %w", err)
}
samples = append(samples, sample)
timeSerie, err := convertLogToTimeSerie(fingerprint, log, mergedLabels)
if err != nil {
return fmt.Errorf("convertLogToTimeSerie error: %w", err)
}
timeSeries = append(timeSeries, timeSerie)
}
}
}
if err := batchSamplesAndTimeSeries(context.WithValue(ctx, "cluster", e.cluster), e.db, samples, timeSeries); err != nil {
otelcolExporterQrynBatchInsertDurationMillis.Record(ctx, time.Now().UnixMilli()-start.UnixMilli(), metric.WithAttributeSet(*newOtelcolAttrSetBatch(errorCodeError, dataTypeLogs)))
e.logger.Error(fmt.Sprintf("failed to insert batch: [%s]", err.Error()))
return err
}
otelcolExporterQrynBatchInsertDurationMillis.Record(ctx, time.Now().UnixMilli()-start.UnixMilli(), metric.WithAttributeSet(*newOtelcolAttrSetBatch(errorCodeSuccess, dataTypeLogs)))
e.logger.Debug("pushLogsData", zap.Int("samples", len(samples)), zap.Int("timeseries", len(timeSeries)), zap.String("cost", time.Since(start).String()))
return nil
}
func batchSamplesAndTimeSeries(ctx context.Context, db clickhouse.Conn, samples []Sample, timeSeries []TimeSerie) error {
isCluster := ctx.Value("cluster").(bool)
samplesBatch, err := db.PrepareBatch(ctx, samplesSQL(isCluster))
if err != nil {
return err
}
for _, sample := range samples {
if err := samplesBatch.AppendStruct(&sample); err != nil {
return err
}
}
if err := samplesBatch.Send(); err != nil {
return err
}
timeSeriesBatch, err := db.PrepareBatch(ctx, TimeSerieSQL(isCluster))
if err != nil {
return err
}
for _, timeSerie := range timeSeries {
if err := timeSeriesBatch.AppendStruct(&timeSerie); err != nil {
return err
}
}
if err := timeSeriesBatch.Send(); err != nil {
return err
}
return nil
}
func convertAttributesToLabels(attributes pcommon.Map) model.LabelSet {
ls := model.LabelSet{}
attributes.Range(func(k string, v pcommon.Value) bool {
ls[model.LabelName(k)] = model.LabelValue(v.AsString())
return true
})
return ls
}
func addLogLevelAttributeAndHint(log plog.LogRecord) {
if log.SeverityNumber() == plog.SeverityNumberUnspecified {
return
}
addLogLevelHit(log)
if _, found := log.Attributes().Get(levelAttributeName); !found {
level := severityNumberToLevel[log.SeverityNumber().String()]
log.Attributes().PutStr(levelAttributeName, level)
}
}
func addLogLevelHit(log plog.LogRecord) {
if value, found := log.Attributes().Get(hintAttributes); found && !strings.Contains(value.AsString(), levelAttributeName) {
log.Attributes().PutStr(hintAttributes, fmt.Sprintf("%s,%s", value.AsString(), levelAttributeName))
} else {
log.Attributes().PutStr(hintAttributes, levelAttributeName)
}
}
var severityNumberToLevel = map[string]string{
plog.SeverityNumberUnspecified.String(): "UNSPECIFIED",
plog.SeverityNumberTrace.String(): "TRACE",
plog.SeverityNumberTrace2.String(): "TRACE2",
plog.SeverityNumberTrace3.String(): "TRACE3",
plog.SeverityNumberTrace4.String(): "TRACE4",
plog.SeverityNumberDebug.String(): "DEBUG",
plog.SeverityNumberDebug2.String(): "DEBUG2",
plog.SeverityNumberDebug3.String(): "DEBUG3",
plog.SeverityNumberDebug4.String(): "DEBUG4",
plog.SeverityNumberInfo.String(): "INFO",
plog.SeverityNumberInfo2.String(): "INFO2",
plog.SeverityNumberInfo3.String(): "INFO3",
plog.SeverityNumberInfo4.String(): "INFO4",
plog.SeverityNumberWarn.String(): "WARN",
plog.SeverityNumberWarn2.String(): "WARN2",
plog.SeverityNumberWarn3.String(): "WARN3",
plog.SeverityNumberWarn4.String(): "WARN4",
plog.SeverityNumberError.String(): "ERROR",
plog.SeverityNumberError2.String(): "ERROR2",
plog.SeverityNumberError3.String(): "ERROR3",
plog.SeverityNumberError4.String(): "ERROR4",
plog.SeverityNumberFatal.String(): "FATAL",
plog.SeverityNumberFatal2.String(): "FATAL2",
plog.SeverityNumberFatal3.String(): "FATAL3",
plog.SeverityNumberFatal4.String(): "FATAL4",
}