mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-28 06:59:40 -08:00
Merge pull request #987 from prometheus/fabxc/marathonfix
Add application labels as meta labels
This commit is contained in:
commit
9e9f88b5ce
|
@ -1,16 +1,19 @@
|
||||||
package marathon
|
package marathon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/prometheus/client_golang/model"
|
clientmodel "github.com/prometheus/client_golang/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
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 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 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 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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
clientmodel "github.com/prometheus/client_golang/model"
|
clientmodel "github.com/prometheus/client_golang/model"
|
||||||
|
|
||||||
|
@ -13,10 +12,8 @@ import (
|
||||||
func AppsToTargetGroups(apps *AppList) map[string]*config.TargetGroup {
|
func AppsToTargetGroups(apps *AppList) map[string]*config.TargetGroup {
|
||||||
tgroups := map[string]*config.TargetGroup{}
|
tgroups := map[string]*config.TargetGroup{}
|
||||||
for _, a := range apps.Apps {
|
for _, a := range apps.Apps {
|
||||||
if isValidApp(&a) {
|
group := createTargetGroup(&a)
|
||||||
group := createTargetGroup(&a)
|
tgroups[group.Source] = group
|
||||||
tgroups[group.Source] = group
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return tgroups
|
return tgroups
|
||||||
}
|
}
|
||||||
|
@ -24,18 +21,24 @@ func AppsToTargetGroups(apps *AppList) map[string]*config.TargetGroup {
|
||||||
func createTargetGroup(app *App) *config.TargetGroup {
|
func createTargetGroup(app *App) *config.TargetGroup {
|
||||||
var (
|
var (
|
||||||
targets = targetsForApp(app)
|
targets = targetsForApp(app)
|
||||||
source = targetGroupName(app)
|
appName = clientmodel.LabelValue(app.ID)
|
||||||
appName = clientmodel.LabelValue(sanitizeName(app.ID))
|
image = clientmodel.LabelValue(app.Container.Docker.Image)
|
||||||
image = clientmodel.LabelValue(imageName(app))
|
|
||||||
)
|
)
|
||||||
return &config.TargetGroup{
|
tg := &config.TargetGroup{
|
||||||
Targets: targets,
|
Targets: targets,
|
||||||
Labels: clientmodel.LabelSet{
|
Labels: clientmodel.LabelSet{
|
||||||
AppLabel: appName,
|
appLabel: appName,
|
||||||
ImageLabel: image,
|
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 {
|
func targetsForApp(app *App) []clientmodel.LabelSet {
|
||||||
|
@ -44,33 +47,12 @@ func targetsForApp(app *App) []clientmodel.LabelSet {
|
||||||
target := targetForTask(&t)
|
target := targetForTask(&t)
|
||||||
targets = append(targets, clientmodel.LabelSet{
|
targets = append(targets, clientmodel.LabelSet{
|
||||||
clientmodel.AddressLabel: clientmodel.LabelValue(target),
|
clientmodel.AddressLabel: clientmodel.LabelValue(target),
|
||||||
TaskLabel: clientmodel.LabelValue(sanitizeName(t.ID)),
|
taskLabel: clientmodel.LabelValue(t.ID),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return targets
|
return targets
|
||||||
}
|
}
|
||||||
|
|
||||||
func imageName(app *App) string {
|
|
||||||
return app.Container.Docker.Image
|
|
||||||
}
|
|
||||||
|
|
||||||
func targetForTask(task *Task) string {
|
func targetForTask(task *Task) string {
|
||||||
return fmt.Sprintf("%s:%d", task.Host, task.Ports[0])
|
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) {
|
func TestMarathonSDRemoveApp(t *testing.T) {
|
||||||
ch, md := newTestDiscovery(func(url string) (*marathon.AppList, error) {
|
ch, md := newTestDiscovery(func(url string) (*marathon.AppList, error) {
|
||||||
return marathonTestAppList(marathonValidLabel, 1), nil
|
return marathonTestAppList(marathonValidLabel, 1), nil
|
||||||
|
|
Loading…
Reference in a new issue