-
Notifications
You must be signed in to change notification settings - Fork 6
/
cumulative_usage.go
56 lines (46 loc) · 1.62 KB
/
cumulative_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
package fetchers
import (
"context"
"fmt"
"strconv"
"code.cloudfoundry.org/cpu-entitlement-plugin/cf"
"code.cloudfoundry.org/lager/v3"
)
type CumulativeInstanceData struct {
InstanceID int
Usage float64
}
type CumulativeUsageFetcher struct {
logCacheClient LogCacheClient
}
func NewCumulativeUsageFetcher(logCacheClient LogCacheClient) CumulativeUsageFetcher {
return CumulativeUsageFetcher{logCacheClient: logCacheClient}
}
func (f CumulativeUsageFetcher) FetchInstanceData(logger lager.Logger, appGuid string, appInstances map[int]cf.Instance) (map[int]interface{}, error) {
logger = logger.Session("cumulative-usage-fetcher", lager.Data{"app-guid": appGuid})
logger.Info("start")
defer logger.Info("end")
query := fmt.Sprintf(`absolute_usage{source_id="%s"} / absolute_entitlement{source_id="%s"}`, appGuid, appGuid)
promqlResult, err := f.logCacheClient.PromQL(context.Background(), query)
if err != nil {
logger.Error("promql-failed", err, lager.Data{"query": query})
return nil, err
}
instanceUsages := make(map[int]interface{})
for _, sample := range promqlResult.GetVector().GetSamples() {
instanceID, err := strconv.Atoi(sample.GetMetric()["instance_id"])
if err != nil {
logger.Info("ignoring-corrupt-instance-id", lager.Data{"instance-id": sample.GetMetric()["instance_id"]})
continue
}
processInstanceID := sample.GetMetric()["process_instance_id"]
if appInstances[instanceID].ProcessInstanceID != processInstanceID {
continue
}
instanceUsages[instanceID] = CumulativeInstanceData{
InstanceID: instanceID,
Usage: sample.GetPoint().GetValue(),
}
}
return instanceUsages, nil
}