Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- `<networks>`: string value consisting of IP, IP mask or named group, for example `"127.0.0.1"` or `"127.0.0.1/24"`.
- `<host_name>`: string value consisting of host name, for example `"example.com"`
- `<byte_size>`: string value matching the regular expression `/^\d+(\.\d+)?[KMGTP]?B?$/i`, for example `"100MB"`
- `<map[label_name]label_value>`: а map consisting of string keys and string values for optional custom labels in metrics

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

# Prometheus metric namespace
namespace: <string> | optional

# Labels that should be added to each sent prometheus metrics
constant_labels: <map[label_name]label_value>
```

### <user_config>
Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,9 @@ type Metrics struct {
// Prometheus metric namespace
Namespace string `yaml:"namespace,omitempty"`

// Constant labels which will be added to each sent prometheus metric
ConstantLabels map[string]string `yaml:"constant_labels,omitempty"`

// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline"`
}
Expand Down
2 changes: 2 additions & 0 deletions docs/src/content/docs/cn/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ server:
# By default access to `/metrics` is unrestricted.
metrics:
allowed_networks: ["office"]
# You can specify constant labels which will be added to each sent prometheus metric
constant_labels: {}

# Configs for input users.
users:
Expand Down
2 changes: 2 additions & 0 deletions docs/src/content/docs/configuration/default.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ server:
metrics:
allowed_networks: ["office"]
namespace: ""
# You can specify constant labels which will be added to each sent prometheus metric
constant_labels: {}

# Proxy settings enable parsing proxy headers in cases where
# CHProxy is run behind another proxy.
Expand Down
16 changes: 10 additions & 6 deletions internal/topology/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@ var (

func initMetrics(cfg *config.Config) {
namespace := cfg.Server.Metrics.Namespace
constLabels := cfg.Server.Metrics.ConstantLabels

HostHealth = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "host_health",
Help: "Health state of hosts by clusters",
Namespace: namespace,
Name: "host_health",
Help: "Health state of hosts by clusters",
ConstLabels: constLabels,
},
[]string{"cluster", "replica", "cluster_node"},
)
HostPenalties = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Name: "host_penalties_total",
Help: "Total number of given penalties by host",
Namespace: namespace,
Name: "host_penalties_total",
Help: "Total number of given penalties by host",
ConstLabels: constLabels,
},
[]string{"cluster", "replica", "cluster_node"},
)
Expand Down
16 changes: 15 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,16 @@ func TestServe(t *testing.T) {
"testdata/http.metrics.namespace.yml",
func(t *testing.T) {
resp := httpGet(t, "http://127.0.0.1:9090/metrics", http.StatusOK)
assert.GreaterOrEqual(t, len(getStringFromResponse(t, resp.Body)), 10000)
metricsString := getStringFromResponse(t, resp.Body)
assert.GreaterOrEqual(t, len(metricsString), 10000)
assert.GreaterOrEqual(
t,
strings.Count(
metricsString,
`constant_label1="value1",constant_label2="value2"`,
),
10,
)
resp.Body.Close()
},
startHTTP,
Expand Down Expand Up @@ -973,6 +982,11 @@ func TestServe(t *testing.T) {
}

cfg := &config.Config{}
cfg.Server.Metrics.ConstantLabels = map[string]string{
"constant_label1": "value1",
"constant_label2": "value2",
}

registerMetrics(cfg)
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
Loading