diff --git a/discovery/triton/triton.go b/discovery/triton/triton.go index 09914faa0..14e2a054f 100644 --- a/discovery/triton/triton.go +++ b/discovery/triton/triton.go @@ -19,6 +19,8 @@ import ( "fmt" "io/ioutil" "net/http" + "net/url" + "strings" "time" "github.com/go-kit/kit/log" @@ -33,6 +35,7 @@ import ( const ( tritonLabel = model.MetaLabelPrefix + "triton_" + tritonLabelGroups = tritonLabel + "groups" tritonLabelMachineID = tritonLabel + "machine_id" tritonLabelMachineAlias = tritonLabel + "machine_alias" tritonLabelMachineBrand = tritonLabel + "machine_brand" @@ -65,6 +68,7 @@ type SDConfig struct { Account string `yaml:"account"` DNSSuffix string `yaml:"dns_suffix"` Endpoint string `yaml:"endpoint"` + Groups []string `yaml:"groups,omitempty"` Port int `yaml:"port"` RefreshInterval model.Duration `yaml:"refresh_interval,omitempty"` TLSConfig config_util.TLSConfig `yaml:"tls_config,omitempty"` @@ -102,11 +106,12 @@ func init() { // DiscoveryResponse models a JSON response from the Triton discovery. type DiscoveryResponse struct { Containers []struct { - ServerUUID string `json:"server_uuid"` - VMAlias string `json:"vm_alias"` - VMBrand string `json:"vm_brand"` - VMImageUUID string `json:"vm_image_uuid"` - VMUUID string `json:"vm_uuid"` + Groups []string `json:"groups"` + ServerUUID string `json:"server_uuid"` + VMAlias string `json:"vm_alias"` + VMBrand string `json:"vm_brand"` + VMImageUUID string `json:"vm_image_uuid"` + VMUUID string `json:"vm_uuid"` } `json:"containers"` } @@ -187,6 +192,11 @@ func (d *Discovery) refresh() (tg *targetgroup.Group, err error) { }() var endpoint = fmt.Sprintf("https://%s:%d/v%d/discover", d.sdConfig.Endpoint, d.sdConfig.Port, d.sdConfig.Version) + if len(d.sdConfig.Groups) > 0 { + groups := url.QueryEscape(strings.Join(d.sdConfig.Groups, ",")) + endpoint = fmt.Sprintf("%s?groups=%s", endpoint, groups) + } + tg = &targetgroup.Group{ Source: endpoint, } @@ -219,6 +229,12 @@ func (d *Discovery) refresh() (tg *targetgroup.Group, err error) { } addr := fmt.Sprintf("%s.%s:%d", container.VMUUID, d.sdConfig.DNSSuffix, d.sdConfig.Port) labels[model.AddressLabel] = model.LabelValue(addr) + + if len(container.Groups) > 0 { + name := "," + strings.Join(container.Groups, ",") + "," + labels[tritonLabelGroups] = model.LabelValue(name) + } + tg.Targets = append(tg.Targets, labels) } diff --git a/discovery/triton/triton_test.go b/discovery/triton/triton_test.go index b7e438ffd..01c4b10a0 100644 --- a/discovery/triton/triton_test.go +++ b/discovery/triton/triton_test.go @@ -55,6 +55,16 @@ var ( CertFile: "shouldnotexist.cert", }, } + groupsconf = SDConfig{ + Account: "testAccount", + DNSSuffix: "triton.example.com", + Endpoint: "127.0.0.1", + Groups: []string{"foo", "bar"}, + Port: 443, + Version: 1, + RefreshInterval: 1, + TLSConfig: config.TLSConfig{InsecureSkipVerify: true}, + } ) func TestTritonSDNew(t *testing.T) { @@ -76,6 +86,20 @@ func TestTritonSDNewBadConfig(t *testing.T) { testutil.Assert(t, td == nil, "") } +func TestTritonSDNewGroupsConfig(t *testing.T) { + td, err := New(nil, &groupsconf) + testutil.Ok(t, err) + testutil.Assert(t, td != nil, "") + testutil.Assert(t, td.client != nil, "") + testutil.Assert(t, td.interval != 0, "") + testutil.Assert(t, td.sdConfig != nil, "") + testutil.Equals(t, groupsconf.Account, td.sdConfig.Account) + testutil.Equals(t, groupsconf.DNSSuffix, td.sdConfig.DNSSuffix) + testutil.Equals(t, groupsconf.Endpoint, td.sdConfig.Endpoint) + testutil.Equals(t, groupsconf.Groups, td.sdConfig.Groups) + testutil.Equals(t, groupsconf.Port, td.sdConfig.Port) +} + func TestTritonSDRun(t *testing.T) { var ( td, err = New(nil, &conf) @@ -112,6 +136,7 @@ func TestTritonSDRefreshMultipleTargets(t *testing.T) { var ( dstr = `{"containers":[ { + "groups":["foo","bar","baz"], "server_uuid":"44454c4c-5000-104d-8037-b7c04f5a5131", "vm_alias":"server01", "vm_brand":"lx", diff --git a/docs/configuration/configuration.md b/docs/configuration/configuration.md index 1c7c6ece5..2802028db 100644 --- a/docs/configuration/configuration.md +++ b/docs/configuration/configuration.md @@ -935,10 +935,12 @@ discovery endpoints. The following meta labels are available on targets during relabeling: -* `__meta_triton_machine_id`: the UUID of the target container +* `__meta_triton_groups`: the list of groups belonging to the target joined by a comma separator * `__meta_triton_machine_alias`: the alias of the target container +* `__meta_triton_machine_brand`: the brand of the target container +* `__meta_triton_machine_id`: the UUID of the target container * `__meta_triton_machine_image`: the target containers image type -* `__meta_triton_machine_server_id`: the server UUID for the target container +* `__meta_triton_server_id`: the server UUID for the target container ```yaml # The information to access the Triton discovery API. @@ -953,6 +955,11 @@ dns_suffix: # often the same value as dns_suffix. endpoint: +# A list of groups for which targets are retrieved. If omitted, all containers +# available to the requesting account are scraped. +groups: + [ - ... ] + # The port to use for discovery and metric scraping. [ port: | default = 9163 ]