From 8fa9ec278be58bf1f9ae7a5128f85a248d9d6361 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Fri, 14 Aug 2015 14:24:31 +0200 Subject: [PATCH] Add application labels as meta labels Removes built-in conditional scraping based on application's 'prometheus' label. --- retrieval/discovery/marathon/constants.go | 15 ++++--- retrieval/discovery/marathon/conversion.go | 50 +++++++--------------- retrieval/discovery/marathon_test.go | 34 --------------- 3 files changed, 25 insertions(+), 74 deletions(-) diff --git a/retrieval/discovery/marathon/constants.go b/retrieval/discovery/marathon/constants.go index 7b761a660..41764cc3f 100644 --- a/retrieval/discovery/marathon/constants.go +++ b/retrieval/discovery/marathon/constants.go @@ -1,16 +1,19 @@ package marathon import ( - "github.com/prometheus/client_golang/model" + clientmodel "github.com/prometheus/client_golang/model" ) const ( + // metaLabelPrefix is the meta prefix used for all meta labels in this discovery. + metaLabelPrefix = clientmodel.MetaLabelPrefix + "marathon_" + // appLabelPrefix is the prefix for the application labels. + appLabelPrefix = metaLabelPrefix + "app_label_" + // AppLabel is used for the name of the app in Marathon. - AppLabel model.LabelName = "__meta_marathon_app" - + appLabel clientmodel.LabelName = metaLabelPrefix + "app" // ImageLabel is the label that is used for the docker image running the service. - ImageLabel model.LabelName = "__meta_marathon_image" - + imageLabel clientmodel.LabelName = metaLabelPrefix + "image" // TaskLabel contains the mesos task name of the app instance. - TaskLabel model.LabelName = "__meta_marathon_task" + taskLabel clientmodel.LabelName = metaLabelPrefix + "task" ) diff --git a/retrieval/discovery/marathon/conversion.go b/retrieval/discovery/marathon/conversion.go index c6405e2f7..4acf499b9 100644 --- a/retrieval/discovery/marathon/conversion.go +++ b/retrieval/discovery/marathon/conversion.go @@ -2,7 +2,6 @@ package marathon import ( "fmt" - "strings" clientmodel "github.com/prometheus/client_golang/model" @@ -13,10 +12,8 @@ import ( func AppsToTargetGroups(apps *AppList) map[string]*config.TargetGroup { tgroups := map[string]*config.TargetGroup{} for _, a := range apps.Apps { - if isValidApp(&a) { - group := createTargetGroup(&a) - tgroups[group.Source] = group - } + group := createTargetGroup(&a) + tgroups[group.Source] = group } return tgroups } @@ -24,18 +21,24 @@ func AppsToTargetGroups(apps *AppList) map[string]*config.TargetGroup { func createTargetGroup(app *App) *config.TargetGroup { var ( targets = targetsForApp(app) - source = targetGroupName(app) - appName = clientmodel.LabelValue(sanitizeName(app.ID)) - image = clientmodel.LabelValue(imageName(app)) + appName = clientmodel.LabelValue(app.ID) + image = clientmodel.LabelValue(app.Container.Docker.Image) ) - return &config.TargetGroup{ + tg := &config.TargetGroup{ Targets: targets, Labels: clientmodel.LabelSet{ - AppLabel: appName, - ImageLabel: image, + appLabel: appName, + imageLabel: image, }, - Source: source, + Source: app.ID, } + + for ln, lv := range app.Labels { + ln = appLabelPrefix + ln + tg.Labels[clientmodel.LabelName(ln)] = clientmodel.LabelValue(lv) + } + + return tg } func targetsForApp(app *App) []clientmodel.LabelSet { @@ -44,33 +47,12 @@ func targetsForApp(app *App) []clientmodel.LabelSet { target := targetForTask(&t) targets = append(targets, clientmodel.LabelSet{ clientmodel.AddressLabel: clientmodel.LabelValue(target), - TaskLabel: clientmodel.LabelValue(sanitizeName(t.ID)), + taskLabel: clientmodel.LabelValue(t.ID), }) } return targets } -func imageName(app *App) string { - return app.Container.Docker.Image -} - func targetForTask(task *Task) string { return fmt.Sprintf("%s:%d", task.Host, task.Ports[0]) } - -func isValidApp(app *App) bool { - if app.RunningTasks > 0 { - _, ok := app.Labels["prometheus"] - return ok - } - return false -} - -func targetGroupName(app *App) string { - return sanitizeName(app.ID) -} - -func sanitizeName(id string) string { - trimID := strings.TrimLeft(id, " -/.") - return strings.Replace(trimID, "/", "-", -1) -} diff --git a/retrieval/discovery/marathon_test.go b/retrieval/discovery/marathon_test.go index fcf11562c..7a1bbf9b1 100644 --- a/retrieval/discovery/marathon_test.go +++ b/retrieval/discovery/marathon_test.go @@ -104,40 +104,6 @@ func TestMarathonSDSendGroup(t *testing.T) { } } -func TestMarathonSDNoLabel(t *testing.T) { - ch, md := newTestDiscovery(func(url string) (*marathon.AppList, error) { - return marathonTestAppList(map[string]string{}, 1), nil - }) - go func() { - select { - case tg := <-ch: - t.Fatalf("Got group: %s", tg) - default: - } - }() - err := md.updateServices(ch) - if err != nil { - t.Fatalf("Got error: %s", err) - } -} - -func TestMarathonSDNotRunning(t *testing.T) { - ch, md := newTestDiscovery(func(url string) (*marathon.AppList, error) { - return marathonTestAppList(marathonValidLabel, 0), nil - }) - go func() { - select { - case tg := <-ch: - t.Fatalf("Got group: %s", tg) - default: - } - }() - err := md.updateServices(ch) - if err != nil { - t.Fatalf("Got error: %s", err) - } -} - func TestMarathonSDRemoveApp(t *testing.T) { ch, md := newTestDiscovery(func(url string) (*marathon.AppList, error) { return marathonTestAppList(marathonValidLabel, 1), nil