/
usage.go
82 lines (77 loc) · 2.89 KB
/
usage.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package gcp
import (
"context"
"fmt"
"time"
monitoring "cloud.google.com/go/monitoring/apiv3/v2"
"cloud.google.com/go/monitoring/apiv3/v2/monitoringpb"
googlepb "github.com/golang/protobuf/ptypes/timestamp"
"github.com/pkg/errors"
"google.golang.org/api/iterator"
"google.golang.org/genproto/googleapis/api/metric"
)
// loadUsage loads the usage from `consumer_quota` metric type for the project. It pulls metric data for last hour and
// and the uses the latest data point as the usage value for the specific metric type.
func loadUsage(ctx context.Context, client *monitoring.MetricClient, project string) ([]record, error) {
req := &monitoringpb.ListTimeSeriesRequest{
Name: fmt.Sprintf("projects/%s", project),
Filter: `metric.type = "serviceruntime.googleapis.com/quota/allocation/usage" AND resource.type = "consumer_quota"`,
Interval: &monitoringpb.TimeInterval{
EndTime: &googlepb.Timestamp{
Seconds: time.Now().Add(-5 * time.Minute).Unix(),
},
StartTime: &googlepb.Timestamp{
Seconds: time.Now().Add(-1 * time.Hour).Unix(),
},
},
}
var usages []record
it := client.ListTimeSeries(ctx, req)
for {
resp, err := it.Next()
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
return usages, errors.Wrap(err, "failed to list quota/allocation/usage timeseries")
}
usage, err := latestRecord(resp)
if err != nil {
return usages, errors.Wrap(err, "failed to load usage from timeseries")
}
usages = append(usages, usage)
}
return usages, nil
}
// latestRecord find the latest data point for the timeseries and returns that as the usage for the metric type.
// Based on https://cloud.google.com/monitoring/api/ref_v3/rest/v3/TimeSeries and definition of the points API
// "The data points of this time series. When listing time series, points are returned in reverse time order.",
// The latestRecord returns the first element of the points as the usage value. In case the points list is empty it
// returns 0 as the usage value.
func latestRecord(ts *monitoringpb.TimeSeries) (record, error) {
service, ok := ts.GetResource().GetLabels()["service"]
if !ok {
return record{}, errors.Errorf("serivce not found for timeseries, actual label %s", ts.GetResource().GetLabels())
}
name, ok := ts.GetMetric().Labels["quota_metric"]
if !ok {
return record{}, errors.New("no name found for timeseries")
}
location, ok := ts.GetResource().GetLabels()["location"]
if !ok {
return record{}, errors.Errorf("location not found for timeseries, actual label %s", ts.GetResource().GetLabels())
}
if ts.GetValueType() != metric.MetricDescriptor_INT64 {
return record{}, errors.Errorf("invalid value type for timeseries, was %s", ts.GetValueType())
}
value := int64(0)
if points := ts.GetPoints(); len(points) > 0 {
value = points[0].GetValue().GetInt64Value()
}
return record{
Service: service,
Name: name,
Location: location,
Value: value,
}, nil
}