-
Notifications
You must be signed in to change notification settings - Fork 432
/
metrics.go
28 lines (26 loc) · 1.12 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package helpers
import (
errors "github.com/rotisserie/eris"
"go.opencensus.io/stats/view"
)
// ReadMetricByLabel looks up the specified metricName and returns the latest data
// recorded for the time series with the specified label key/value pair.
//
// If the metric has not yet been registered, this function will fail. If the metric
// has been registered, but there is not yet any time series data recorded with the label
// key/value provided, then an error is returned. The error response allows tests to distinguish
// "the metric was never recorded" from "a value of 0 was recorded"
func ReadMetricByLabel(metricName string, labelKey string, labelValue string) (int, error) {
rows, err := view.RetrieveData(metricName)
if err != nil {
return 0, errors.Wrapf(err, "failed to retrieve data for %s", metricName)
}
for _, row := range rows {
for _, tag := range row.Tags {
if tag.Key.Name() == labelKey && tag.Value == labelValue {
return int(row.Data.(*view.LastValueData).Value), nil
}
}
}
return 0, errors.Errorf("%s does not have any time series with label (key=%s,value=%s)", metricName, labelKey, labelValue)
}