Skip to content

Commit

Permalink
Update static-discovery config to supply __address__
Browse files Browse the repository at this point in the history
  • Loading branch information
goller committed May 18, 2017
1 parent 922790b commit 95d8f7c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
3 changes: 2 additions & 1 deletion etc/kapacitor/kapacitor.conf
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,9 @@ default-retention-policy = ""
#[[static-discovery]]
# enabled = false
# id = "mystatic"
# targets = []
# targets = ["localhost:9100"]
# [static.labels]
# region = "us-east-1"
#
#[[triton]]
# enabled = false
Expand Down
10 changes: 6 additions & 4 deletions services/static_discovery/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ type Config struct {
ID string `toml:"id" override:"id"`
// Targets is a list of targets identified by a label set. Each target is
// uniquely identifiable in the group by its address label.
Targets []map[string]string `toml:"targets" override:"targets"`
Targets []string `toml:"targets" override:"targets"`
// Labels is a set of labels that is common across all targets in the group.
Labels map[string]string `toml:"labels" override:"labels"`
}

// Init the static configuration to an empty set of structures
func (s *Config) Init() {
s.Targets = []map[string]string{}
s.Targets = []string{}
s.Labels = map[string]string{}
}

Expand All @@ -46,10 +46,12 @@ func (s Config) PromConfig() []*config.TargetGroup {
}
return res
}
target := func(t []map[string]string) []model.LabelSet {
target := func(t []string) []model.LabelSet {
res := make([]model.LabelSet, len(t))
for i, l := range t {
res[i] = set(l)
res[i] = model.LabelSet{
model.LabelName(model.AddressLabel): model.LabelValue(l),
}
}
return res
}
Expand Down
64 changes: 64 additions & 0 deletions services/static_discovery/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package static_discovery

import (
"reflect"
"testing"

"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/config"
)

func TestConfig_PromConfig(t *testing.T) {
type fields struct {
Enabled bool
ID string
Targets []string
Labels map[string]string
}
tests := []struct {
name string
fields fields
want []*config.TargetGroup
}{
{
name: "Test Address Label",
fields: fields{
ID: "mylocalhost",
Targets: []string{
"localhost:9100",
"localhost:9200",
},
Labels: map[string]string{
"my": "neat",
"metrics": "host",
},
},
want: []*config.TargetGroup{
&config.TargetGroup{
Source: "mylocalhost",
Targets: []model.LabelSet{
{model.AddressLabel: "localhost:9100"},
{model.AddressLabel: "localhost:9200"},
},
Labels: model.LabelSet{
"my": "neat",
"metrics": "host",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := Config{
Enabled: tt.fields.Enabled,
ID: tt.fields.ID,
Targets: tt.fields.Targets,
Labels: tt.fields.Labels,
}
if got := s.PromConfig(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Config.PromConfig() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 95d8f7c

Please sign in to comment.