-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
metrics_receiver.go
334 lines (295 loc) · 10 KB
/
metrics_receiver.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package prometheusreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver"
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"regexp"
"sync"
"time"
"github.com/go-kit/log"
"github.com/mitchellh/hashstructure/v2"
"github.com/prometheus/client_golang/prometheus"
commonconfig "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/discovery"
promHTTP "github.com/prometheus/prometheus/discovery/http"
"github.com/prometheus/prometheus/scrape"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver/internal"
)
const (
defaultGCInterval = 2 * time.Minute
gcIntervalDelta = 1 * time.Minute
)
// pReceiver is the type that provides Prometheus scraper/receiver functionality.
type pReceiver struct {
cfg *Config
consumer consumer.Metrics
cancelFunc context.CancelFunc
targetAllocatorStop chan struct{}
configLoaded chan struct{}
loadConfigOnce sync.Once
settings receiver.CreateSettings
scrapeManager *scrape.Manager
discoveryManager *discovery.Manager
httpClient *http.Client
registerer prometheus.Registerer
}
// New creates a new prometheus.Receiver reference.
func newPrometheusReceiver(set receiver.CreateSettings, cfg *Config, next consumer.Metrics) *pReceiver {
pr := &pReceiver{
cfg: cfg,
consumer: next,
settings: set,
configLoaded: make(chan struct{}),
targetAllocatorStop: make(chan struct{}),
registerer: prometheus.WrapRegistererWith(
prometheus.Labels{"receiver": set.ID.String()},
prometheus.DefaultRegisterer),
}
return pr
}
// Start is the method that starts Prometheus scraping. It
// is controlled by having previously defined a Configuration using perhaps New.
func (r *pReceiver) Start(_ context.Context, host component.Host) error {
discoveryCtx, cancel := context.WithCancel(context.Background())
r.cancelFunc = cancel
logger := internal.NewZapToGokitLogAdapter(r.settings.Logger)
// add scrape configs defined by the collector configs
baseCfg := r.cfg.PrometheusConfig
err := r.initPrometheusComponents(discoveryCtx, logger)
if err != nil {
r.settings.Logger.Error("Failed to initPrometheusComponents Prometheus components", zap.Error(err))
return err
}
err = r.applyCfg(baseCfg)
if err != nil {
r.settings.Logger.Error("Failed to apply new scrape configuration", zap.Error(err))
return err
}
allocConf := r.cfg.TargetAllocator
if allocConf != nil {
r.httpClient, err = r.cfg.TargetAllocator.ToClient(host, r.settings.TelemetrySettings)
if err != nil {
r.settings.Logger.Error("Failed to create http client", zap.Error(err))
return err
}
err = r.startTargetAllocator(allocConf, baseCfg)
if err != nil {
return err
}
}
r.loadConfigOnce.Do(func() {
close(r.configLoaded)
})
return nil
}
func (r *pReceiver) startTargetAllocator(allocConf *TargetAllocator, baseCfg *PromConfig) error {
r.settings.Logger.Info("Starting target allocator discovery")
// immediately sync jobs, not waiting for the first tick
savedHash, err := r.syncTargetAllocator(uint64(0), allocConf, baseCfg)
if err != nil {
return err
}
go func() {
targetAllocatorIntervalTicker := time.NewTicker(allocConf.Interval)
for {
select {
case <-targetAllocatorIntervalTicker.C:
hash, newErr := r.syncTargetAllocator(savedHash, allocConf, baseCfg)
if newErr != nil {
r.settings.Logger.Error(newErr.Error())
continue
}
savedHash = hash
case <-r.targetAllocatorStop:
targetAllocatorIntervalTicker.Stop()
r.settings.Logger.Info("Stopping target allocator")
return
}
}
}()
return nil
}
// syncTargetAllocator request jobs from targetAllocator and update underlying receiver, if the response does not match the provided compareHash.
// baseDiscoveryCfg can be used to provide additional ScrapeConfigs which will be added to the retrieved jobs.
func (r *pReceiver) syncTargetAllocator(compareHash uint64, allocConf *TargetAllocator, baseCfg *PromConfig) (uint64, error) {
r.settings.Logger.Debug("Syncing target allocator jobs")
scrapeConfigsResponse, err := r.getScrapeConfigsResponse(allocConf.Endpoint)
if err != nil {
r.settings.Logger.Error("Failed to retrieve job list", zap.Error(err))
return 0, err
}
hash, err := hashstructure.Hash(scrapeConfigsResponse, hashstructure.FormatV2, nil)
if err != nil {
r.settings.Logger.Error("Failed to hash job list", zap.Error(err))
return 0, err
}
if hash == compareHash {
// no update needed
return hash, nil
}
// Clear out the current configurations
baseCfg.ScrapeConfigs = []*config.ScrapeConfig{}
for jobName, scrapeConfig := range scrapeConfigsResponse {
var httpSD promHTTP.SDConfig
if allocConf.HTTPSDConfig == nil {
httpSD = promHTTP.SDConfig{
RefreshInterval: model.Duration(30 * time.Second),
}
} else {
httpSD = promHTTP.SDConfig(*allocConf.HTTPSDConfig)
}
escapedJob := url.QueryEscape(jobName)
httpSD.URL = fmt.Sprintf("%s/jobs/%s/targets?collector_id=%s", allocConf.Endpoint, escapedJob, allocConf.CollectorID)
httpSD.HTTPClientConfig.FollowRedirects = false
scrapeConfig.ServiceDiscoveryConfigs = discovery.Configs{
&httpSD,
}
baseCfg.ScrapeConfigs = append(baseCfg.ScrapeConfigs, scrapeConfig)
}
err = r.applyCfg(baseCfg)
if err != nil {
r.settings.Logger.Error("Failed to apply new scrape configuration", zap.Error(err))
return 0, err
}
return hash, nil
}
// instantiateShard inserts the SHARD environment variable in the returned configuration
func (r *pReceiver) instantiateShard(body []byte) []byte {
shard, ok := os.LookupEnv("SHARD")
if !ok {
shard = "0"
}
return bytes.ReplaceAll(body, []byte("$(SHARD)"), []byte(shard))
}
func (r *pReceiver) getScrapeConfigsResponse(baseURL string) (map[string]*config.ScrapeConfig, error) {
scrapeConfigsURL := fmt.Sprintf("%s/scrape_configs", baseURL)
_, err := url.Parse(scrapeConfigsURL) // check if valid
if err != nil {
return nil, err
}
resp, err := r.httpClient.Get(scrapeConfigsURL)
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
jobToScrapeConfig := map[string]*config.ScrapeConfig{}
envReplacedBody := r.instantiateShard(body)
err = yaml.Unmarshal(envReplacedBody, &jobToScrapeConfig)
if err != nil {
return nil, err
}
err = resp.Body.Close()
if err != nil {
return nil, err
}
return jobToScrapeConfig, nil
}
func (r *pReceiver) applyCfg(cfg *PromConfig) error {
if err := r.scrapeManager.ApplyConfig((*config.Config)(cfg)); err != nil {
return err
}
discoveryCfg := make(map[string]discovery.Configs)
for _, scrapeConfig := range cfg.ScrapeConfigs {
discoveryCfg[scrapeConfig.JobName] = scrapeConfig.ServiceDiscoveryConfigs
r.settings.Logger.Info("Scrape job added", zap.String("jobName", scrapeConfig.JobName))
}
return r.discoveryManager.ApplyConfig(discoveryCfg)
}
func (r *pReceiver) initPrometheusComponents(ctx context.Context, logger log.Logger) error {
sdMetrics, err := discovery.CreateAndRegisterSDMetrics(r.registerer)
if err != nil {
return err
}
r.discoveryManager = discovery.NewManager(ctx, logger, r.registerer, sdMetrics)
go func() {
r.settings.Logger.Info("Starting discovery manager")
if err = r.discoveryManager.Run(); err != nil && !errors.Is(err, context.Canceled) {
r.settings.Logger.Error("Discovery manager failed", zap.Error(err))
r.settings.TelemetrySettings.ReportStatus(component.NewFatalErrorEvent(err))
}
}()
var startTimeMetricRegex *regexp.Regexp
if r.cfg.StartTimeMetricRegex != "" {
startTimeMetricRegex, err = regexp.Compile(r.cfg.StartTimeMetricRegex)
if err != nil {
return err
}
}
store, err := internal.NewAppendable(
r.consumer,
r.settings,
gcInterval(r.cfg.PrometheusConfig),
r.cfg.UseStartTimeMetric,
startTimeMetricRegex,
useCreatedMetricGate.IsEnabled(),
r.cfg.PrometheusConfig.GlobalConfig.ExternalLabels,
r.cfg.TrimMetricSuffixes,
)
if err != nil {
return err
}
scrapeManager, err := scrape.NewManager(&scrape.Options{
PassMetadataInContext: true,
ExtraMetrics: r.cfg.ReportExtraScrapeMetrics,
HTTPClientOptions: []commonconfig.HTTPClientOption{
commonconfig.WithUserAgent(r.settings.BuildInfo.Command + "/" + r.settings.BuildInfo.Version),
},
}, logger, store, r.registerer)
if err != nil {
return err
}
r.scrapeManager = scrapeManager
go func() {
// The scrape manager needs to wait for the configuration to be loaded before beginning
<-r.configLoaded
r.settings.Logger.Info("Starting scrape manager")
if err := r.scrapeManager.Run(r.discoveryManager.SyncCh()); err != nil {
r.settings.Logger.Error("Scrape manager failed", zap.Error(err))
r.settings.TelemetrySettings.ReportStatus(component.NewFatalErrorEvent(err))
}
}()
return nil
}
// gcInterval returns the longest scrape interval used by a scrape config,
// plus a delta to prevent race conditions.
// This ensures jobs are not garbage collected between scrapes.
func gcInterval(cfg *PromConfig) time.Duration {
gcInterval := defaultGCInterval
if time.Duration(cfg.GlobalConfig.ScrapeInterval)+gcIntervalDelta > gcInterval {
gcInterval = time.Duration(cfg.GlobalConfig.ScrapeInterval) + gcIntervalDelta
}
for _, scrapeConfig := range cfg.ScrapeConfigs {
if time.Duration(scrapeConfig.ScrapeInterval)+gcIntervalDelta > gcInterval {
gcInterval = time.Duration(scrapeConfig.ScrapeInterval) + gcIntervalDelta
}
}
return gcInterval
}
// Shutdown stops and cancels the underlying Prometheus scrapers.
func (r *pReceiver) Shutdown(context.Context) error {
if r.cancelFunc != nil {
r.cancelFunc()
}
if r.scrapeManager != nil {
r.scrapeManager.Stop()
}
close(r.targetAllocatorStop)
return nil
}