forked from grafana/alloy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_gen_scrapeconfig.go
More file actions
205 lines (190 loc) · 7.48 KB
/
config_gen_scrapeconfig.go
File metadata and controls
205 lines (190 loc) · 7.48 KB
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
package configgen
// SEE https://github.com/prometheus-operator/prometheus-operator/blob/aa8222d7e9b66e9293ed11c9291ea70173021029/pkg/prometheus/promcfg.go
import (
"fmt"
"strings"
"time"
promopv1alpha1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1"
"github.com/prometheus-operator/prometheus-operator/pkg/namespacelabeler"
commonConfig "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/discovery"
"github.com/prometheus/prometheus/discovery/http"
"github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/model/relabel"
)
func (cg *ConfigGenerator) GenerateScrapeConfigConfigs(m *promopv1alpha1.ScrapeConfig) (cfg []*config.ScrapeConfig, errors []error) {
for i, ep := range m.Spec.StaticConfigs {
if scrapeConfig, err := cg.generateStaticScrapeConfigConfig(m, ep, i); err != nil {
errors = append(errors, err)
} else {
cfg = append(cfg, scrapeConfig)
}
}
for i, ep := range m.Spec.HTTPSDConfigs {
if scrapeConfig, err := cg.generateHTTPScrapeConfigConfig(m, ep, i); err != nil {
errors = append(errors, err)
} else {
cfg = append(cfg, scrapeConfig)
}
}
return
}
func (cg *ConfigGenerator) generateStaticScrapeConfigConfig(m *promopv1alpha1.ScrapeConfig, sc promopv1alpha1.StaticConfig, i int) (cfg *config.ScrapeConfig, err error) {
relabels := cg.initRelabelings()
metricRelabels := relabeler{}
cfg, err = cg.commonScrapeConfigConfig(m, i, &relabels, &metricRelabels)
if err != nil {
return nil, err
}
cfg.JobName = fmt.Sprintf("scrapeConfig/%s/%s/static/%d", m.Namespace, m.Name, i)
targets := []model.LabelSet{}
for _, target := range sc.Targets {
targets = append(targets, model.LabelSet{
model.AddressLabel: model.LabelValue(target),
})
}
labels := model.LabelSet{}
// promote "__address__" to "instance" label.
relabels.add(&relabel.Config{
SourceLabels: model.LabelNames{model.AddressLabel},
TargetLabel: model.InstanceLabel,
})
for k, v := range sc.Labels {
labels[model.LabelName(k)] = model.LabelValue(v)
}
discoveryCfg := discovery.StaticConfig{
&targetgroup.Group{
Targets: targets,
Labels: labels,
Source: cfg.JobName,
},
}
cfg.ServiceDiscoveryConfigs = append(cfg.ServiceDiscoveryConfigs, discoveryCfg)
return cg.finalizeScrapeConfig(cfg, m, &relabels, &metricRelabels)
}
func (cg *ConfigGenerator) generateHTTPScrapeConfigConfig(m *promopv1alpha1.ScrapeConfig, httpSD promopv1alpha1.HTTPSDConfig, i int) (cfg *config.ScrapeConfig, err error) {
relabels := cg.initRelabelings()
metricRelabels := relabeler{}
cfg, err = cg.commonScrapeConfigConfig(m, i, &relabels, &metricRelabels)
if err != nil {
return nil, err
}
cfg.JobName = fmt.Sprintf("scrapeConfig/%s/%s/http/%d", m.Namespace, m.Name, i)
// Convert HTTPSDConfig to Prometheus HTTP SD config
httpSDConfig := &http.SDConfig{
HTTPClientConfig: commonConfig.DefaultHTTPClientConfig,
RefreshInterval: model.Duration(30 * time.Second), // Default refresh interval
URL: httpSD.URL,
}
// Set refresh interval if specified
if httpSD.RefreshInterval != nil {
if httpSDConfig.RefreshInterval, err = model.ParseDuration(string(*httpSD.RefreshInterval)); err != nil {
return nil, fmt.Errorf("parsing refresh interval from HTTPSDConfig: %w", err)
}
}
// Add TLS configuration if specified
if httpSD.TLSConfig != nil {
if httpSDConfig.HTTPClientConfig.TLSConfig, err = cg.generateSafeTLS(*httpSD.TLSConfig, m.Namespace); err != nil {
return nil, err
}
}
// Add BasicAuth if specified
if httpSD.BasicAuth != nil {
httpSDConfig.HTTPClientConfig.BasicAuth, err = cg.generateBasicAuth(*httpSD.BasicAuth, m.Namespace)
if err != nil {
return nil, err
}
}
// Add Authorization if specified
if httpSD.Authorization != nil {
httpSDConfig.HTTPClientConfig.Authorization, err = cg.generateAuthorization(*httpSD.Authorization, m.Namespace)
if err != nil {
return nil, err
}
}
cfg.ServiceDiscoveryConfigs = append(cfg.ServiceDiscoveryConfigs, httpSDConfig)
return cg.finalizeScrapeConfig(cfg, m, &relabels, &metricRelabels)
}
func (cg *ConfigGenerator) commonScrapeConfigConfig(m *promopv1alpha1.ScrapeConfig, _ int, relabels *relabeler, metricRelabels *relabeler) (cfg *config.ScrapeConfig, err error) {
cfg = cg.generateDefaultScrapeConfig()
if m.Spec.HonorLabels != nil {
cfg.HonorLabels = *m.Spec.HonorLabels
}
if m.Spec.HonorTimestamps != nil {
cfg.HonorTimestamps = *m.Spec.HonorTimestamps
}
if m.Spec.ScrapeInterval != nil {
if cfg.ScrapeInterval, err = model.ParseDuration(string(*m.Spec.ScrapeInterval)); err != nil {
return nil, fmt.Errorf("parsing interval from scrapeConfig: %w", err)
}
}
if m.Spec.ScrapeTimeout != nil {
if cfg.ScrapeTimeout, err = model.ParseDuration(string(*m.Spec.ScrapeTimeout)); err != nil {
return nil, fmt.Errorf("parsing timeout from scrapeConfig: %w", err)
}
}
if m.Spec.ScrapeProtocols != nil {
protocols, err := convertScrapeProtocols(m.Spec.ScrapeProtocols)
if err != nil {
return nil, fmt.Errorf("converting scrape protocols: %w", err)
}
cfg.ScrapeProtocols = protocols
}
if m.Spec.MetricsPath != nil {
cfg.MetricsPath = *m.Spec.MetricsPath
}
if m.Spec.Params != nil {
cfg.Params = m.Spec.Params
}
if m.Spec.Scheme != nil {
// Prometheus Operator ScrapeConfig CRD requires spec.scheme to be uppercase "HTTP" or "HTTPS", but
// the implementation expects lowercase "http" or "https" in the final scrape configuration. So, we
// have to lowercase the schema.
cfg.Scheme = strings.ToLower(*m.Spec.Scheme)
}
if m.Spec.TLSConfig != nil {
if cfg.HTTPClientConfig.TLSConfig, err = cg.generateSafeTLS(*m.Spec.TLSConfig, m.Namespace); err != nil {
return nil, err
}
}
if m.Spec.BasicAuth != nil {
cfg.HTTPClientConfig.BasicAuth, err = cg.generateBasicAuth(*m.Spec.BasicAuth, m.Namespace)
if err != nil {
return nil, err
}
}
if m.Spec.Authorization != nil {
cfg.HTTPClientConfig.Authorization, err = cg.generateAuthorization(*m.Spec.Authorization, m.Namespace)
if err != nil {
return nil, err
}
}
relabels.add(&relabel.Config{
Replacement: m.Namespace,
TargetLabel: "__meta_kubernetes_scrapeconfig_namespace",
}, &relabel.Config{
Replacement: m.Name,
TargetLabel: "__meta_kubernetes_scrapeconfig_name",
})
labeler := namespacelabeler.New("", nil, false)
if err = relabels.addFromV1(labeler.GetRelabelingConfigs(m.TypeMeta, m.ObjectMeta, m.Spec.RelabelConfigs)...); err != nil {
return nil, fmt.Errorf("parsing relabel configs: %w", err)
}
if err = metricRelabels.addFromV1(labeler.GetRelabelingConfigs(m.TypeMeta, m.ObjectMeta, m.Spec.MetricRelabelConfigs)...); err != nil {
return nil, fmt.Errorf("parsing metric relabel configs: %w", err)
}
cfg.SampleLimit = uint(defaultIfNil(m.Spec.SampleLimit, 0))
cfg.TargetLimit = uint(defaultIfNil(m.Spec.TargetLimit, 0))
cfg.LabelLimit = uint(defaultIfNil(m.Spec.LabelLimit, 0))
cfg.LabelNameLengthLimit = uint(defaultIfNil(m.Spec.LabelNameLengthLimit, 0))
cfg.LabelValueLengthLimit = uint(defaultIfNil(m.Spec.LabelValueLengthLimit, 0))
return cfg, err
}
// finalizeScrapeConfig applies common finalization steps to a scrape config
func (cg *ConfigGenerator) finalizeScrapeConfig(cfg *config.ScrapeConfig, m *promopv1alpha1.ScrapeConfig, relabels *relabeler, metricRelabels *relabeler) (*config.ScrapeConfig, error) {
cfg.RelabelConfigs = relabels.configs
cfg.MetricRelabelConfigs = metricRelabels.configs
return cfg, cfg.Validate(cg.ScrapeOptions.GlobalConfig())
}