-
Notifications
You must be signed in to change notification settings - Fork 6
/
current_usage.go
106 lines (87 loc) · 2.67 KB
/
current_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package fetchers
import (
"context"
"errors"
"fmt"
"strconv"
"code.cloudfoundry.org/cpu-entitlement-plugin/cf"
"code.cloudfoundry.org/log-cache/pkg/rpc/logcache_v1"
)
type CurrentInstanceData struct {
InstanceID int
Usage float64
}
//go:generate counterfeiter . Fetcher
type Fetcher interface {
FetchInstanceData(appGUID string, appInstances map[int]cf.Instance) (map[int]interface{}, error)
}
type CurrentUsageFetcher struct {
client LogCacheClient
fallbackFetcher Fetcher
}
func NewCurrentUsageFetcher(client LogCacheClient) CurrentUsageFetcher {
return CurrentUsageFetcher{
client: client,
fallbackFetcher: NewCumulativeUsageFetcher(client),
}
}
func NewCurrentUsageFetcherWithFallbackFetcher(client LogCacheClient, fallbackFetcher Fetcher) CurrentUsageFetcher {
return CurrentUsageFetcher{
client: client,
fallbackFetcher: fallbackFetcher,
}
}
func (f CurrentUsageFetcher) FetchInstanceData(appGUID string, appInstances map[int]cf.Instance) (map[int]interface{}, error) {
res, err := f.client.PromQL(
context.Background(),
fmt.Sprintf(`idelta(absolute_usage{source_id="%s"}[1m]) / idelta(absolute_entitlement{source_id="%s"}[1m])`, appGUID, appGUID),
)
if err != nil {
return nil, err
}
currentUsage := parseCurrentUsage(res, appInstances)
if len(currentUsage) == len(appInstances) {
return currentUsage, nil
}
cumulativeResult, err := f.fallbackFetcher.FetchInstanceData(appGUID, appInstances)
if err != nil {
return nil, err
}
for instanceID, _ := range appInstances {
if _, ok := currentUsage[instanceID]; ok {
continue
}
result, ok := cumulativeResult[instanceID]
if !ok {
return nil, fmt.Errorf("could not find historical usage for instance ID %d", instanceID)
}
cumulativeData, ok := result.(CumulativeInstanceData)
if !ok {
return map[int]interface{}{}, errors.New("")
}
currentUsage[instanceID] = CurrentInstanceData{
InstanceID: cumulativeData.InstanceID,
Usage: cumulativeData.Usage,
}
}
return currentUsage, nil
}
func parseCurrentUsage(res *logcache_v1.PromQL_InstantQueryResult, appInstances map[int]cf.Instance) map[int]interface{} {
usagePerInstance := make(map[int]interface{})
for _, sample := range res.GetVector().GetSamples() {
instanceID, err := strconv.Atoi(sample.GetMetric()["instance_id"])
if err != nil {
continue
}
processInstanceID := sample.GetMetric()["process_instance_id"]
if processInstanceID != appInstances[instanceID].ProcessInstanceID {
continue
}
dataPoint := CurrentInstanceData{
InstanceID: instanceID,
Usage: sample.GetPoint().GetValue(),
}
usagePerInstance[instanceID] = dataPoint
}
return usagePerInstance
}