-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
traces_exporter.go
227 lines (213 loc) · 8.88 KB
/
traces_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
// 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"
"net/http"
"sync"
"time"
"github.com/DataDog/datadog-agent/pkg/trace/agent"
traceconfig "github.com/DataDog/datadog-agent/pkg/trace/config"
tracelog "github.com/DataDog/datadog-agent/pkg/trace/log"
"github.com/DataDog/datadog-agent/pkg/trace/telemetry"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
"github.com/DataDog/datadog-go/v5/statsd"
"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/consumer"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
"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/scrub"
)
type traceExporter struct {
params exporter.Settings
cfg *Config
ctx context.Context // ctx triggers shutdown upon cancellation
client *zorkian.Client // client sends runnimg metrics to backend & performs API validation
metricsAPI *datadogV2.MetricsApi // client sends runnimg metrics to backend
scrubber scrub.Scrubber // scrubber scrubs sensitive information from error messages
onceMetadata *sync.Once // onceMetadata ensures that metadata is sent only once across all exporters
agent *agent.Agent // agent processes incoming traces
sourceProvider source.Provider // is able to source the origin of a trace (hostname, container, etc)
metadataReporter *inframetadata.Reporter // reports host metadata from resource attributes and metrics
retrier *clientutil.Retrier // retrier handles retries on requests
}
func newTracesExporter(
ctx context.Context,
params exporter.Settings,
cfg *Config,
onceMetadata *sync.Once,
sourceProvider source.Provider,
agent *agent.Agent,
metadataReporter *inframetadata.Reporter,
) (*traceExporter, error) {
scrubber := scrub.NewScrubber()
exp := &traceExporter{
params: params,
cfg: cfg,
ctx: ctx,
agent: agent,
onceMetadata: onceMetadata,
scrubber: scrubber,
sourceProvider: sourceProvider,
retrier: clientutil.NewRetrier(params.Logger, cfg.BackOffConfig, scrubber),
metadataReporter: metadataReporter,
}
// client to send running metric to the backend & perform API key validation
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) }()
exp.metricsAPI = datadogV2.NewMetricsApi(apiClient)
} else {
client := clientutil.CreateZorkianClient(string(cfg.API.Key), cfg.Metrics.TCPAddrConfig.Endpoint)
go func() { errchan <- clientutil.ValidateAPIKeyZorkian(params.Logger, client) }()
exp.client = client
}
if cfg.API.FailOnInvalidKey {
if err := <-errchan; err != nil {
return nil, err
}
}
return exp, nil
}
var _ consumer.ConsumeTracesFunc = (*traceExporter)(nil).consumeTraces
// headerComputedStats specifies the HTTP header which indicates whether APM stats
// have already been computed for a payload.
const headerComputedStats = "Datadog-Client-Computed-Stats"
func (exp *traceExporter) consumeTraces(
ctx context.Context,
td ptrace.Traces,
) (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 td.ResourceSpans().Len() > 0 {
attrs = td.ResourceSpans().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 < td.ResourceSpans().Len(); i++ {
res := td.ResourceSpans().At(i).Resource()
consumeResource(exp.metadataReporter, res, exp.params.Logger)
}
}
rspans := td.ResourceSpans()
hosts := make(map[string]struct{})
tags := make(map[string]struct{})
header := make(http.Header)
if noAPMStatsFeatureGate.IsEnabled() {
header[headerComputedStats] = []string{"true"}
}
for i := 0; i < rspans.Len(); i++ {
rspan := rspans.At(i)
src := exp.agent.OTLPReceiver.ReceiveResourceSpans(ctx, rspan, header)
switch src.Kind {
case source.HostnameKind:
hosts[src.Identifier] = struct{}{}
case source.AWSECSFargateKind:
tags[src.Tag()] = struct{}{}
case source.InvalidKind:
}
}
exp.exportUsageMetrics(ctx, hosts, tags)
return nil
}
func (exp *traceExporter) exportUsageMetrics(ctx context.Context, hosts map[string]struct{}, tags map[string]struct{}) {
now := pcommon.NewTimestampFromTime(time.Now())
buildTags := metrics.TagsFromBuildInfo(exp.params.BuildInfo)
var err error
if isMetricExportV2Enabled() {
series := make([]datadogV2.MetricSeries, 0, len(hosts)+len(tags))
for host := range hosts {
series = append(series, metrics.DefaultMetrics("traces", host, uint64(now), buildTags)...)
}
for tag := range tags {
ms := metrics.DefaultMetrics("traces", "", uint64(now), buildTags)
for i := range ms {
ms[i].Tags = append(ms[i].Tags, tag)
}
series = append(series, ms...)
}
_, err = exp.retrier.DoWithRetries(ctx, func(context.Context) error {
ctx2 := clientutil.GetRequestContext(ctx, string(exp.cfg.API.Key))
_, httpresp, merr := exp.metricsAPI.SubmitMetrics(ctx2, datadogV2.MetricPayload{Series: series}, *clientutil.GZipSubmitMetricsOptionalParameters)
return clientutil.WrapError(merr, httpresp)
})
} else {
series := make([]zorkian.Metric, 0, len(hosts)+len(tags))
for host := range hosts {
series = append(series, metrics.DefaultZorkianMetrics("traces", host, uint64(now), exp.params.BuildInfo)...)
}
for tag := range tags {
ms := metrics.DefaultZorkianMetrics("traces", "", uint64(now), exp.params.BuildInfo)
for i := range ms {
ms[i].Tags = append(ms[i].Tags, tag)
}
series = append(series, ms...)
}
_, err = exp.retrier.DoWithRetries(ctx, func(context.Context) error {
return exp.client.PostMetrics(series)
})
}
if err != nil {
exp.params.Logger.Error("Error posting hostname/tags series", zap.Error(err))
}
}
func newTraceAgent(ctx context.Context, params exporter.Settings, cfg *Config, sourceProvider source.Provider, metricsClient statsd.ClientInterface, attrsTranslator *attributes.Translator) (*agent.Agent, error) {
acfg, err := newTraceAgentConfig(ctx, params, cfg, sourceProvider, attrsTranslator)
if err != nil {
return nil, err
}
return agent.NewAgent(ctx, acfg, telemetry.NewNoopCollector(), metricsClient), nil
}
func newTraceAgentConfig(ctx context.Context, params exporter.Settings, cfg *Config, sourceProvider source.Provider, attrsTranslator *attributes.Translator) (*traceconfig.AgentConfig, error) {
acfg := traceconfig.New()
src, err := sourceProvider.Source(ctx)
if err != nil {
return nil, err
}
if src.Kind == source.HostnameKind {
acfg.Hostname = src.Identifier
}
acfg.OTLPReceiver.AttributesTranslator = attrsTranslator
acfg.OTLPReceiver.SpanNameRemappings = cfg.Traces.SpanNameRemappings
acfg.OTLPReceiver.SpanNameAsResourceName = cfg.Traces.SpanNameAsResourceName
acfg.Endpoints[0].APIKey = string(cfg.API.Key)
acfg.Ignore["resource"] = cfg.Traces.IgnoreResources
acfg.ReceiverPort = 0 // disable HTTP receiver
acfg.AgentVersion = fmt.Sprintf("datadogexporter-%s-%s", params.BuildInfo.Command, params.BuildInfo.Version)
acfg.SkipSSLValidation = cfg.ClientConfig.TLSSetting.InsecureSkipVerify
acfg.ComputeStatsBySpanKind = cfg.Traces.ComputeStatsBySpanKind
acfg.PeerTagsAggregation = cfg.Traces.PeerTagsAggregation
acfg.PeerTags = cfg.Traces.PeerTags
if v := cfg.Traces.flushInterval; v > 0 {
acfg.TraceWriter.FlushPeriodSeconds = v
}
if v := cfg.Traces.TraceBuffer; v > 0 {
acfg.TraceBuffer = v
}
if addr := cfg.Traces.Endpoint; addr != "" {
acfg.Endpoints[0].Host = addr
}
if cfg.Traces.ComputeTopLevelBySpanKind {
acfg.Features["enable_otlp_compute_top_level_by_span_kind"] = struct{}{}
}
tracelog.SetLogger(&zaplogger{params.Logger}) //TODO: This shouldn't be a singleton
return acfg, nil
}