Skip to content

Commit 64d9d22

Browse files
Add metric counter for Heartbeat requests (#77)
1 parent 7a874ce commit 64d9d22

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

handler/handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (c *Client) Nearest(rw http.ResponseWriter, req *http.Request) {
161161
status := http.StatusServiceUnavailable
162162
result.Error = v2.NewError("nearest", "Failed to lookup nearest machines", status)
163163
writeResult(rw, result.Error.Status, &result)
164-
metrics.RequestsTotal.WithLabelValues(http.StatusText(result.Error.Status)).Inc()
164+
metrics.RequestsTotal.WithLabelValues("nearest", http.StatusText(result.Error.Status)).Inc()
165165
return
166166
}
167167

@@ -171,7 +171,7 @@ func (c *Client) Nearest(rw http.ResponseWriter, req *http.Request) {
171171
if errLat != nil || errLon != nil {
172172
result.Error = v2.NewError("client", errFailedToLookupClient.Error(), http.StatusInternalServerError)
173173
writeResult(rw, result.Error.Status, &result)
174-
metrics.RequestsTotal.WithLabelValues(http.StatusText(result.Error.Status)).Inc()
174+
metrics.RequestsTotal.WithLabelValues("nearest", http.StatusText(result.Error.Status)).Inc()
175175
return
176176
}
177177

@@ -181,15 +181,15 @@ func (c *Client) Nearest(rw http.ResponseWriter, req *http.Request) {
181181
if err != nil {
182182
result.Error = v2.NewError("nearest", "Failed to lookup nearest machines", http.StatusInternalServerError)
183183
writeResult(rw, result.Error.Status, &result)
184-
metrics.RequestsTotal.WithLabelValues(http.StatusText(result.Error.Status)).Inc()
184+
metrics.RequestsTotal.WithLabelValues("nearest", http.StatusText(result.Error.Status)).Inc()
185185
return
186186
}
187187

188188
// Populate target URLs and write out response.
189189
c.populateURLs(targets, urls, experiment, req.Form)
190190
result.Results = targets
191191
writeResult(rw, http.StatusOK, &result)
192-
metrics.RequestsTotal.WithLabelValues(http.StatusText(http.StatusOK)).Inc()
192+
metrics.RequestsTotal.WithLabelValues("nearest", http.StatusText(http.StatusOK)).Inc()
193193
}
194194

195195
// checkClientLocation looks up the client location and copies the location

handler/heartbeat.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ func (c *Client) Heartbeat(rw http.ResponseWriter, req *http.Request) {
2525
ws, err := upgrader.Upgrade(rw, req, nil)
2626
if err != nil {
2727
log.Errorf("failed to establish a connection: %v", err)
28+
metrics.RequestsTotal.WithLabelValues("heartbeat", err.Error()).Inc()
2829
return
2930
}
3031
metrics.CurrentHeartbeatConnections.Inc()
32+
metrics.RequestsTotal.WithLabelValues("heartbeat", "OK").Inc()
3133
go c.handleHeartbeats(ws)
3234
}
3335

metrics/metrics.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ import (
66
)
77

88
var (
9-
// RequestsTotal counts the number of 'nearest' requests served by
9+
// RequestsTotal counts the number of requests served by
1010
// the Locate service.
1111
//
1212
// Example usage:
13-
// metrics.RequestsTotal.WithLabelValues("200").Inc()
13+
// metrics.RequestsTotal.WithLabelValues("nearest", "200").Inc()
1414
RequestsTotal = promauto.NewCounterVec(
1515
prometheus.CounterOpts{
1616
Name: "locate_requests_total",
17-
Help: "Number of 'nearest' requests served by the Locate service.",
17+
Help: "Number of requests served by the Locate service.",
1818
},
19-
[]string{"status"},
19+
[]string{"type", "status"},
2020
)
2121

2222
// AppEngineTotal counts the number of times App Engine headers are

metrics/metrics_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
func TestLintMetrics(t *testing.T) {
10-
RequestsTotal.WithLabelValues("status")
10+
RequestsTotal.WithLabelValues("type", "status")
1111
AppEngineTotal.WithLabelValues("country")
1212
CurrentHeartbeatConnections.Set(0)
1313
promtest.LintMetrics(nil)

0 commit comments

Comments
 (0)