-
Couldn't load subscription status.
- Fork 2.4k
Carry #780: Export env variables as prometheus labels #1023
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ package metrics | |
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
| "time" | ||
|
|
||
| info "github.com/google/cadvisor/info/v1" | ||
|
|
@@ -508,11 +509,20 @@ func (c *PrometheusCollector) collectContainersInfo(ch chan<- prometheus.Metric) | |
| if c.containerNameToLabels != nil { | ||
| newLabels := c.containerNameToLabels(name) | ||
| for k, v := range newLabels { | ||
| baseLabels = append(baseLabels, k) | ||
| baseLabels = append(baseLabels, sanitizeLabelName(k)) | ||
| baseLabelValues = append(baseLabelValues, v) | ||
| } | ||
| } | ||
|
|
||
| for k, v := range container.Spec.Labels { | ||
| baseLabels = append(baseLabels, sanitizeLabelName(k)) | ||
| baseLabelValues = append(baseLabelValues, v) | ||
| } | ||
| for k, v := range container.Spec.Envs { | ||
| baseLabels = append(baseLabels, sanitizeLabelName(k)) | ||
| baseLabelValues = append(baseLabelValues, v) | ||
| } | ||
|
|
||
| // Container spec | ||
| desc := prometheus.NewDesc("container_start_time_seconds", "Start time of the container since unix epoch in seconds.", baseLabels, nil) | ||
| ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, float64(container.Spec.CreationTime.Unix()), baseLabelValues...) | ||
|
|
@@ -571,3 +581,11 @@ func specMemoryValue(v uint64) float64 { | |
| } | ||
| return float64(v) | ||
| } | ||
|
|
||
| var invalidLabelCharRE = regexp.MustCompile(`[^a-zA-Z0-9_]`) | ||
|
|
||
| // sanitizeLabelName replaces anything that doesn't match | ||
| // client_label.LabelNameRE with an underscore. | ||
| func sanitizeLabelName(name string) string { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this needed? Does it make sense to enable it by default? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From: http://prometheus.io/docs/concepts/data_model/#metric-names-and-labels There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should sanitize rather than drop. This is normal practice for prometheus metrics. |
||
| return invalidLabelCharRE.ReplaceAllString(name, "_") | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Update the description. The existing description is valid for
Labelsfield. Let's add a new one forEnvsand mention that only whitelisted env vars are exposed, to avoid users filing issues that this field is always empty.