Skip to content

Commit 811ccb0

Browse files
authored
Merge pull request #261 from grafana/beorn7/prom-config
Change the instance name for standard pod scraping to be unique
2 parents 809d4c4 + 0ccca40 commit 811ccb0

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

prometheus-ksonnet/lib/prometheus-config.libsonnet

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,34 @@
134134
replacement: '$1',
135135
},
136136

137-
// But also include the namespace as a separate label, for routing alerts
137+
// But also include the namespace, container, pod as separate labels,
138+
// for routing alerts and joining with cAdvisor metrics.
138139
{
139140
source_labels: ['__meta_kubernetes_namespace'],
140141
action: 'replace',
141142
target_label: 'namespace',
142143
},
143-
144-
// Rename instances to be the pod name
145144
{
146145
source_labels: ['__meta_kubernetes_pod_name'],
147146
action: 'replace',
147+
target_label: 'pod', // Not 'pod_name', which disappeared in K8s 1.16.
148+
},
149+
{
150+
source_labels: ['__meta_kubernetes_container_name'],
151+
action: 'replace',
152+
target_label: 'container', // Not 'container_name', which disappeared in K8s 1.16.
153+
},
154+
155+
// Rename instances to the concatenation of pod:container:port.
156+
// All three components are needed to guarantee a unique instance label.
157+
{
158+
source_labels: [
159+
'__meta_kubernetes_pod_name',
160+
'__meta_kubernetes_pod_container_name',
161+
'__meta_kubernetes_pod_container_port_name',
162+
],
163+
action: 'replace',
164+
separator: ':',
148165
target_label: 'instance',
149166
},
150167

@@ -163,10 +180,12 @@
163180
],
164181
},
165182

166-
// A separate scrape config for kube-state-metrics which doesn't add a namespace
167-
// label, instead taking the namespace label from the exported timeseries. This
168-
// prevents the exported namespace label being renamed to exported_namespace, and
169-
// allows us to route alerts based on namespace.
183+
// A separate scrape config for kube-state-metrics which doesn't
184+
// add namespace, container, and pod labels, instead taking
185+
// those labels from the exported timeseries. This prevents them
186+
// being renamed to exported_namespace etc. and allows us to
187+
// route alerts based on namespace and join KSM metrics with
188+
// cAdvisor metrics.
170189
{
171190
job_name: '%s/kube-state-metrics' % $._config.namespace,
172191
kubernetes_sd_configs: [{
@@ -192,11 +211,16 @@
192211
action: 'keep',
193212
},
194213

195-
// Rename instances to be the pod name.
196-
// As the scrape two ports of KSM, include the port name in the instance
197-
// name. Otherwise alerts about scrape failures and timeouts won't work.
214+
// Rename instances to the concatenation of pod:container:port.
215+
// In the specific case of KSM, we could leave out the container
216+
// name and still have a unique instance label, but we leave it
217+
// in here for consistency with the normal pod scraping.
198218
{
199-
source_labels: ['__meta_kubernetes_pod_name', '__meta_kubernetes_pod_container_port_name'],
219+
source_labels: [
220+
'__meta_kubernetes_pod_name',
221+
'__meta_kubernetes_pod_container_name',
222+
'__meta_kubernetes_pod_container_port_name',
223+
],
200224
action: 'replace',
201225
separator: ':',
202226
target_label: 'instance',

0 commit comments

Comments
 (0)