Skip to content

Commit

Permalink
add whitelisted env as container metadata
Browse files Browse the repository at this point in the history
This add Envs to container spec as a metadata source. When using prometheus
exposition format, they will be merged into the list of metrics' labels.

Also changed the cli flag to docker_env_metadata_whitelist, and add refenrences
of whitelist envs to API

Signed-off-by: Daniel Dao <dqminh@cloudflare.com>
  • Loading branch information
dqminh committed Jan 13, 2016
1 parent 544b852 commit e5b6bfa
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 68 deletions.
6 changes: 3 additions & 3 deletions container/docker/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var dockerCgroupRegexp = regexp.MustCompile(`.+-([a-z0-9]{64})\.scope$`)

var noSystemd = flag.Bool("nosystemd", false, "Explicitly disable systemd support for Docker containers")

var dockerMetadataEnvs = flag.String("docker_metadata_env", "", "Comma seperated list with names of env variables, which will be exported as metadata (default: empty)")
var dockerEnvWhitelist = flag.String("docker_env_metadata_whitelist", "", "a comma-separated list of environment variable keys that needs to be collected for docker containers")

// TODO(vmarmol): Export run dir too for newer Dockers.
// Directory holding Docker container state information.
Expand Down Expand Up @@ -120,7 +120,7 @@ func (self *dockerFactory) NewContainerHandler(name string, inHostNamespace bool
return
}

exposedMetadata := strings.Split(*dockerMetadataEnvs, ",")
metadataEnvs := strings.Split(*dockerEnvWhitelist, ",")

handler, err = newDockerContainerHandler(
client,
Expand All @@ -130,7 +130,7 @@ func (self *dockerFactory) NewContainerHandler(name string, inHostNamespace bool
self.storageDriver,
&self.cgroupSubsystems,
inHostNamespace,
exposedMetadata,
metadataEnvs,
)
return
}
Expand Down
14 changes: 7 additions & 7 deletions container/docker/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ type dockerContainerHandler struct {
// Time at which this container was created.
creationTime time.Time

// Metadata labels associated with the container.
// Metadata associated with the container.
labels map[string]string
envs map[string]string

// The container PID used to switch namespaces as required
pid int
Expand All @@ -93,7 +94,7 @@ func newDockerContainerHandler(
storageDriver storageDriver,
cgroupSubsystems *containerlibcontainer.CgroupSubsystems,
inHostNamespace bool,
exposedMetadata []string,
metadataEnvs []string,
) (container.ContainerHandler, error) {
// Create the cgroup paths.
cgroupPaths := make(map[string]string, len(cgroupSubsystems.MountPoints))
Expand Down Expand Up @@ -159,13 +160,11 @@ func newDockerContainerHandler(
handler.networkMode = ctnr.HostConfig.NetworkMode

// split env vars to get metadata map.
if len(exposedMetadata) > 0 {
for _, exposedEnv := range metadataEnvs {
for _, envVar := range ctnr.Config.Env {
splits := strings.SplitN(envVar, "=", 2)
for _, exposedVar := range exposedMetadata {
if splits[0] == exposedVar {
handler.labels[strings.ToLower(exposedVar)] = splits[1]
}
if splits[0] == exposedEnv {
handler.envs[strings.ToLower(exposedEnv)] = splits[1]
}
}
}
Expand Down Expand Up @@ -259,6 +258,7 @@ func (self *dockerContainerHandler) GetSpec() (info.ContainerSpec, error) {
}

spec.Labels = self.labels
spec.Envs = self.envs
spec.Image = self.image
spec.HasNetwork = hasNet(self.networkMode)

Expand Down
2 changes: 2 additions & 0 deletions info/v1/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type ContainerSpec struct {

// Metadata labels associated with this container.
Labels map[string]string `json:"labels,omitempty"`
// Metadata envs associated with this container. Only whitelisted envs are added.
Envs map[string]string `json:"envs,omitempty"`

HasCpu bool `json:"has_cpu"`
Cpu CpuSpec `json:"cpu,omitempty"`
Expand Down
2 changes: 2 additions & 0 deletions info/v2/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ type ContainerSpec struct {

// Metadata labels associated with this container.
Labels map[string]string `json:"labels,omitempty"`
// Metadata envs associated with this container. Only whitelisted envs are added.
Envs map[string]string `json:"envs,omitempty"`

HasCpu bool `json:"has_cpu"`
Cpu CpuSpec `json:"cpu,omitempty"`
Expand Down
10 changes: 7 additions & 3 deletions metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,13 @@ func (c *PrometheusCollector) collectContainersInfo(ch chan<- prometheus.Metric)
}
}

for labelKey, labelValue := range container.Spec.Labels {
baseLabels = append(baseLabels, sanitizeLabelName(labelKey))
baseLabelValues = append(baseLabelValues, labelValue)
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
Expand Down
5 changes: 4 additions & 1 deletion metrics/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ func (p testSubcontainersInfoProvider) SubcontainersInfo(string, *info.Container
Image: "test",
CreationTime: time.Unix(1257894000, 0),
Labels: map[string]string{
"foo.metric": "bar",
"foo.label": "bar",
},
Envs: map[string]string{
"foo+env": "prod",
},
},
Stats: []*info.ContainerStats{
Expand Down
Loading

0 comments on commit e5b6bfa

Please sign in to comment.