-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
factory.go
510 lines (459 loc) · 16.1 KB
/
factory.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
// 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"
"runtime"
"sync"
"time"
pb "github.com/DataDog/datadog-agent/pkg/proto/pbgo/trace"
"github.com/DataDog/datadog-agent/pkg/trace/agent"
"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"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/configretry"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/otel/metric/noop"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/hostmetadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/datadog"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/resourcetotelemetry"
)
var metricExportNativeClientFeatureGate = featuregate.GlobalRegistry().MustRegister(
"exporter.datadogexporter.metricexportnativeclient",
featuregate.StageBeta,
featuregate.WithRegisterDescription("When enabled, metric export in datadogexporter uses native Datadog client APIs instead of Zorkian APIs."),
)
// noAPMStatsFeatureGate causes the trace consumer to skip APM stats computation.
var noAPMStatsFeatureGate = featuregate.GlobalRegistry().MustRegister(
"exporter.datadogexporter.DisableAPMStats",
featuregate.StageAlpha,
featuregate.WithRegisterDescription("Datadog Exporter will not compute APM Stats"),
)
// isMetricExportV2Enabled returns true if metric export in datadogexporter uses native Datadog client APIs, false if it uses Zorkian APIs
func isMetricExportV2Enabled() bool {
return metricExportNativeClientFeatureGate.IsEnabled()
}
// enableNativeMetricExport switches metric export to call native Datadog APIs instead of Zorkian APIs.
func enableNativeMetricExport() error {
return featuregate.GlobalRegistry().Set(metricExportNativeClientFeatureGate.ID(), true)
}
// enableZorkianMetricExport switches metric export to call Zorkian APIs instead of native Datadog APIs.
func enableZorkianMetricExport() error {
return featuregate.GlobalRegistry().Set(metricExportNativeClientFeatureGate.ID(), false)
}
const metadataReporterPeriod = 30 * time.Minute
func consumeResource(metadataReporter *inframetadata.Reporter, res pcommon.Resource, logger *zap.Logger) {
if err := metadataReporter.ConsumeResource(res); err != nil {
logger.Warn("failed to consume resource for host metadata", zap.Error(err), zap.Any("resource", res))
}
}
type factory struct {
onceMetadata sync.Once
onceProvider sync.Once
sourceProvider source.Provider
providerErr error
onceReporter sync.Once
onceStopReporter sync.Once
reporter *inframetadata.Reporter
reporterErr error
onceAttributesTranslator sync.Once
attributesTranslator *attributes.Translator
attributesErr error
wg sync.WaitGroup // waits for agent to exit
registry *featuregate.Registry
}
func (f *factory) SourceProvider(set component.TelemetrySettings, configHostname string) (source.Provider, error) {
f.onceProvider.Do(func() {
f.sourceProvider, f.providerErr = hostmetadata.GetSourceProvider(set, configHostname)
})
return f.sourceProvider, f.providerErr
}
func (f *factory) AttributesTranslator(set component.TelemetrySettings) (*attributes.Translator, error) {
f.onceAttributesTranslator.Do(func() {
// disable metrics for the translator
// Metrics are disabled until we figure out the details on how do we want to report the metric.
set.MeterProvider = noop.NewMeterProvider()
f.attributesTranslator, f.attributesErr = attributes.NewTranslator(set)
})
return f.attributesTranslator, f.attributesErr
}
// Reporter builds and returns an *inframetadata.Reporter.
func (f *factory) Reporter(params exporter.CreateSettings, pcfg hostmetadata.PusherConfig) (*inframetadata.Reporter, error) {
f.onceReporter.Do(func() {
pusher := hostmetadata.NewPusher(params, pcfg)
f.reporter, f.reporterErr = inframetadata.NewReporter(params.Logger, pusher, metadataReporterPeriod)
if f.reporterErr == nil {
go func() {
if err := f.reporter.Run(context.Background()); err != nil {
params.Logger.Error("Host metadata reporter failed at runtime", zap.Error(err))
}
}()
}
})
return f.reporter, f.reporterErr
}
// StopReporter stops the host metadata reporter.
func (f *factory) StopReporter() {
// Use onceReporter or wait until it is done
f.onceReporter.Do(func() {})
// Stop the reporter
f.onceStopReporter.Do(func() {
if f.reporterErr == nil && f.reporter != nil {
f.reporter.Stop()
}
})
}
func (f *factory) TraceAgent(ctx context.Context, params exporter.CreateSettings, cfg *Config, sourceProvider source.Provider) (*agent.Agent, error) {
agnt, err := newTraceAgent(ctx, params, cfg, sourceProvider)
if err != nil {
return nil, err
}
f.wg.Add(1)
go func() {
defer f.wg.Done()
agnt.Run()
}()
return agnt, nil
}
func newFactoryWithRegistry(registry *featuregate.Registry) exporter.Factory {
f := &factory{registry: registry}
return exporter.NewFactory(
metadata.Type,
f.createDefaultConfig,
exporter.WithMetrics(f.createMetricsExporter, metadata.MetricsStability),
exporter.WithTraces(f.createTracesExporter, metadata.TracesStability),
exporter.WithLogs(f.createLogsExporter, metadata.LogsStability),
)
}
// NewFactory creates a Datadog exporter factory
func NewFactory() exporter.Factory {
return newFactoryWithRegistry(featuregate.GlobalRegistry())
}
func defaulttimeoutSettings() exporterhelper.TimeoutSettings {
return exporterhelper.TimeoutSettings{
Timeout: 15 * time.Second,
}
}
// createDefaultConfig creates the default exporter configuration
func (f *factory) createDefaultConfig() component.Config {
return &Config{
TimeoutSettings: defaulttimeoutSettings(),
BackOffConfig: configretry.NewDefaultBackOffConfig(),
QueueSettings: exporterhelper.NewDefaultQueueSettings(),
API: APIConfig{
Site: "datadoghq.com",
},
Metrics: MetricsConfig{
TCPAddr: confignet.TCPAddr{
Endpoint: "https://api.datadoghq.com",
},
DeltaTTL: 3600,
ExporterConfig: MetricsExporterConfig{
ResourceAttributesAsTags: false,
InstrumentationScopeMetadataAsTags: false,
},
HistConfig: HistogramConfig{
Mode: "distributions",
SendAggregations: false,
},
SumConfig: SumConfig{
CumulativeMonotonicMode: CumulativeMonotonicSumModeToDelta,
InitialCumulativeMonotonicMode: InitialValueModeAuto,
},
SummaryConfig: SummaryConfig{
Mode: SummaryModeGauges,
},
},
Traces: TracesConfig{
TCPAddr: confignet.TCPAddr{
Endpoint: "https://trace.agent.datadoghq.com",
},
IgnoreResources: []string{},
},
Logs: LogsConfig{
TCPAddr: confignet.TCPAddr{
Endpoint: "https://http-intake.logs.datadoghq.com",
},
},
HostMetadata: HostMetadataConfig{
Enabled: true,
HostnameSource: HostnameSourceConfigOrSystem,
},
}
}
// checkAndCastConfig checks the configuration type and its warnings, and casts it to
// the Datadog Config struct.
func checkAndCastConfig(c component.Config, logger *zap.Logger) *Config {
cfg, ok := c.(*Config)
if !ok {
panic("programming error: config structure is not of type *datadogexporter.Config")
}
cfg.logWarnings(logger)
return cfg
}
func (f *factory) consumeStatsPayload(ctx context.Context, out chan []byte, traceagent *agent.Agent, tracerVersion string, logger *zap.Logger) {
for i := 0; i < runtime.NumCPU(); i++ {
f.wg.Add(1)
go func() {
defer f.wg.Done()
for {
select {
case <-ctx.Done():
return
case msg := <-out:
sp := &pb.StatsPayload{}
err := proto.Unmarshal(msg, sp)
if err != nil {
logger.Error("failed to unmarshal stats payload", zap.Error(err))
continue
}
for _, sc := range sp.Stats {
traceagent.ProcessStats(sc, "", tracerVersion)
}
}
}
}()
}
}
// createMetricsExporter creates a metrics exporter based on this config.
func (f *factory) createMetricsExporter(
ctx context.Context,
set exporter.CreateSettings,
c component.Config,
) (exporter.Metrics, error) {
cfg := checkAndCastConfig(c, set.TelemetrySettings.Logger)
hostProvider, err := f.SourceProvider(set.TelemetrySettings, cfg.Hostname)
if err != nil {
return nil, fmt.Errorf("failed to build hostname provider: %w", err)
}
ctx, cancel := context.WithCancel(ctx)
// cancel() runs on shutdown
var pushMetricsFn consumer.ConsumeMetricsFunc
traceagent, err := f.TraceAgent(ctx, set, cfg, hostProvider)
if err != nil {
cancel()
return nil, fmt.Errorf("failed to start trace-agent: %w", err)
}
var statsOut chan []byte
if datadog.ConnectorPerformanceFeatureGate.IsEnabled() {
statsOut = make(chan []byte, 1000)
statsv := set.BuildInfo.Command + set.BuildInfo.Version
f.consumeStatsPayload(ctx, statsOut, traceagent, statsv, set.Logger)
}
pcfg := newMetadataConfigfromConfig(cfg)
metadataReporter, err := f.Reporter(set, pcfg)
if err != nil {
cancel()
return nil, fmt.Errorf("failed to build host metadata reporter: %w", err)
}
attrsTranslator, err := f.AttributesTranslator(set.TelemetrySettings)
if err != nil {
cancel()
return nil, fmt.Errorf("failed to build attributes translator: %w", err)
}
if cfg.OnlyMetadata {
pushMetricsFn = func(_ context.Context, md pmetric.Metrics) error {
// only sending metadata use only metrics
f.onceMetadata.Do(func() {
attrs := pcommon.NewMap()
if md.ResourceMetrics().Len() > 0 {
attrs = md.ResourceMetrics().At(0).Resource().Attributes()
}
go hostmetadata.RunPusher(ctx, set, pcfg, hostProvider, attrs, metadataReporter)
})
// Consume resources for host metadata
for i := 0; i < md.ResourceMetrics().Len(); i++ {
res := md.ResourceMetrics().At(i).Resource()
consumeResource(metadataReporter, res, set.Logger)
}
return nil
}
} else {
exp, metricsErr := newMetricsExporter(ctx, set, cfg, &f.onceMetadata, attrsTranslator, hostProvider, traceagent, metadataReporter, statsOut)
if metricsErr != nil {
cancel() // first cancel context
f.wg.Wait() // then wait for shutdown
return nil, metricsErr
}
pushMetricsFn = exp.PushMetricsDataScrubbed
}
exporter, err := exporterhelper.NewMetricsExporter(
ctx,
set,
cfg,
pushMetricsFn,
// explicitly disable since we rely on http.Client timeout logic.
exporterhelper.WithTimeout(exporterhelper.TimeoutSettings{Timeout: 0 * time.Second}),
// We use our own custom mechanism for retries, since we hit several endpoints.
exporterhelper.WithRetry(configretry.BackOffConfig{Enabled: false}),
// The metrics remapping code mutates data
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: true}),
exporterhelper.WithQueue(cfg.QueueSettings),
exporterhelper.WithShutdown(func(context.Context) error {
cancel()
f.StopReporter()
if statsOut != nil {
close(statsOut)
}
return nil
}),
)
if err != nil {
return nil, err
}
return resourcetotelemetry.WrapMetricsExporter(
resourcetotelemetry.Settings{Enabled: cfg.Metrics.ExporterConfig.ResourceAttributesAsTags}, exporter), nil
}
// createTracesExporter creates a trace exporter based on this config.
func (f *factory) createTracesExporter(
ctx context.Context,
set exporter.CreateSettings,
c component.Config,
) (exporter.Traces, error) {
cfg := checkAndCastConfig(c, set.TelemetrySettings.Logger)
var (
pusher consumer.ConsumeTracesFunc
stop component.ShutdownFunc
)
hostProvider, err := f.SourceProvider(set.TelemetrySettings, cfg.Hostname)
if err != nil {
return nil, fmt.Errorf("failed to build hostname provider: %w", err)
}
ctx, cancel := context.WithCancel(ctx)
// cancel() runs on shutdown
traceagent, err := f.TraceAgent(ctx, set, cfg, hostProvider)
if err != nil {
cancel()
return nil, fmt.Errorf("failed to start trace-agent: %w", err)
}
pcfg := newMetadataConfigfromConfig(cfg)
metadataReporter, err := f.Reporter(set, pcfg)
if err != nil {
cancel()
return nil, fmt.Errorf("failed to build host metadata reporter: %w", err)
}
if cfg.OnlyMetadata {
// only host metadata needs to be sent, once.
pusher = func(_ context.Context, td ptrace.Traces) error {
f.onceMetadata.Do(func() {
attrs := pcommon.NewMap()
if td.ResourceSpans().Len() > 0 {
attrs = td.ResourceSpans().At(0).Resource().Attributes()
}
go hostmetadata.RunPusher(ctx, set, pcfg, hostProvider, attrs, metadataReporter)
})
// Consume resources for host metadata
for i := 0; i < td.ResourceSpans().Len(); i++ {
res := td.ResourceSpans().At(i).Resource()
consumeResource(metadataReporter, res, set.Logger)
}
return nil
}
stop = func(context.Context) error {
cancel()
f.StopReporter()
return nil
}
} else {
tracex, err2 := newTracesExporter(ctx, set, cfg, &f.onceMetadata, hostProvider, traceagent, metadataReporter)
if err2 != nil {
cancel()
f.wg.Wait() // then wait for shutdown
return nil, err2
}
pusher = tracex.consumeTraces
stop = func(context.Context) error {
cancel() // first cancel context
f.StopReporter()
return nil
}
}
return exporterhelper.NewTracesExporter(
ctx,
set,
cfg,
pusher,
// explicitly disable since we rely on http.Client timeout logic.
exporterhelper.WithTimeout(exporterhelper.TimeoutSettings{Timeout: 0 * time.Second}),
// We don't do retries on traces because of deduping concerns on APM Events.
exporterhelper.WithRetry(configretry.BackOffConfig{Enabled: false}),
exporterhelper.WithQueue(cfg.QueueSettings),
exporterhelper.WithShutdown(stop),
)
}
// createLogsExporter creates a logs exporter based on the config.
func (f *factory) createLogsExporter(
ctx context.Context,
set exporter.CreateSettings,
c component.Config,
) (exporter.Logs, error) {
cfg := checkAndCastConfig(c, set.TelemetrySettings.Logger)
var pusher consumer.ConsumeLogsFunc
hostProvider, err := f.SourceProvider(set.TelemetrySettings, cfg.Hostname)
if err != nil {
return nil, fmt.Errorf("failed to build hostname provider: %w", err)
}
ctx, cancel := context.WithCancel(ctx)
// cancel() runs on shutdown
pcfg := newMetadataConfigfromConfig(cfg)
metadataReporter, err := f.Reporter(set, pcfg)
if err != nil {
cancel()
return nil, fmt.Errorf("failed to build host metadata reporter: %w", err)
}
attributesTranslator, err := f.AttributesTranslator(set.TelemetrySettings)
if err != nil {
cancel()
return nil, fmt.Errorf("failed to build attributes translator: %w", err)
}
if cfg.OnlyMetadata {
// only host metadata needs to be sent, once.
pusher = func(_ context.Context, td plog.Logs) error {
f.onceMetadata.Do(func() {
attrs := pcommon.NewMap()
go hostmetadata.RunPusher(ctx, set, pcfg, hostProvider, attrs, metadataReporter)
})
for i := 0; i < td.ResourceLogs().Len(); i++ {
res := td.ResourceLogs().At(i).Resource()
consumeResource(metadataReporter, res, set.Logger)
}
return nil
}
} else {
exp, err := newLogsExporter(ctx, set, cfg, &f.onceMetadata, attributesTranslator, hostProvider, metadataReporter)
if err != nil {
cancel()
f.wg.Wait() // then wait for shutdown
return nil, err
}
pusher = exp.consumeLogs
}
return exporterhelper.NewLogsExporter(
ctx,
set,
cfg,
pusher,
// explicitly disable since we rely on http.Client timeout logic.
exporterhelper.WithTimeout(exporterhelper.TimeoutSettings{Timeout: 0 * time.Second}),
exporterhelper.WithRetry(cfg.BackOffConfig),
exporterhelper.WithQueue(cfg.QueueSettings),
exporterhelper.WithShutdown(func(context.Context) error {
cancel()
f.StopReporter()
return nil
}),
)
}