-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
logs_exporter.go
72 lines (60 loc) · 2.06 KB
/
logs_exporter.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2021-present Datadog, Inc.
package logsagentexporter
import (
"context"
"errors"
"strings"
"time"
"github.com/DataDog/datadog-agent/pkg/logs/message"
"github.com/DataDog/datadog-agent/pkg/logs/sources"
"github.com/DataDog/datadog-agent/pkg/util/scrubber"
"go.uber.org/zap"
logsmapping "github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/logs"
"go.opentelemetry.io/collector/pdata/plog"
)
// otelTag specifies a tag to be added to all logs sent from the Datadog Agent
const otelTag = "otel_source:datadog_agent"
// createConsumeLogsFunc returns an implementation of consumer.ConsumeLogsFunc
func createConsumeLogsFunc(logger *zap.Logger, logSource *sources.LogSource, logsAgentChannel chan *message.Message) func(context.Context, plog.Logs) error {
return func(_ context.Context, ld plog.Logs) (err error) {
defer func() {
if err != nil {
newErr, scrubbingErr := scrubber.ScrubString(err.Error())
if scrubbingErr != nil {
err = scrubbingErr
} else {
err = errors.New(newErr)
}
}
}()
tr, _ := logsmapping.NewTranslator(...)
ddLogs := tr.MapLogs(ld)
for _, ddLog := range ddLogs {
ddLog.Ddtags = nil
service := ""
if ddLog.Service != nil {
service = *ddLog.Service
}
status := ddLog.AdditionalProperties["status"]
if status == "" {
status = message.StatusInfo
}
origin := message.NewOrigin(logSource)
origin.SetTags(tags)
origin.SetService(service)
origin.SetSource(logSourceName)
content, err := ddLog.MarshalJSON()
if err != nil {
logger.Error("Error parsing log: " + err.Error())
}
// ingestionTs is an internal field used for latency tracking on the status page, not the actual log timestamp.
ingestionTs := time.Now().UnixNano()
message := message.NewMessage(content, origin, status, ingestionTs)
logsAgentChannel <- message
}
return nil
}
}