forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obsreport_scraper.go
181 lines (154 loc) · 5.84 KB
/
obsreport_scraper.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
// 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"
"errors"
"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/featuregate"
"go.opentelemetry.io/collector/internal/obsreportconfig"
"go.opentelemetry.io/collector/internal/obsreportconfig/obsmetrics"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/scrapererror"
)
var (
scraperName = "scraper"
scraperScope = scopeName + nameSep + scraperName
)
// Scraper is a helper to add observability to a component.Scraper.
type Scraper struct {
level configtelemetry.Level
receiverID component.ID
scraper component.ID
mutators []tag.Mutator
tracer trace.Tracer
logger *zap.Logger
useOtelForMetrics bool
otelAttrs []attribute.KeyValue
scrapedMetricsPoints syncint64.Counter
erroredMetricsPoints syncint64.Counter
}
// ScraperSettings are settings for creating a Scraper.
type ScraperSettings struct {
ReceiverID component.ID
Scraper component.ID
ReceiverCreateSettings receiver.CreateSettings
}
// NewScraper creates a new Scraper.
func NewScraper(cfg ScraperSettings) (*Scraper, error) {
return newScraper(cfg, featuregate.GetRegistry())
}
func newScraper(cfg ScraperSettings, registry *featuregate.Registry) (*Scraper, error) {
scraper := &Scraper{
level: cfg.ReceiverCreateSettings.TelemetrySettings.MetricsLevel,
receiverID: cfg.ReceiverID,
scraper: cfg.Scraper,
mutators: []tag.Mutator{
tag.Upsert(obsmetrics.TagKeyReceiver, cfg.ReceiverID.String(), tag.WithTTL(tag.TTLNoPropagation)),
tag.Upsert(obsmetrics.TagKeyScraper, cfg.Scraper.String(), tag.WithTTL(tag.TTLNoPropagation))},
tracer: cfg.ReceiverCreateSettings.TracerProvider.Tracer(cfg.Scraper.String()),
logger: cfg.ReceiverCreateSettings.Logger,
useOtelForMetrics: registry.IsEnabled(obsreportconfig.UseOtelForInternalMetricsfeatureGateID),
otelAttrs: []attribute.KeyValue{
attribute.String(obsmetrics.ReceiverKey, cfg.ReceiverID.String()),
attribute.String(obsmetrics.ScraperKey, cfg.Scraper.String()),
},
}
if err := scraper.createOtelMetrics(cfg); err != nil {
return nil, err
}
return scraper, nil
}
func (s *Scraper) createOtelMetrics(cfg ScraperSettings) error {
if !s.useOtelForMetrics {
return nil
}
meter := cfg.ReceiverCreateSettings.MeterProvider.Meter(scraperScope)
var errors, err error
s.scrapedMetricsPoints, err = meter.SyncInt64().Counter(
obsmetrics.ScraperPrefix+obsmetrics.ScrapedMetricPointsKey,
instrument.WithDescription("Number of metric points successfully scraped."),
instrument.WithUnit(unit.Dimensionless),
)
errors = multierr.Append(errors, err)
s.erroredMetricsPoints, err = meter.SyncInt64().Counter(
obsmetrics.ScraperPrefix+obsmetrics.ErroredMetricPointsKey,
instrument.WithDescription("Number of metric points that were unable to be scraped."),
instrument.WithUnit(unit.Dimensionless),
)
errors = multierr.Append(errors, err)
return errors
}
// StartMetricsOp is called when a scrape operation is started. The
// returned context should be used in other calls to the obsreport functions
// dealing with the same scrape operation.
func (s *Scraper) StartMetricsOp(ctx context.Context) context.Context {
ctx, _ = tag.New(ctx, s.mutators...)
spanName := obsmetrics.ScraperPrefix + s.receiverID.String() + obsmetrics.NameSep + s.scraper.String() + obsmetrics.ScraperMetricsOperationSuffix
ctx, _ = s.tracer.Start(ctx, spanName)
return ctx
}
// EndMetricsOp completes the scrape operation that was started with
// StartMetricsOp.
func (s *Scraper) EndMetricsOp(
scraperCtx context.Context,
numScrapedMetrics int,
err error,
) {
numErroredMetrics := 0
if err != nil {
var partialErr scrapererror.PartialScrapeError
if errors.As(err, &partialErr) {
numErroredMetrics = partialErr.Failed
} else {
numErroredMetrics = numScrapedMetrics
numScrapedMetrics = 0
}
}
span := trace.SpanFromContext(scraperCtx)
if s.level != configtelemetry.LevelNone {
s.recordMetrics(scraperCtx, numScrapedMetrics, numErroredMetrics)
}
// end span according to errors
if span.IsRecording() {
span.SetAttributes(
attribute.String(obsmetrics.FormatKey, string(component.DataTypeMetrics)),
attribute.Int64(obsmetrics.ScrapedMetricPointsKey, int64(numScrapedMetrics)),
attribute.Int64(obsmetrics.ErroredMetricPointsKey, int64(numErroredMetrics)),
)
recordError(span, err)
}
span.End()
}
func (s *Scraper) recordMetrics(scraperCtx context.Context, numScrapedMetrics, numErroredMetrics int) {
if s.useOtelForMetrics {
s.scrapedMetricsPoints.Add(scraperCtx, int64(numScrapedMetrics), s.otelAttrs...)
s.erroredMetricsPoints.Add(scraperCtx, int64(numErroredMetrics), s.otelAttrs...)
} else { // OC for metrics
stats.Record(
scraperCtx,
obsmetrics.ScraperScrapedMetricPoints.M(int64(numScrapedMetrics)),
obsmetrics.ScraperErroredMetricPoints.M(int64(numErroredMetrics)))
}
}