Skip to content

Commit

Permalink
Merge pull request #1984 from jaloren/filter_docker_prom_mets
Browse files Browse the repository at this point in the history
Control whether container labels are exported as prometheus metrics.
  • Loading branch information
dashpole authored Jul 4, 2018
2 parents 28c3221 + 4bd335b commit 9828330
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cadvisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"crypto/tls"

"github.com/golang/glog"
"github.com/google/cadvisor/metrics"
)

var argIp = flag.String("listen_ip", "", "IP to listen on, defaults to all IPs")
Expand All @@ -58,6 +59,8 @@ var enableProfiling = flag.Bool("profiling", false, "Enable profiling via web in
var collectorCert = flag.String("collector_cert", "", "Collector's certificate, exposed to endpoints for certificate based authentication.")
var collectorKey = flag.String("collector_key", "", "Key for the collector's certificate")

var storeContainerLabels = flag.Bool("store_container_labels", true, "convert container labels and environment variables into labels on prometheus metrics for each container. If flag set to false, then only metrics exported are container name, first alias, and image name")

var (
// Metrics to be ignored.
// Tcp metrics are ignored by default.
Expand Down Expand Up @@ -152,7 +155,11 @@ func main() {
glog.Fatalf("Failed to register HTTP handlers: %v", err)
}

cadvisorhttp.RegisterPrometheusHandler(mux, containerManager, *prometheusEndpoint, nil)
containerLabelFunc := metrics.DefaultContainerLabels
if !*storeContainerLabels {
containerLabelFunc = metrics.BaseContainerLabels
}
cadvisorhttp.RegisterPrometheusHandler(mux, containerManager, *prometheusEndpoint, containerLabelFunc)

// Start the manager.
if err := containerManager.Start(); err != nil {
Expand Down
13 changes: 13 additions & 0 deletions metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,19 @@ func DefaultContainerLabels(container *info.ContainerInfo) map[string]string {
return set
}

// BaseContainerLabels implements ContainerLabelsFunc. It only exports the
// container name, first alias, and image name.
func BaseContainerLabels(container *info.ContainerInfo) map[string]string {
set := map[string]string{LabelID: container.Name}
if len(container.Aliases) > 0 {
set[LabelName] = container.Aliases[0]
}
if image := container.Spec.Image; len(image) > 0 {
set[LabelImage] = image
}
return set
}

func (c *PrometheusCollector) collectContainersInfo(ch chan<- prometheus.Metric) {
containers, err := c.infoProvider.SubcontainersInfo("/", &info.ContainerInfoRequest{NumStats: 1})
if err != nil {
Expand Down

0 comments on commit 9828330

Please sign in to comment.