Skip to content

Commit

Permalink
List support for known servers and script-exporter targets (#49)
Browse files Browse the repository at this point in the history
* List known servers
* Add formats for servers and script-exporter
* Add a service label for byos configs
  • Loading branch information
stephen-soltesz authored Oct 22, 2024
1 parent dd47e34 commit 1ad75c4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 15 deletions.
1 change: 1 addition & 0 deletions api/v0/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type DeleteResponse struct {
type ListResponse struct {
Error *v2.Error `json:",omitempty"`
StaticConfig []discovery.StaticConfig `json:",omitempty"`
Servers []string `json:",omitempty"`
}

// Network contains IPv4 and IPv6 addresses.
Expand Down
46 changes: 35 additions & 11 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,32 +347,56 @@ func (s *Server) List(rw http.ResponseWriter, req *http.Request) {
return
}

format := req.URL.Query().Get("format")

// Create a prometheus StaticConfig for each known host.
for i := range hosts {
h, err := host.Parse(hosts[i])
if err != nil {
continue
}
if format == "script-exporter" {
// NOTE: do not assign any ports for script exporter.
ports[i] = []string{""}
} else {
// Convert port strings to ":<port>".
p := []string{}
for j := range ports[i] {
p = append(p, ":"+ports[i][j])
}
ports[i] = p
}
for _, port := range ports[i] {
labels := map[string]string{
"machine": hosts[i],
"type": "virtual",
"deployment": "byos",
"managed": "none",
"org": h.Org,
}
if req.URL.Query().Get("service") != "" {
labels["service"] = req.URL.Query().Get("service")
}
// We create one record per host to add a unique "machine" label to each one.
configs = append(configs, discovery.StaticConfig{
Targets: []string{hosts[i] + ":" + port},
Labels: map[string]string{
"machine": hosts[i],
"type": "virtual",
"deployment": "byos",
"managed": "none",
"org": h.Org,
},
Targets: []string{hosts[i] + port},
Labels: labels,
})
}
}

var results interface{}
format := req.URL.Query().Get("format")
if format == "prometheus" {
switch format {
case "script-exporter":
fallthrough
case "blackbox":
fallthrough
case "prometheus": // TODO(soltesz): retire this name.
results = configs
} else {
case "servers":
resp.Servers = hosts
results = resp
default:
// NOTE: default format is not valid for prometheus StaticConfig format.
resp.StaticConfig = configs
results = resp
Expand Down
35 changes: 31 additions & 4 deletions handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,11 +576,31 @@ func TestServer_List(t *testing.T) {
params: "?format=prometheus",
lister: &fakeStatusTracker{
nodes: []string{"test1"},
ports: [][]string{[]string{}},
ports: [][]string{{}},
},
wantCode: http.StatusOK,
wantLength: 0,
},
{
name: "success-servers",
params: "?format=servers",
lister: &fakeStatusTracker{
nodes: []string{"ndt-lga3356-040e9f4b.mlab.autojoin.measurement-lab.org"},
ports: [][]string{{"9990"}},
},
wantCode: http.StatusOK,
wantLength: 1,
},
{
name: "success-script-exporter",
params: "?format=script-exporter&service=ndt7_client_byos",
lister: &fakeStatusTracker{
nodes: []string{"ndt-lga3356-040e9f4b.mlab.autojoin.measurement-lab.org"},
ports: [][]string{{"9990"}},
},
wantCode: http.StatusOK,
wantLength: 1,
},
{
name: "error-internal",
params: "",
Expand All @@ -606,17 +626,24 @@ func TestServer_List(t *testing.T) {
var err error
raw := rw.Body.Bytes()
configs := []discovery.StaticConfig{}
if strings.Contains(tt.params, "prometheus") {
length := 0
if strings.Contains(tt.params, "prometheus") || strings.Contains(tt.params, "script-exporter") {
err = json.Unmarshal(raw, &configs)
length = len(configs)
} else if strings.Contains(tt.params, "servers") {
resp := v0.ListResponse{}
err = json.Unmarshal(raw, &resp)
length = len(resp.Servers)
} else {
resp := v0.ListResponse{}
err = json.Unmarshal(raw, &resp)
configs = resp.StaticConfig
length = len(configs)
}
testingx.Must(t, err, "failed to unmarshal response")

if len(configs) != tt.wantLength {
t.Errorf("List() returned wrong length; got %d, want %d", len(configs), tt.wantLength)
if length != tt.wantLength {
t.Errorf("List() returned wrong length; got %d, want %d", length, tt.wantLength)
}
})
}
Expand Down

0 comments on commit 1ad75c4

Please sign in to comment.