Skip to content

Commit 61d5262

Browse files
Use correct machine hostname for Prometheus query (#204)
* Use correct machine name for Prometheus query * Remove logging
1 parent de3990f commit 61d5262

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

handler/prometheus.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"time"
1010

11+
"github.com/m-lab/go/host"
1112
"github.com/m-lab/locate/static"
1213
prom "github.com/prometheus/client_golang/api/prometheus/v1"
1314
"github.com/prometheus/common/model"
@@ -47,9 +48,16 @@ func (c *Client) Prometheus(rw http.ResponseWriter, req *http.Request) {
4748
rw.WriteHeader(http.StatusOK)
4849
}
4950

50-
// UpdatePrometheusForMachine updates the Prometheus signals for a single machine.
51-
func (c *Client) UpdatePrometheusForMachine(ctx context.Context, machine string) error {
52-
err := c.updatePrometheus(ctx, fmt.Sprintf("machine=%s", machine))
51+
// UpdatePrometheusForMachine updates the Prometheus signals for a single machine hostname.
52+
func (c *Client) UpdatePrometheusForMachine(ctx context.Context, hostname string) error {
53+
name, err := host.Parse(hostname)
54+
if err != nil {
55+
log.Printf("Error parsing hostname %s", hostname)
56+
return err
57+
}
58+
59+
machine := name.String()
60+
err = c.updatePrometheus(ctx, fmt.Sprintf("machine=%s", machine))
5361
if err != nil {
5462
log.Printf("Error updating Prometheus signals for machine %s", machine)
5563
}

handler/prometheus_test.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
"testing"
1010
"time"
1111

12+
"github.com/m-lab/go/host"
13+
"github.com/m-lab/go/testingx"
14+
"github.com/m-lab/locate/connection/testdata"
1215
"github.com/m-lab/locate/heartbeat"
1316
"github.com/m-lab/locate/heartbeat/heartbeattest"
1417
prom "github.com/prometheus/client_golang/api/prometheus/v1"
@@ -80,27 +83,39 @@ func TestClient_Prometheus(t *testing.T) {
8083
}
8184

8285
func TestClient_UpdatePrometheusForMachine(t *testing.T) {
86+
hostname, err := host.Parse(testdata.FakeHostname)
87+
testingx.Must(t, err, "failed to parse hostname")
88+
8389
tests := []struct {
84-
name string
85-
machine string
86-
prom PrometheusClient
87-
tracker heartbeat.StatusTracker
88-
wantErr bool
90+
name string
91+
hostname string
92+
prom PrometheusClient
93+
tracker heartbeat.StatusTracker
94+
wantErr bool
8995
}{
9096
{
91-
name: "success",
92-
machine: "fake-machine-name",
97+
name: "success",
98+
hostname: hostname.StringAll(),
9399
prom: &fakePromClient{
94100
queryResult: model.Vector{},
95101
},
96102
tracker: &heartbeattest.FakeStatusTracker{},
97103
wantErr: false,
98104
},
99105
{
100-
name: "error",
101-
machine: "fake-machine-name",
106+
name: "prom-error",
107+
hostname: hostname.StringAll(),
108+
prom: &fakePromClient{
109+
queryErr: formatQuery(e2eQuery, "machine="+hostname.String()),
110+
queryResult: model.Vector{},
111+
},
112+
tracker: &heartbeattest.FakeStatusTracker{},
113+
wantErr: true,
114+
},
115+
{
116+
name: "parse-error",
117+
hostname: "invalid-hostname",
102118
prom: &fakePromClient{
103-
queryErr: formatQuery(e2eQuery, "machine=fake-machine-name"),
104119
queryResult: model.Vector{},
105120
},
106121
tracker: &heartbeattest.FakeStatusTracker{},
@@ -117,7 +132,7 @@ func TestClient_UpdatePrometheusForMachine(t *testing.T) {
117132
PrometheusClient: tt.prom,
118133
}
119134

120-
if err := c.UpdatePrometheusForMachine(context.Background(), tt.machine); (err != nil) != tt.wantErr {
135+
if err := c.UpdatePrometheusForMachine(context.Background(), tt.hostname); (err != nil) != tt.wantErr {
121136
t.Errorf("Client.UpdatePrometheusForMachine() error = %v, wantErr %v", err, tt.wantErr)
122137
}
123138
})

0 commit comments

Comments
 (0)