/
obsreport_exporter.go
260 lines (223 loc) · 9.67 KB
/
obsreport_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
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package obsreport // import "go.opentelemetry.io/collector/obsreport"
import (
"context"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/instrument/syncint64"
"go.opentelemetry.io/otel/metric/unit"
"go.opentelemetry.io/otel/trace"
"go.uber.org/multierr"
"go.uber.org/zap"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/internal/obsreportconfig"
"go.opentelemetry.io/collector/internal/obsreportconfig/obsmetrics"
)
const (
exporterName = "exporter"
exporterScope = scopeName + nameSep + exporterName
)
// Exporter is a helper to add observability to a component.Exporter.
type Exporter struct {
level configtelemetry.Level
spanNamePrefix string
mutators []tag.Mutator
tracer trace.Tracer
logger *zap.Logger
useOtelForMetrics bool
otelAttrs []attribute.KeyValue
sentSpans syncint64.Counter
failedToSendSpans syncint64.Counter
sentMetricPoints syncint64.Counter
failedToSendMetricPoints syncint64.Counter
sentLogRecords syncint64.Counter
failedToSendLogRecords syncint64.Counter
}
// ExporterSettings are settings for creating an Exporter.
type ExporterSettings struct {
ExporterID component.ID
ExporterCreateSettings exporter.CreateSettings
}
// NewExporter creates a new Exporter.
func NewExporter(cfg ExporterSettings) (*Exporter, error) {
return newExporter(cfg, featuregate.GetRegistry())
}
func newExporter(cfg ExporterSettings, registry *featuregate.Registry) (*Exporter, error) {
exp := &Exporter{
level: cfg.ExporterCreateSettings.TelemetrySettings.MetricsLevel,
spanNamePrefix: obsmetrics.ExporterPrefix + cfg.ExporterID.String(),
mutators: []tag.Mutator{tag.Upsert(obsmetrics.TagKeyExporter, cfg.ExporterID.String(), tag.WithTTL(tag.TTLNoPropagation))},
tracer: cfg.ExporterCreateSettings.TracerProvider.Tracer(cfg.ExporterID.String()),
logger: cfg.ExporterCreateSettings.Logger,
useOtelForMetrics: registry.IsEnabled(obsreportconfig.UseOtelForInternalMetricsfeatureGateID),
otelAttrs: []attribute.KeyValue{
attribute.String(obsmetrics.ExporterKey, cfg.ExporterID.String()),
},
}
if err := exp.createOtelMetrics(cfg); err != nil {
return nil, err
}
return exp, nil
}
func (exp *Exporter) createOtelMetrics(cfg ExporterSettings) error {
if !exp.useOtelForMetrics {
return nil
}
meter := cfg.ExporterCreateSettings.MeterProvider.Meter(exporterScope)
var errors, err error
exp.sentSpans, err = meter.SyncInt64().Counter(
obsmetrics.ExporterPrefix+obsmetrics.SentSpansKey,
instrument.WithDescription("Number of spans successfully sent to destination."),
instrument.WithUnit(unit.Dimensionless))
errors = multierr.Append(errors, err)
exp.failedToSendSpans, err = meter.SyncInt64().Counter(
obsmetrics.ExporterPrefix+obsmetrics.FailedToSendSpansKey,
instrument.WithDescription("Number of spans in failed attempts to send to destination."),
instrument.WithUnit(unit.Dimensionless))
errors = multierr.Append(errors, err)
exp.sentMetricPoints, err = meter.SyncInt64().Counter(
obsmetrics.ExporterPrefix+obsmetrics.SentMetricPointsKey,
instrument.WithDescription("Number of metric points successfully sent to destination."),
instrument.WithUnit(unit.Dimensionless))
errors = multierr.Append(errors, err)
exp.failedToSendMetricPoints, err = meter.SyncInt64().Counter(
obsmetrics.ExporterPrefix+obsmetrics.FailedToSendMetricPointsKey,
instrument.WithDescription("Number of metric points in failed attempts to send to destination."),
instrument.WithUnit(unit.Dimensionless))
errors = multierr.Append(errors, err)
exp.sentLogRecords, err = meter.SyncInt64().Counter(
obsmetrics.ExporterPrefix+obsmetrics.SentLogRecordsKey,
instrument.WithDescription("Number of log record successfully sent to destination."),
instrument.WithUnit(unit.Dimensionless))
errors = multierr.Append(errors, err)
exp.failedToSendLogRecords, err = meter.SyncInt64().Counter(
obsmetrics.ExporterPrefix+obsmetrics.FailedToSendLogRecordsKey,
instrument.WithDescription("Number of log records in failed attempts to send to destination."),
instrument.WithUnit(unit.Dimensionless))
errors = multierr.Append(errors, err)
return errors
}
// StartTracesOp is called at the start of an Export operation.
// The returned context should be used in other calls to the Exporter functions
// dealing with the same export operation.
func (exp *Exporter) StartTracesOp(ctx context.Context) context.Context {
return exp.startOp(ctx, obsmetrics.ExportTraceDataOperationSuffix)
}
// EndTracesOp completes the export operation that was started with StartTracesOp.
func (exp *Exporter) EndTracesOp(ctx context.Context, numSpans int, err error) {
numSent, numFailedToSend := toNumItems(numSpans, err)
exp.recordMetrics(ctx, component.DataTypeTraces, numSent, numFailedToSend)
endSpan(ctx, err, numSent, numFailedToSend, obsmetrics.SentSpansKey, obsmetrics.FailedToSendSpansKey)
}
// StartMetricsOp is called at the start of an Export operation.
// The returned context should be used in other calls to the Exporter functions
// dealing with the same export operation.
func (exp *Exporter) StartMetricsOp(ctx context.Context) context.Context {
return exp.startOp(ctx, obsmetrics.ExportMetricsOperationSuffix)
}
// EndMetricsOp completes the export operation that was started with
// StartMetricsOp.
func (exp *Exporter) EndMetricsOp(ctx context.Context, numMetricPoints int, err error) {
numSent, numFailedToSend := toNumItems(numMetricPoints, err)
exp.recordMetrics(ctx, component.DataTypeMetrics, numSent, numFailedToSend)
endSpan(ctx, err, numSent, numFailedToSend, obsmetrics.SentMetricPointsKey, obsmetrics.FailedToSendMetricPointsKey)
}
// StartLogsOp is called at the start of an Export operation.
// The returned context should be used in other calls to the Exporter functions
// dealing with the same export operation.
func (exp *Exporter) StartLogsOp(ctx context.Context) context.Context {
return exp.startOp(ctx, obsmetrics.ExportLogsOperationSuffix)
}
// EndLogsOp completes the export operation that was started with StartLogsOp.
func (exp *Exporter) EndLogsOp(ctx context.Context, numLogRecords int, err error) {
numSent, numFailedToSend := toNumItems(numLogRecords, err)
exp.recordMetrics(ctx, component.DataTypeLogs, numSent, numFailedToSend)
endSpan(ctx, err, numSent, numFailedToSend, obsmetrics.SentLogRecordsKey, obsmetrics.FailedToSendLogRecordsKey)
}
// startOp creates the span used to trace the operation. Returning
// the updated context and the created span.
func (exp *Exporter) startOp(ctx context.Context, operationSuffix string) context.Context {
spanName := exp.spanNamePrefix + operationSuffix
ctx, _ = exp.tracer.Start(ctx, spanName)
return ctx
}
func (exp *Exporter) recordMetrics(ctx context.Context, dataType component.DataType, numSent, numFailed int64) {
if exp.level == configtelemetry.LevelNone {
return
}
if exp.useOtelForMetrics {
exp.recordWithOtel(ctx, dataType, numSent, numFailed)
} else {
exp.recordWithOC(ctx, dataType, numSent, numFailed)
}
}
func (exp *Exporter) recordWithOtel(ctx context.Context, dataType component.DataType, sent int64, failed int64) {
var sentMeasure, failedMeasure syncint64.Counter
switch dataType {
case component.DataTypeTraces:
sentMeasure = exp.sentSpans
failedMeasure = exp.failedToSendSpans
case component.DataTypeMetrics:
sentMeasure = exp.sentMetricPoints
failedMeasure = exp.failedToSendMetricPoints
case component.DataTypeLogs:
sentMeasure = exp.sentLogRecords
failedMeasure = exp.failedToSendLogRecords
}
sentMeasure.Add(ctx, sent, exp.otelAttrs...)
failedMeasure.Add(ctx, failed, exp.otelAttrs...)
}
func (exp *Exporter) recordWithOC(ctx context.Context, dataType component.DataType, sent int64, failed int64) {
var sentMeasure, failedMeasure *stats.Int64Measure
switch dataType {
case component.DataTypeTraces:
sentMeasure = obsmetrics.ExporterSentSpans
failedMeasure = obsmetrics.ExporterFailedToSendSpans
case component.DataTypeMetrics:
sentMeasure = obsmetrics.ExporterSentMetricPoints
failedMeasure = obsmetrics.ExporterFailedToSendMetricPoints
case component.DataTypeLogs:
sentMeasure = obsmetrics.ExporterSentLogRecords
failedMeasure = obsmetrics.ExporterFailedToSendLogRecords
}
_ = stats.RecordWithTags(
ctx,
exp.mutators,
sentMeasure.M(sent),
failedMeasure.M(failed))
}
func endSpan(ctx context.Context, err error, numSent, numFailedToSend int64, sentItemsKey, failedToSendItemsKey string) {
span := trace.SpanFromContext(ctx)
// End the span according to errors.
if span.IsRecording() {
span.SetAttributes(
attribute.Int64(sentItemsKey, numSent),
attribute.Int64(failedToSendItemsKey, numFailedToSend),
)
recordError(span, err)
}
span.End()
}
func toNumItems(numExportedItems int, err error) (int64, int64) {
if err != nil {
return 0, int64(numExportedItems)
}
return int64(numExportedItems), 0
}