-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlabels-scrape.alloy
419 lines (357 loc) · 12.9 KB
/
labels-scrape.alloy
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
Module Components: labels_scrape
Description: Scrapes targets for metrics based on Docker Containers labels
Note: Every argument except for "forward_to" is optional, and does have a defined default value. However, the values for these
arguments are not defined using the default = " ... " argument syntax, but rather using the coalesce(argument.value, " ... ").
This is because if the argument passed in from another consuming module is set to null, the default = " ... " syntax will
does not override the value passed in, where coalesce() will return the first non-null value.
Following labels are available:
metrics.grafana.com/scrape: true
the default scraping scheme is http, only support http now.
metrics.grafana.com/scheme: http
or
prometheus.io/scheme: http
the default path to scrape is /metrics, this can be specified as a single value which would override, the scrape path being used
for all ports attached to the target:
metrics.grafana.com/path: /metrics/some_path
the default port to scrape is the target port, this can be specified as a single value which would override the scrape port being
used for all ports attached to the target, note that even if an target had multiple targets, the relabel_config targets are
deduped before scraping:
metrics.grafana.com/port: 8080
or
prometheus.io/port: 8080
the default interval to scrape is 15s, this can be specified as a single value which would override, the scrape interval being used
for all ports attached to the target:
metrics.grafana.com/interval: 15s
or
prometheus.io/interval: 15s
the default timeout for scraping is 10s, this can be specified as a single value which would override, the scrape interval being
used for all ports attached to the target:
metrics.grafana.com/timeout: 10s
or
prometheus.io/timeout: 10s
the default job is namespace/{{ service name }} there may be a different job name is required because of a set of dashboards, rules,
etc. to support this there is a job annotation which will override the default value:
metrics.grafana.com/job: integrations/kubernetes/kube-state-metrics
or
prometheus.io/job: integrations/kubernetes/kube-state-metrics
*/
declare "labels_scrape" {
/*****************************************************************
* ARGUMENTS
*****************************************************************/
argument "forward_to" {
comment = "Must be a list(MetricsReceiver) where collected metrics should be forwarded to"
}
argument "label_prefix" {
// Docs: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
// i.e. metrics.grafana.com, then again for prometheus.io
comment = "The label_prefix to use Auto-Scraping (default: metrics.grafana.com)"
default = "metrics.grafana.com"
optional = true
}
argument "__sd_label" {
optional = true
comment = "The logic is used to transform the label_prefix argument into a valid label name by removing unsupported characters."
default = string.replace(string.replace(string.replace(coalesce(argument.label_prefix.value, "metrics.grafana.com"), ".", "_"), "/", "_"), "-", "_")
}
argument "cluster" {
optional = true
default = "docker-compose"
}
argument "namespace" {
optional = true
default = "monitoring-system"
}
argument "tenant" {
comment = "The tenant to write metrics to. This does not have to be the tenantId, this is the value to look for in the logs.grafana.com/tenant label, and this can be a regex. (default: (.*))"
optional = true
default = "(.*)"
}
argument "keep_metrics" {
comment = "A regex of metrics to keep (default: (.+))"
optional = true
default = "(.+)"
}
argument "drop_metrics" {
comment = "A regex of metrics to drop (default: \"\")"
optional = true
default = ""
}
argument "scrape_interval" {
comment = "How often to scrape metrics from the targets (default: 60s)"
optional = true
default = "60s"
}
argument "scrape_timeout" {
comment = "How long before a scrape times out (default: 10s)"
optional = true
default = "10s"
}
/*****************************************************************
* Targets From Docker Discovery
*****************************************************************/
discovery.docker "dd_metrics" {
host = "unix:///var/run/docker.sock"
refresh_interval = "30s"
filter {
name = "status"
values = ["running"]
}
}
/*****************************************************************
* Discovery Relabelings (pre-scrape)
*****************************************************************/
discovery.relabel "dr_label_metrics" {
targets = discovery.docker.dd_metrics.targets
/****************************************************************************************************************
* Handle Discovers From Docker Engine Containers Targets to Keep or Drop
* https://grafana.com/docs/agent/latest/flow/reference/components/discovery.docker/#exported-fields
****************************************************************************************************************/
// allow resources to declare their metrics scraped or not
// Example Annotation:
// metrics.grafana.com/scrape: false
//
// the label prometheus.io/service-monitor: "false" is a common label for headless services, when performing endpoint
// service discovery, if there is both a load-balanced service and headless service, this can result in duplicate
// scrapes if the name of the service is attached as a label. any targets with this label or annotation set should be dropped
rule {
action = "replace"
source_labels = [
"__meta_docker_container_label_" + argument.__sd_label.value + "_scrape",
"__meta_docker_container_label_prometheus_io_scrape",
]
separator = ";"
regex = "^(?:;*)?(true|false).*$"
replacement = "$1"
target_label = "__tmp_scrape"
}
// drop any targets that have scrape: false
rule {
action = "drop"
source_labels = ["__tmp_scrape"]
regex = "false"
}
// allow resources to declare the protocol to use when collecting metrics, the default value is "http",
// Example Annotation:
// metrics.grafana.com/scheme: http
rule {
action = "replace"
replacement = "http"
target_label = "__scheme__"
}
rule {
action = "replace"
source_labels = [
"__meta_docker_container_label_" + argument.__sd_label.value + "_scheme",
"__meta_docker_container_label_prometheus_io_scheme",
]
separator = ";"
regex = "^(?:;*)?(https?).*$"
replacement = "$1"
target_label = "__scheme__"
}
// allow resources to declare their metrics the tenant their metrics should be sent to,
// Example Annotation:
// metrics.grafana.com/tenant: primary
//
// Note: This does not necessarily have to be the actual tenantId, it can be a friendly name as well that is simply used
// to determine if the metrics should be gathered for the current tenant
rule {
action = "keep"
source_labels = [
"__meta_docker_container_label_" + argument.__sd_label.value + "_tenant",
"__meta_docker_container_label_prometheus_io_tenant",
]
regex = "^(" + argument.tenant.value + ")$"
}
rule {
action = "replace"
source_labels = [
"__meta_docker_container_label_" + argument.__sd_label.value + "_port",
"__meta_docker_container_label_prometheus_io_port",
]
separator = ";"
regex = "^(?:;*)?(\\d+).*$"
replacement = "$1"
target_label = "__tmp_metrics_port"
}
// allow resources to declare the port to use when collecting metrics, the default value is the discovered port from
// Example Annotation:
// metrics.grafana.com/port: 9090
rule {
action = "replace"
source_labels = [
"__address__",
"__tmp_metrics_port",
]
separator = ";"
regex = "^([^:]+)(?::\\d+)?;(\\d+)$"
replacement = "$1:$2"
target_label = "__address__"
}
// allow resources to declare their the path to use when collecting their metrics, the default value is "/metrics",
// Example Annotation:
// metrics.grafana.com/path: /metrics/foo
rule {
action = "replace"
source_labels = [
"__meta_docker_container_label_" + argument.__sd_label.value + "_path",
"__meta_docker_container_label_prometheus_io_path",
]
separator = ";"
regex = "^(?:;*)?([^;]+).*$"
replacement = "$1"
target_label = "__metrics_path__"
}
// allow resources to declare how often their metrics should be collected, the default value is 15s,
// the following duration formats are supported (s|m|ms|h|d):
// Example Annotation:
// metrics.grafana.com/interval: 15s
rule {
action = "replace"
replacement = argument.scrape_interval.value
target_label = "__scrape_interval__"
}
rule {
action = "replace"
source_labels = [
"__meta_docker_container_label_" + argument.__sd_label.value + "_interval",
"__meta_docker_container_label_prometheus_io_interval",
]
separator = ";"
regex = "^(?:;*)?(\\d+(s|m|ms|h|d)).*$"
replacement = "$1"
target_label = "__scrape_interval__"
}
// allow resources to declare the timeout of the scrape request, the default value is 10s,
// the following duration formats are supported (s|m|ms|h|d):
// Example Annotation:
// metrics.grafana.com/timeout: 10s
rule {
action = "replace"
replacement = argument.scrape_timeout.value
target_label = "__scrape_timeout__"
}
rule {
action = "replace"
source_labels = [
"__meta_docker_container_label_" + argument.__sd_label.value + "_timeout",
"__meta_docker_container_label_prometheus_io_timeout",
]
separator = ";"
regex = "^(?:;*)?(\\d+(s|m|ms|h|d)).*$"
replacement = "$1"
target_label = "__scrape_timeout__"
}
/********************************************
* Handle Setting Common Labels
********************************************/
// set the cluster label
rule {
action = "replace"
replacement = argument.cluster.value
target_label = "cluster"
}
// set the namespace label
rule {
action = "replace"
replacement = argument.namespace.value
target_label = "namespace"
}
// set a default job label to be the namespace/service_name
rule {
action = "replace"
source_labels = [
"__meta_docker_container_label_com_docker_compose_service",
]
regex = "^(?:;*)?([^;]+).*$"
replacement = argument.namespace.value + "/$1"
target_label = "job"
}
// allow resources to declare their the job label value to use when collecting their metrics, the default value is "",
// Example Annotation:
// metrics.grafana.com/job: integrations/kubernetes/cadvisor
rule {
action = "replace"
source_labels = [
"__meta_docker_container_label_" + argument.__sd_label.value + "_job",
"__meta_docker_container_label_prometheus_io_job",
]
separator = ";"
regex = "^(?:;*)?([^;]+).*$"
replacement = "$1"
target_label = "job"
}
rule {
source_labels = ["__meta_docker_container_name"]
regex = "/(.*)"
target_label = "pod"
}
rule {
source_labels = ["__meta_docker_container_name"]
regex = "/(.*)"
target_label = "container"
}
rule {
action = "replace"
source_labels = [
"__meta_docker_container_label_com_docker_compose_service",
]
regex = "^(?:;*)?([^;]+).*$"
replacement = "$1"
target_label = "app"
}
rule {
action = "replace"
source_labels = [
"__meta_docker_container_label_app",
]
regex = "^(?:;*)?([^;]+).*$"
replacement = "$1"
target_label = "app"
}
}
// only keep http targets
discovery.relabel "dr_keep_http_targets" {
targets = discovery.relabel.dr_label_metrics.output
rule {
action = "keep"
source_labels = ["__scheme__"]
regex = "http"
}
}
/*****************************************************************
* Prometheus Scrape Labels Targets
*****************************************************************/
prometheus.scrape "pc_docker_metrics" {
targets = array.concat(
discovery.relabel.dr_keep_http_targets.output,
)
job_name = "label-metrics"
scrape_classic_histograms = true
clustering {
enabled = true
}
forward_to = [prometheus.relabel.pr_docker_metrics.receiver]
}
/*****************************************************************
* Prometheus Metric Relabelings (post-scrape)
*****************************************************************/
prometheus.relabel "pr_docker_metrics" {
forward_to = argument.forward_to.value
// perform generic relabeling using keep_metrics and drop_metrics
// keep only metrics that match the keep_metrics regex
rule {
action = "keep"
source_labels = ["__name__"]
regex = argument.keep_metrics.value
}
// drop metrics that match the drop_metrics regex
rule {
action = "drop"
source_labels = ["__name__"]
regex = argument.drop_metrics.value
}
}
}