-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
metrics_exporter.go
269 lines (243 loc) · 9.79 KB
/
metrics_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
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package datadogexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter"
import (
"bytes"
"context"
"errors"
"fmt"
"net/http"
"sync"
"time"
pb "github.com/DataDog/datadog-agent/pkg/proto/pbgo/trace"
"github.com/DataDog/datadog-agent/pkg/trace/api"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
"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"
otlpmetrics "github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/metrics"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
zorkian "gopkg.in/zorkian/go-datadog-api.v2"
"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/metrics"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metrics/sketches"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/scrub"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/datadog"
)
type metricsExporter struct {
params exporter.CreateSettings
cfg *Config
ctx context.Context
client *zorkian.Client
metricsAPI *datadogV2.MetricsApi
tr *otlpmetrics.Translator
scrubber scrub.Scrubber
retrier *clientutil.Retrier
onceMetadata *sync.Once
sourceProvider source.Provider
metadataReporter *inframetadata.Reporter
// getPushTime returns a Unix time in nanoseconds, representing the time pushing metrics.
// It will be overwritten in tests.
getPushTime func() uint64
apmStatsProcessor api.StatsProcessor
}
// translatorFromConfig creates a new metrics translator from the exporter
func translatorFromConfig(set component.TelemetrySettings, cfg *Config, attrsTranslator *attributes.Translator, sourceProvider source.Provider, statsOut chan []byte) (*otlpmetrics.Translator, error) {
options := []otlpmetrics.TranslatorOption{
otlpmetrics.WithDeltaTTL(cfg.Metrics.DeltaTTL),
otlpmetrics.WithFallbackSourceProvider(sourceProvider),
otlpmetrics.WithRemapping(),
}
if cfg.Metrics.HistConfig.SendAggregations {
options = append(options, otlpmetrics.WithHistogramAggregations())
}
if cfg.Metrics.SummaryConfig.Mode == SummaryModeGauges {
options = append(options, otlpmetrics.WithQuantiles())
}
if cfg.Metrics.ExporterConfig.InstrumentationScopeMetadataAsTags {
options = append(options, otlpmetrics.WithInstrumentationScopeMetadataAsTags())
}
options = append(options, otlpmetrics.WithHistogramMode(otlpmetrics.HistogramMode(cfg.Metrics.HistConfig.Mode)))
var numberMode otlpmetrics.NumberMode
switch cfg.Metrics.SumConfig.CumulativeMonotonicMode {
case CumulativeMonotonicSumModeRawValue:
numberMode = otlpmetrics.NumberModeRawValue
case CumulativeMonotonicSumModeToDelta:
numberMode = otlpmetrics.NumberModeCumulativeToDelta
}
options = append(options, otlpmetrics.WithNumberMode(numberMode))
options = append(options, otlpmetrics.WithInitialCumulMonoValueMode(
otlpmetrics.InitialCumulMonoValueMode(cfg.Metrics.SumConfig.InitialCumulativeMonotonicMode)))
if datadog.ConnectorPerformanceFeatureGate.IsEnabled() {
options = append(options, otlpmetrics.WithStatsOut(statsOut))
}
return otlpmetrics.NewTranslator(set, attrsTranslator, options...)
}
func newMetricsExporter(
ctx context.Context,
params exporter.CreateSettings,
cfg *Config,
onceMetadata *sync.Once,
attrsTranslator *attributes.Translator,
sourceProvider source.Provider,
apmStatsProcessor api.StatsProcessor,
metadataReporter *inframetadata.Reporter,
statsOut chan []byte,
) (*metricsExporter, error) {
tr, err := translatorFromConfig(params.TelemetrySettings, cfg, attrsTranslator, sourceProvider, statsOut)
if err != nil {
return nil, err
}
scrubber := scrub.NewScrubber()
exporter := &metricsExporter{
params: params,
cfg: cfg,
ctx: ctx,
tr: tr,
scrubber: scrubber,
retrier: clientutil.NewRetrier(params.Logger, cfg.BackOffConfig, scrubber),
onceMetadata: onceMetadata,
sourceProvider: sourceProvider,
getPushTime: func() uint64 { return uint64(time.Now().UTC().UnixNano()) },
apmStatsProcessor: apmStatsProcessor,
metadataReporter: metadataReporter,
}
errchan := make(chan error)
if isMetricExportV2Enabled() {
apiClient := clientutil.CreateAPIClient(
params.BuildInfo,
cfg.Metrics.TCPAddr.Endpoint,
cfg.TimeoutSettings,
cfg.LimitedHTTPClientSettings.TLSSetting.InsecureSkipVerify)
go func() { errchan <- clientutil.ValidateAPIKey(ctx, string(cfg.API.Key), params.Logger, apiClient) }()
exporter.metricsAPI = datadogV2.NewMetricsApi(apiClient)
} else {
client := clientutil.CreateZorkianClient(string(cfg.API.Key), cfg.Metrics.TCPAddr.Endpoint)
client.ExtraHeader["User-Agent"] = clientutil.UserAgent(params.BuildInfo)
client.HttpClient = clientutil.NewHTTPClient(cfg.TimeoutSettings, cfg.LimitedHTTPClientSettings.TLSSetting.InsecureSkipVerify)
go func() { errchan <- clientutil.ValidateAPIKeyZorkian(params.Logger, client) }()
exporter.client = client
}
if cfg.API.FailOnInvalidKey {
err = <-errchan
if err != nil {
return nil, err
}
}
return exporter, nil
}
func (exp *metricsExporter) pushSketches(ctx context.Context, sl sketches.SketchSeriesList) error {
payload, err := sl.Marshal()
if err != nil {
return fmt.Errorf("failed to marshal sketches: %w", err)
}
req, err := http.NewRequestWithContext(ctx,
http.MethodPost,
exp.cfg.Metrics.TCPAddr.Endpoint+sketches.SketchSeriesEndpoint,
bytes.NewBuffer(payload),
)
if err != nil {
return fmt.Errorf("failed to build sketches HTTP request: %w", err)
}
clientutil.SetDDHeaders(req.Header, exp.params.BuildInfo, string(exp.cfg.API.Key))
clientutil.SetExtraHeaders(req.Header, clientutil.ProtobufHeaders)
var resp *http.Response
if isMetricExportV2Enabled() {
resp, err = exp.metricsAPI.Client.Cfg.HTTPClient.Do(req)
} else {
resp, err = exp.client.HttpClient.Do(req)
}
if err != nil {
return clientutil.WrapError(fmt.Errorf("failed to do sketches HTTP request: %w", err), resp)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return clientutil.WrapError(fmt.Errorf("error when sending payload to %s: %s", sketches.SketchSeriesEndpoint, resp.Status), resp)
}
return nil
}
func (exp *metricsExporter) PushMetricsDataScrubbed(ctx context.Context, md pmetric.Metrics) error {
return exp.scrubber.Scrub(exp.PushMetricsData(ctx, md))
}
func (exp *metricsExporter) PushMetricsData(ctx context.Context, md pmetric.Metrics) error {
if exp.cfg.HostMetadata.Enabled {
// Start host metadata with resource attributes from
// the first payload.
exp.onceMetadata.Do(func() {
attrs := pcommon.NewMap()
if md.ResourceMetrics().Len() > 0 {
attrs = md.ResourceMetrics().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 < md.ResourceMetrics().Len(); i++ {
res := md.ResourceMetrics().At(i).Resource()
consumeResource(exp.metadataReporter, res, exp.params.Logger)
}
}
var consumer otlpmetrics.Consumer
if isMetricExportV2Enabled() {
consumer = metrics.NewConsumer()
} else {
consumer = metrics.NewZorkianConsumer()
}
metadata, err := exp.tr.MapMetrics(ctx, md, consumer)
if err != nil {
return fmt.Errorf("failed to map metrics: %w", err)
}
src, err := exp.sourceProvider.Source(ctx)
if err != nil {
return err
}
var tags []string
if src.Kind == source.AWSECSFargateKind {
tags = append(tags, exp.cfg.HostMetadata.Tags...)
}
var sl sketches.SketchSeriesList
var sp []*pb.ClientStatsPayload
var errs []error
if isMetricExportV2Enabled() {
var ms []datadogV2.MetricSeries
ms, sl, sp = consumer.(*metrics.Consumer).All(exp.getPushTime(), exp.params.BuildInfo, tags, metadata)
if len(ms) > 0 {
exp.params.Logger.Debug("exporting native Datadog payload", zap.Any("metric", ms))
_, experr := exp.retrier.DoWithRetries(ctx, func(context.Context) error {
ctx = clientutil.GetRequestContext(ctx, string(exp.cfg.API.Key))
_, httpresp, merr := exp.metricsAPI.SubmitMetrics(ctx, datadogV2.MetricPayload{Series: ms}, *clientutil.GZipSubmitMetricsOptionalParameters)
return clientutil.WrapError(merr, httpresp)
})
errs = append(errs, experr)
}
} else {
var ms []zorkian.Metric
ms, sl, sp = consumer.(*metrics.ZorkianConsumer).All(exp.getPushTime(), exp.params.BuildInfo, tags)
if len(ms) > 0 {
exp.params.Logger.Debug("exporting Zorkian Datadog payload", zap.Any("metric", ms))
_, experr := exp.retrier.DoWithRetries(ctx, func(context.Context) error {
return exp.client.PostMetrics(ms)
})
errs = append(errs, experr)
}
}
if len(sl) > 0 {
exp.params.Logger.Debug("exporting sketches payload", zap.Any("sketches", sl))
_, experr := exp.retrier.DoWithRetries(ctx, func(ctx context.Context) error {
return exp.pushSketches(ctx, sl)
})
errs = append(errs, experr)
}
if len(sp) > 0 {
exp.params.Logger.Debug("exporting APM stats payloads", zap.Any("stats_payloads", sp))
statsv := exp.params.BuildInfo.Command + exp.params.BuildInfo.Version
for _, p := range sp {
exp.apmStatsProcessor.ProcessStats(p, "", statsv)
}
}
return errors.Join(errs...)
}