Skip to content
This repository has been archived by the owner on Aug 31, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1 from juliusv/more-labels
Browse files Browse the repository at this point in the history
Attach task and service meta labels to targets
  • Loading branch information
nustiueudinastea committed Apr 10, 2017
2 parents 86db8de + db11d39 commit b5a6f7a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ RUN cd /go/src/github.com/weaveworks/prometheus-swarm/ && glide install

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /app/main /go/src/github.com/weaveworks/prometheus-swarm/swarm.go

ENTRYPOINT ["/app/main", "discover"]
ENTRYPOINT ["/app/main", "discover"]
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ services:
prometheus.port: 8079
```

## Metadata labels

The discovery tool attaches a set of metadata labels to each target that are available during the [relabeling phase](https://prometheus.io/docs/operating/configuration/#<relabel_config>) of the service discovery in Prometheus:

* `__meta_docker_service_label_<labelname>`: The value of this service label.
* `__meta_docker_task_label_<labelname>`: The value of this task label.
* `__meta_docker_task_name`: The name of the Docker task.

Labels starting with `__` are removed after the relabeling phase, so that these labels will not show up on time series directly.

## Excluding services

To exclude a specific service from being included in the scrape targets, add a label of format `prometheus.ignore: "true"`.
Expand Down
24 changes: 16 additions & 8 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 21 additions & 4 deletions swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/util/strutil"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -211,6 +213,23 @@ func collectIPs(task swarm.Task) ([]net.IP, map[string]swarm.Network) {
return containerIPs, taskNetworks
}

func taskLabels(task swarm.Task, serviceIDMap map[string]swarm.Service) map[string]string {
service := serviceIDMap[task.ServiceID]
labels := map[string]string{
model.JobLabel: service.Spec.Name,

model.MetaLabelPrefix + "docker_task_name": task.Name,
model.MetaLabelPrefix + "docker_task_desired_state": string(task.DesiredState),
}
for k, v := range task.Labels {
labels[strutil.SanitizeLabelName(model.MetaLabelPrefix+"docker_task_label_"+k)] = v
}
for k, v := range service.Spec.Labels {
labels[strutil.SanitizeLabelName(model.MetaLabelPrefix+"docker_service_label_"+k)] = v
}
return labels
}

func discoverSwarm(prometheusContainerID string, outputFile string, discoveryType string) {
cli, err := client.NewEnvClient()
if err != nil {
Expand Down Expand Up @@ -275,12 +294,10 @@ func discoverSwarm(prometheusContainerID string, outputFile string, discoveryTyp

scrapetask := scrapeTask{
Targets: taskEndpoints,
Labels: map[string]string{
"job": serviceIDMap[task.ServiceID].Spec.Name,
},
Labels: taskLabels(task, serviceIDMap),
}
scrapeTasks = append(scrapeTasks, scrapetask)

scrapeTasks = append(scrapeTasks, scrapetask)
}

err = connectNetworks(allNetworks, prometheusContainerID)
Expand Down

0 comments on commit b5a6f7a

Please sign in to comment.