mirror of
https://github.com/prometheus/prometheus
synced 2024-12-28 01:32:19 +00:00
Add application labels as meta labels
Removes built-in conditional scraping based on application's 'prometheus' label.
This commit is contained in:
parent
15b4115a25
commit
8fa9ec278b
@ -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"
|
||||
)
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user