Skip to content
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

optionally expose Docker env variables as labels #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ The command line parameters that can be used are:
* -config.filter-label (string): docker label (and optional value) to filter on "NAME_OF_LABEL[=VALUE]".
* -config.port-label (string): Docker label to define the scrape port of the application
(if missing an application won't be scraped) (default "PROMETHEUS_EXPORTER_PORT")
* -config.env-vars (string): Docker env variables to expose if they exist
(multiple values can be specified, separated by ',')

## Usage

Expand Down
52 changes: 26 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,6 @@ import (
"github.com/go-yaml/yaml"
)

type labels struct {
TaskArn string `yaml:"task_arn"`
TaskName string `yaml:"task_name"`
JobName string `yaml:"job,omitempty"`
TaskRevision string `yaml:"task_revision"`
TaskGroup string `yaml:"task_group"`
ClusterArn string `yaml:"cluster_arn"`
ContainerName string `yaml:"container_name"`
ContainerArn string `yaml:"container_arn"`
DockerImage string `yaml:"docker_image"`
MetricsPath string `yaml:"__metrics_path__,omitempty"`
}

var cluster = flag.String("config.cluster", "", "name of the cluster to scrape")
var outFile = flag.String("config.write-to", "ecs_file_sd.yml", "path of file to write ECS service discovery information to")
var interval = flag.Duration("config.scrape-interval", 60*time.Second, "interval at which to scrape the AWS API for ECS service discovery information")
Expand All @@ -55,6 +42,7 @@ var prometheusPathLabel = flag.String("config.path-label", "PROMETHEUS_EXPORTER_
var prometheusFilterLabel = flag.String("config.filter-label", "", "Docker label (and optionally value) to require to scrape the application")
var prometheusServerNameLabel = flag.String("config.server-name-label", "PROMETHEUS_EXPORTER_SERVER_NAME", "Docker label to define the server name")
var prometheusJobNameLabel = flag.String("config.job-name-label", "PROMETHEUS_EXPORTER_JOB_NAME", "Docker label to define the job name")
var prometheusEnvVars = flag.String("config.env-vars", "", "Docker env variables to expose if they exist (multiple values can be specified, separated by ',')")

// logError is a convenience function that decodes all possible ECS
// errors and displays them to standard error.
Expand Down Expand Up @@ -123,8 +111,8 @@ type PrometheusContainer struct {
// PrometheusTaskInfo is the final structure that will be
// output as a Prometheus file service discovery config.
type PrometheusTaskInfo struct {
Targets []string `yaml:"targets"`
Labels labels `yaml:"labels"`
Targets []string `yaml:"targets"`
Labels map[string]string `yaml:"labels"`
}

// ExporterInformation returns a list of []*PrometheusTaskInfo
Expand Down Expand Up @@ -251,21 +239,33 @@ func (t *AugmentedTask) ExporterInformation() []*PrometheusTaskInfo {
host = ip
}

labels := labels{
TaskArn: *t.TaskArn,
TaskName: *t.TaskDefinition.Family,
JobName: d.DockerLabels[*prometheusJobNameLabel],
TaskRevision: fmt.Sprintf("%d", *t.TaskDefinition.Revision),
TaskGroup: *t.Group,
ClusterArn: *t.ClusterArn,
ContainerName: *i.Name,
ContainerArn: *i.ContainerArn,
DockerImage: *d.Image,
labels := map[string]string{
"task_arn": *t.TaskArn,
"task_name": *t.TaskDefinition.Family,
"task_revision": fmt.Sprintf("%d", *t.TaskDefinition.Revision),
"task_group": *t.Group,
"cluster_arn": *t.ClusterArn,
"container_name": *i.Name,
"container_arn": *i.ContainerArn,
"docker_image": *d.Image,
}

if jobName := d.DockerLabels[*prometheusJobNameLabel]; jobName != "" {
labels["job"] = jobName
}

exporterPath, ok = d.DockerLabels[*prometheusPathLabel]
if ok {
labels.MetricsPath = exporterPath
labels["__metrics_path__"] = exporterPath
}

envVars := strings.Split(*prometheusEnvVars, ",")
for _, envVar := range envVars {
for _, keyPair := range d.Environment {
if *keyPair.Name == envVar {
labels[envVar] = *keyPair.Value
}
}
}

ret = append(ret, &PrometheusTaskInfo{
Expand Down