Skip to content

Commit a9364c8

Browse files
lioshikAleksey Smirnov
andauthored
feature implement constant labels (#542)
Co-authored-by: Aleksey Smirnov <[email protected]>
1 parent b815b9a commit a9364c8

File tree

7 files changed

+161
-100
lines changed

7 files changed

+161
-100
lines changed

config/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- `<networks>`: string value consisting of IP, IP mask or named group, for example `"127.0.0.1"` or `"127.0.0.1/24"`.
88
- `<host_name>`: string value consisting of host name, for example `"example.com"`
99
- `<byte_size>`: string value matching the regular expression `/^\d+(\.\d+)?[KMGTP]?B?$/i`, for example `"100MB"`
10+
- `<map[label_name]label_value>`: а map consisting of string keys and string values for optional custom labels in metrics
1011

1112
### Global configuration consist of:
1213
```yml
@@ -237,6 +238,9 @@ allowed_networks: <network_groups>, <networks> ... | optional
237238

238239
# Prometheus metric namespace
239240
namespace: <string> | optional
241+
242+
# Labels that should be added to each sent prometheus metrics
243+
constant_labels: <map[label_name]label_value>
240244
```
241245
242246
### <user_config>

config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,9 @@ type Metrics struct {
492492
// Prometheus metric namespace
493493
Namespace string `yaml:"namespace,omitempty"`
494494

495+
// Constant labels which will be added to each sent prometheus metric
496+
ConstantLabels map[string]string `yaml:"constant_labels,omitempty"`
497+
495498
// Catches all undefined fields and must be empty after parsing.
496499
XXX map[string]interface{} `yaml:",inline"`
497500
}

docs/src/content/docs/cn/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,8 @@ server:
487487
# By default access to `/metrics` is unrestricted.
488488
metrics:
489489
allowed_networks: ["office"]
490+
# You can specify constant labels which will be added to each sent prometheus metric
491+
constant_labels: {}
490492

491493
# Configs for input users.
492494
users:

docs/src/content/docs/configuration/default.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ server:
177177
metrics:
178178
allowed_networks: ["office"]
179179
namespace: ""
180+
# You can specify constant labels which will be added to each sent prometheus metric
181+
constant_labels: {}
180182

181183
# Proxy settings enable parsing proxy headers in cases where
182184
# CHProxy is run behind another proxy.

internal/topology/metrics.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,23 @@ var (
1313

1414
func initMetrics(cfg *config.Config) {
1515
namespace := cfg.Server.Metrics.Namespace
16+
constLabels := cfg.Server.Metrics.ConstantLabels
17+
1618
HostHealth = prometheus.NewGaugeVec(
1719
prometheus.GaugeOpts{
18-
Namespace: namespace,
19-
Name: "host_health",
20-
Help: "Health state of hosts by clusters",
20+
Namespace: namespace,
21+
Name: "host_health",
22+
Help: "Health state of hosts by clusters",
23+
ConstLabels: constLabels,
2124
},
2225
[]string{"cluster", "replica", "cluster_node"},
2326
)
2427
HostPenalties = prometheus.NewCounterVec(
2528
prometheus.CounterOpts{
26-
Namespace: namespace,
27-
Name: "host_penalties_total",
28-
Help: "Total number of given penalties by host",
29+
Namespace: namespace,
30+
Name: "host_penalties_total",
31+
Help: "Total number of given penalties by host",
32+
ConstLabels: constLabels,
2933
},
3034
[]string{"cluster", "replica", "cluster_node"},
3135
)

main_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,16 @@ func TestServe(t *testing.T) {
640640
"testdata/http.metrics.namespace.yml",
641641
func(t *testing.T) {
642642
resp := httpGet(t, "http://127.0.0.1:9090/metrics", http.StatusOK)
643-
assert.GreaterOrEqual(t, len(getStringFromResponse(t, resp.Body)), 10000)
643+
metricsString := getStringFromResponse(t, resp.Body)
644+
assert.GreaterOrEqual(t, len(metricsString), 10000)
645+
assert.GreaterOrEqual(
646+
t,
647+
strings.Count(
648+
metricsString,
649+
`constant_label1="value1",constant_label2="value2"`,
650+
),
651+
10,
652+
)
644653
resp.Body.Close()
645654
},
646655
startHTTP,
@@ -973,6 +982,11 @@ func TestServe(t *testing.T) {
973982
}
974983

975984
cfg := &config.Config{}
985+
cfg.Server.Metrics.ConstantLabels = map[string]string{
986+
"constant_label1": "value1",
987+
"constant_label2": "value2",
988+
}
989+
976990
registerMetrics(cfg)
977991
for _, tc := range testCases {
978992
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)