-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathlogs_exporter.go
162 lines (146 loc) · 6.32 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
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package datadogexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter"
import (
"context"
"fmt"
"sync"
"github.com/DataDog/datadog-agent/comp/logs/agent/config"
"github.com/DataDog/datadog-agent/comp/otelcol/logsagentpipeline"
"github.com/DataDog/datadog-agent/comp/otelcol/logsagentpipeline/logsagentpipelineimpl"
"github.com/DataDog/datadog-agent/comp/otelcol/otlp/components/exporter/logsagentexporter"
"github.com/DataDog/datadog-agent/pkg/logs/sources"
"github.com/DataDog/opentelemetry-mapping-go/pkg/inframetadata"
"github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes"
"github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes/source"
logsmapping "github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/logs"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/clientutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/hostmetadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/logs"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/scrub"
)
const (
// logSourceName specifies the Datadog source tag value to be added to logs sent from the Datadog exporter.
logSourceName = "otlp_log_ingestion"
// otelSource specifies a source to be added to all logs sent from the Datadog exporter. The tag has key `otel_source` and the value specified on this constant.
otelSource = "datadog_exporter"
)
type logsExporter struct {
params exporter.Settings
cfg *Config
ctx context.Context // ctx triggers shutdown upon cancellation
scrubber scrub.Scrubber // scrubber scrubs sensitive information from error messages
translator *logsmapping.Translator
sender *logs.Sender
onceMetadata *sync.Once
sourceProvider source.Provider
metadataReporter *inframetadata.Reporter
}
// newLogsExporter creates a new instance of logsExporter
func newLogsExporter(
ctx context.Context,
params exporter.Settings,
cfg *Config,
onceMetadata *sync.Once,
attributesTranslator *attributes.Translator,
sourceProvider source.Provider,
metadataReporter *inframetadata.Reporter,
) (*logsExporter, error) {
// create Datadog client
// validation endpoint is provided by Metrics
errchan := make(chan error)
if isMetricExportV2Enabled() {
apiClient := clientutil.CreateAPIClient(
params.BuildInfo,
cfg.Metrics.TCPAddrConfig.Endpoint,
cfg.ClientConfig)
go func() { errchan <- clientutil.ValidateAPIKey(ctx, string(cfg.API.Key), params.Logger, apiClient) }()
} else {
client := clientutil.CreateZorkianClient(string(cfg.API.Key), cfg.Metrics.TCPAddrConfig.Endpoint)
go func() { errchan <- clientutil.ValidateAPIKeyZorkian(params.Logger, client) }()
}
// validate the apiKey
if cfg.API.FailOnInvalidKey {
if err := <-errchan; err != nil {
return nil, err
}
}
translator, err := logsmapping.NewTranslator(params.TelemetrySettings, attributesTranslator, otelSource)
if err != nil {
return nil, fmt.Errorf("failed to create logs translator: %w", err)
}
s := logs.NewSender(cfg.Logs.TCPAddrConfig.Endpoint, params.Logger, cfg.ClientConfig, cfg.Logs.DumpPayloads, string(cfg.API.Key))
return &logsExporter{
params: params,
cfg: cfg,
ctx: ctx,
translator: translator,
sender: s,
onceMetadata: onceMetadata,
scrubber: scrub.NewScrubber(),
sourceProvider: sourceProvider,
metadataReporter: metadataReporter,
}, nil
}
var _ consumer.ConsumeLogsFunc = (*logsExporter)(nil).consumeLogs
// consumeLogs is implementation of consumer.ConsumeLogsFunc
func (exp *logsExporter) consumeLogs(ctx context.Context, ld plog.Logs) (err error) {
defer func() { err = exp.scrubber.Scrub(err) }()
if exp.cfg.HostMetadata.Enabled {
// start host metadata with resource attributes from
// the first payload.
exp.onceMetadata.Do(func() {
attrs := pcommon.NewMap()
if ld.ResourceLogs().Len() > 0 {
attrs = ld.ResourceLogs().At(0).Resource().Attributes()
}
go hostmetadata.RunPusher(exp.ctx, exp.params, newMetadataConfigfromConfig(exp.cfg), exp.sourceProvider, attrs, exp.metadataReporter)
})
// Consume resources for host metadata
for i := 0; i < ld.ResourceLogs().Len(); i++ {
res := ld.ResourceLogs().At(i).Resource()
consumeResource(exp.metadataReporter, res, exp.params.Logger)
}
}
payloads := exp.translator.MapLogs(ctx, ld)
return exp.sender.SubmitLogs(exp.ctx, payloads)
}
// newLogsAgentExporter creates new instances of the logs agent and the logs agent exporter
func newLogsAgentExporter(
ctx context.Context,
params exporter.Settings,
cfg *Config,
sourceProvider source.Provider,
) (logsagentpipeline.LogsAgent, *logsagentexporter.Exporter, error) {
logComponent := newLogComponent(params.TelemetrySettings)
cfgComponent := newConfigComponent(params.TelemetrySettings, cfg)
logsAgentConfig := &logsagentexporter.Config{
OtelSource: otelSource,
LogSourceName: logSourceName,
}
hostnameComponent := logs.NewHostnameService(sourceProvider)
logsAgent := logsagentpipelineimpl.NewLogsAgent(logsagentpipelineimpl.Dependencies{
Log: logComponent,
Config: cfgComponent,
Hostname: hostnameComponent,
})
err := logsAgent.Start(ctx)
if err != nil {
return nil, &logsagentexporter.Exporter{}, fmt.Errorf("failed to create logs agent: %w", err)
}
pipelineChan := logsAgent.GetPipelineProvider().NextPipelineChan()
logSource := sources.NewLogSource(logsAgentConfig.LogSourceName, &config.LogsConfig{})
attributesTranslator, err := attributes.NewTranslator(params.TelemetrySettings)
if err != nil {
return nil, &logsagentexporter.Exporter{}, fmt.Errorf("failed to create attribute translator: %w", err)
}
logsAgentExporter, err := logsagentexporter.NewExporter(params.TelemetrySettings, logsAgentConfig, logSource, pipelineChan, attributesTranslator)
if err != nil {
return nil, &logsagentexporter.Exporter{}, fmt.Errorf("failed to create logs agent exporter: %w", err)
}
return logsAgent, logsAgentExporter, nil
}