-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
receiver.go
152 lines (131 loc) · 4.59 KB
/
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package simpleprometheusreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/simpleprometheusreceiver"
import (
"context"
"errors"
"fmt"
configutil "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/discovery"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver"
"k8s.io/client-go/rest"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/simpleprometheusreceiver/internal/metadata"
)
type prometheusReceiverWrapper struct {
params receiver.Settings
config *Config
consumer consumer.Metrics
prometheusRecever receiver.Metrics
}
// newPrometheusReceiverWrapper returns a prometheusReceiverWrapper
func newPrometheusReceiverWrapper(params receiver.Settings, cfg *Config, consumer consumer.Metrics) *prometheusReceiverWrapper {
return &prometheusReceiverWrapper{params: params, config: cfg, consumer: consumer}
}
// Start creates and starts the prometheus receiver.
func (prw *prometheusReceiverWrapper) Start(ctx context.Context, host component.Host) error {
pFactory := prometheusreceiver.NewFactory()
pConfig, err := getPrometheusConfigWrapper(prw.config, prw.params)
if err != nil {
return fmt.Errorf("failed to create prometheus receiver config: %w", err)
}
pr, err := pFactory.CreateMetrics(ctx, prw.params, pConfig, prw.consumer)
if err != nil {
return fmt.Errorf("failed to create prometheus receiver: %w", err)
}
prw.prometheusRecever = pr
return prw.prometheusRecever.Start(ctx, host)
}
// Deprecated: [v0.55.0] Use getPrometheusConfig instead.
func getPrometheusConfigWrapper(cfg *Config, params receiver.Settings) (*prometheusreceiver.Config, error) {
if cfg.TLSEnabled {
params.Logger.Warn("the `tls_config` and 'tls_enabled' settings are deprecated, please use `tls` instead")
cfg.ClientConfig.TLSSetting = configtls.ClientConfig{
Config: configtls.Config{
CAFile: cfg.TLSConfig.CAFile,
CertFile: cfg.TLSConfig.CertFile,
KeyFile: cfg.TLSConfig.KeyFile,
},
Insecure: false,
InsecureSkipVerify: cfg.TLSConfig.InsecureSkipVerify,
}
}
return getPrometheusConfig(cfg)
}
func getPrometheusConfig(cfg *Config) (*prometheusreceiver.Config, error) {
var bearerToken string
if cfg.UseServiceAccount {
restConfig, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
bearerToken = restConfig.BearerToken
if bearerToken == "" {
return nil, errors.New("bearer token was empty")
}
}
out := &prometheusreceiver.Config{}
httpConfig := configutil.HTTPClientConfig{}
scheme := "http"
tlsConfig, err := cfg.TLSSetting.LoadTLSConfig(context.Background())
if err != nil {
return nil, fmt.Errorf("tls config is not valid: %w", err)
}
if tlsConfig != nil {
scheme = "https"
httpConfig.TLSConfig = configutil.TLSConfig{
CAFile: cfg.TLSSetting.CAFile,
CertFile: cfg.TLSSetting.CertFile,
KeyFile: cfg.TLSSetting.KeyFile,
InsecureSkipVerify: cfg.TLSSetting.InsecureSkipVerify,
}
}
httpConfig.BearerToken = configutil.Secret(bearerToken)
labels := make(model.LabelSet, len(cfg.Labels)+1)
for k, v := range cfg.Labels {
labels[model.LabelName(k)] = model.LabelValue(v)
}
labels[model.AddressLabel] = model.LabelValue(cfg.Endpoint)
jobName := cfg.JobName
if jobName == "" {
jobName = fmt.Sprintf("%s/%s", metadata.Type, cfg.Endpoint)
}
scrapeConfig := &config.ScrapeConfig{
ScrapeInterval: model.Duration(cfg.CollectionInterval),
ScrapeTimeout: model.Duration(cfg.CollectionInterval),
JobName: jobName,
HonorTimestamps: true,
Scheme: scheme,
MetricsPath: cfg.MetricsPath,
Params: cfg.Params,
ServiceDiscoveryConfigs: discovery.Configs{
&discovery.StaticConfig{
{
Targets: []model.LabelSet{
labels,
},
},
},
},
}
scrapeConfig.HTTPClientConfig = httpConfig
out.PrometheusConfig = &prometheusreceiver.PromConfig{
GlobalConfig: config.DefaultGlobalConfig,
ScrapeConfigs: []*config.ScrapeConfig{
scrapeConfig,
},
}
return out, nil
}
// Shutdown stops the underlying Prometheus receiver.
func (prw *prometheusReceiverWrapper) Shutdown(ctx context.Context) error {
if prw.prometheusRecever == nil {
return nil
}
return prw.prometheusRecever.Shutdown(ctx)
}