/
workload.go
170 lines (146 loc) · 3.79 KB
/
workload.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package models
import (
"context"
"fmt"
"time"
promApi "github.com/prometheus/client_golang/api"
promApiV1 "github.com/prometheus/client_golang/api/prometheus/v1"
promModel "github.com/prometheus/common/model"
)
// WorkloadQuery returns workloads
const workloadQuery = `
sum(
rate(
istio_requests_total {
reporter = "destination",
source_app != "istio-ingressgateway",
source_app != "telemetry",
destination_app != "telemetry",
source_app != "policy",
destination_app != "policy",
source_app != "mixer",
destination_app != "mixer"
}[%s]
)
) by (
source_workload,
destination_workload,
source_app,
destination_app
)
`
// WorkloadStatus struct.
type WorkloadStatus struct {
Time time.Time `json:"date"`
Status string `json:"status"`
}
// Workload struct.
type Workload struct {
Name string `json:"name"` // name of the workload
App string `json:"app,omitempty"` // istio app
Sources []Workload `json:"sources"`
Destinations []Workload `json:"destinations"`
Statuses []AggregatedStatusItem `json:"statuses"`
}
// AddSource adds a source workload
func (w *Workload) AddSource(wi Workload) []Workload {
w.Sources = append(w.Sources, wi)
return w.Sources
}
// AddDestination adds a destination workload
func (w *Workload) AddDestination(wi Workload) []Workload {
w.Destinations = append(w.Destinations, wi)
return w.Destinations
}
// GetWorkloads returns workload with it's destination workloads
func GetWorkloads(addr string) (map[string]Workload, error) {
// Fetch data
matrix, err := fetchQuery(addr, GetWorkloadQuery())
if err != nil {
return nil, err
}
workloads := make(map[string]Workload)
// Add sources with destinations
for _, sample := range matrix {
metric := sample.Metric
// Get workload
var id string
var workload Workload
id, workload = getWorkloadByMetric(metric, "source", workloads)
// Add destination workload
destinationWorkload := Workload{
Name: string(metric["destination_workload"]),
App: string(metric["destination_app"]),
}
workload.AddDestination(destinationWorkload)
workloads[id] = workload
}
// Add destinations with sources
for _, sample := range matrix {
metric := sample.Metric
// Get workload
var id string
var workload Workload
id, workload = getWorkloadByMetric(
metric,
"destination",
workloads,
)
// Add source workload
sourceWorkload := Workload{
Name: string(metric["source_workload"]),
App: string(metric["source_app"]),
}
workload.AddSource(sourceWorkload)
workloads[id] = workload
}
return workloads, nil
}
// GetWorkloadQuery returns a workload query
func GetWorkloadQuery() string {
return fmt.Sprintf(workloadQuery, "60s")
}
func fetchQuery(addr string, pq string) (promModel.Vector, error) {
client, err := promApi.NewClient(promApi.Config{Address: addr})
if err != nil {
return nil, err
}
api := promApiV1.NewAPI(client)
val, err := api.Query(context.Background(), pq, time.Now())
if err != nil {
return nil, err
}
matrix := val.(promModel.Vector)
return matrix, nil
}
func getWorkloadByMetric(
metric promModel.Metric,
sourceType string,
workloads map[string]Workload,
) (
id string,
workload Workload,
) {
// Extract data
var name, app string
if sourceType == "source" {
name = string(metric["source_workload"])
app = string(metric["source_app"])
} else {
name = string(metric["destination_workload"])
app = string(metric["destination_app"])
}
// Find or create workload
id = name + "-" + app
if v, found := workloads[id]; found {
workload = v
} else {
workload = Workload{
Name: name,
App: app,
Sources: make([]Workload, 0),
Destinations: make([]Workload, 0),
}
}
return id, workload
}