forked from linkerd/linkerd2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprometheus.go
265 lines (232 loc) · 8.39 KB
/
prometheus.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package api
import (
"context"
"errors"
"fmt"
"math"
"sort"
"strings"
"time"
"github.com/linkerd/linkerd2/pkg/k8s"
pb "github.com/linkerd/linkerd2/viz/metrics-api/gen/viz"
"github.com/prometheus/common/model"
log "github.com/sirupsen/logrus"
"go.opencensus.io/trace"
)
type promType string
type promResult struct {
prom promType
vec model.Vector
err error
}
const (
promGatewayAlive = promType("QUERY_GATEWAY_ALIVE")
promRequests = promType("QUERY_REQUESTS")
promAllowedRequests = promType("QUERY_ALLOWED_REQUESTS")
promDeniedRequests = promType("QUERY_DENIED_REQUESTS")
promActualRequests = promType("QUERY_ACTUAL_REQUESTS")
promTCPConnections = promType("QUERY_TCP_CONNECTIONS")
promTCPReadBytes = promType("QUERY_TCP_READ_BYTES")
promTCPWriteBytes = promType("QUERY_TCP_WRITE_BYTES")
promLatencyP50 = promType("0.5")
promLatencyP95 = promType("0.95")
promLatencyP99 = promType("0.99")
namespaceLabel = model.LabelName("namespace")
dstNamespaceLabel = model.LabelName("dst_namespace")
gatewayNameLabel = model.LabelName("gateway_name")
gatewayNamespaceLabel = model.LabelName("gateway_namespace")
remoteClusterNameLabel = model.LabelName("target_cluster_name")
authorityLabel = model.LabelName("authority")
serverKindLabel = model.LabelName("srv_kind")
serverNameLabel = model.LabelName("srv_name")
authorizationKindLabel = model.LabelName("authz_kind")
authorizationNameLabel = model.LabelName("authz_name")
routeKindLabel = model.LabelName("route_kind")
routeNameLabel = model.LabelName("route_name")
)
var (
// ErrNoPrometheusInstance is returned when there is no prometheus instance configured
ErrNoPrometheusInstance = errors.New("No prometheus instance to connect")
)
func extractSampleValue(sample *model.Sample) uint64 {
value := uint64(0)
if !math.IsNaN(float64(sample.Value)) {
value = uint64(math.Round(float64(sample.Value)))
}
return value
}
func (s *grpcServer) queryProm(ctx context.Context, query string) (model.Vector, error) {
log.Debugf("Query request: %q", query)
_, span := trace.StartSpan(ctx, "query.prometheus")
defer span.End()
span.AddAttributes(trace.StringAttribute("queryString", query))
if s.prometheusAPI == nil {
return nil, ErrNoPrometheusInstance
}
// single data point (aka summary) query
res, warn, err := s.prometheusAPI.Query(ctx, query, time.Time{})
if err != nil {
return nil, fmt.Errorf("Query failed: %q: %w", query, err)
}
if warn != nil {
log.Warnf("%v", warn)
}
log.Debugf("Query response:\n\t%+v", res)
if res.Type() != model.ValVector {
return nil, fmt.Errorf("Unexpected query result type (expected Vector): %s", res.Type())
}
return res.(model.Vector), nil
}
// add filtering by resource type
// note that metricToKey assumes the label ordering (namespace, name)
func promGroupByLabelNames(resource *pb.Resource) model.LabelNames {
names := model.LabelNames{namespaceLabel}
if resource.Type != k8s.Namespace {
names = append(names, promResourceType(resource))
}
return names
}
// add filtering by resource type
// note that metricToKey assumes the label ordering (namespace, name)
func promDstGroupByLabelNames(resource *pb.Resource) model.LabelNames {
names := model.LabelNames{dstNamespaceLabel}
if isNonK8sResourceQuery(resource.GetType()) {
names = append(names, promResourceType(resource))
} else if resource.Type != k8s.Namespace {
names = append(names, "dst_"+promResourceType(resource))
}
return names
}
// query a named resource
func promQueryLabels(resource *pb.Resource) model.LabelSet {
set := model.LabelSet{}
if resource != nil {
if resource.Name != "" {
if resource.GetType() == k8s.Server {
set[serverKindLabel] = model.LabelValue("server")
set[serverNameLabel] = model.LabelValue(resource.GetName())
} else if resource.GetType() == k8s.ServerAuthorization {
set[authorizationKindLabel] = model.LabelValue("serverauthorization")
set[authorizationNameLabel] = model.LabelValue(resource.GetName())
} else if resource.GetType() == k8s.AuthorizationPolicy {
set[authorizationKindLabel] = model.LabelValue("authorizationpolicy")
set[authorizationNameLabel] = model.LabelValue(resource.GetName())
} else if resource.GetType() == k8s.HTTPRoute {
set[routeNameLabel] = model.LabelValue(resource.GetName())
} else if resource.GetType() != k8s.Service {
set[promResourceType(resource)] = model.LabelValue(resource.Name)
}
}
if shouldAddNamespaceLabel(resource) {
set[namespaceLabel] = model.LabelValue(resource.Namespace)
}
}
return set
}
// query a named resource
func promDstQueryLabels(resource *pb.Resource) model.LabelSet {
set := model.LabelSet{}
if resource.Name != "" {
if isNonK8sResourceQuery(resource.GetType()) {
set[promResourceType(resource)] = model.LabelValue(resource.Name)
} else {
set["dst_"+promResourceType(resource)] = model.LabelValue(resource.Name)
if shouldAddNamespaceLabel(resource) {
set[dstNamespaceLabel] = model.LabelValue(resource.Namespace)
}
}
}
return set
}
// insert a not-nil check into a LabelSet to verify that data for a specified
// label name exists. due to the `!=` this must be inserted as a string. the
// structure of this code is taken from the Prometheus labelset.go library.
func generateLabelStringWithExclusion(l model.LabelSet, labelNames ...string) string {
lstrs := make([]string, 0, len(l))
for l, v := range l {
lstrs = append(lstrs, fmt.Sprintf("%s=%q", l, v))
}
for _, labelName := range labelNames {
lstrs = append(lstrs, fmt.Sprintf(`%s!=""`, labelName))
}
sort.Strings(lstrs)
return fmt.Sprintf("{%s}", strings.Join(lstrs, ", "))
}
// insert a regex-match check into a LabelSet for labels that match the provided
// string. this is modeled on generateLabelStringWithExclusion().
func generateLabelStringWithRegex(l model.LabelSet, labelName string, stringToMatch string) string {
lstrs := make([]string, 0, len(l))
for l, v := range l {
lstrs = append(lstrs, fmt.Sprintf("%s=%q", l, v))
}
lstrs = append(lstrs, fmt.Sprintf(`%s=~"^%s.*"`, labelName, stringToMatch))
sort.Strings(lstrs)
return fmt.Sprintf("{%s}", strings.Join(lstrs, ", "))
}
// generate Prometheus queries for latency quantiles, based on a quantile query
// template, query labels, a time window and grouping.
func generateQuantileQueries(quantileQuery, labels, timeWindow, groupBy string) map[promType]string {
return map[promType]string{
promLatencyP50: fmt.Sprintf(quantileQuery, promLatencyP50, labels, timeWindow, groupBy),
promLatencyP95: fmt.Sprintf(quantileQuery, promLatencyP95, labels, timeWindow, groupBy),
promLatencyP99: fmt.Sprintf(quantileQuery, promLatencyP99, labels, timeWindow, groupBy),
}
}
// determine if we should add "namespace=<namespace>" to a named query
func shouldAddNamespaceLabel(resource *pb.Resource) bool {
return resource.Type != k8s.Namespace && resource.Namespace != ""
}
// query for inbound or outbound requests
func promDirectionLabels(direction string) model.LabelSet {
return model.LabelSet{
model.LabelName("direction"): model.LabelValue(direction),
}
}
func promPeerLabel(peer string) model.LabelSet {
return model.LabelSet{
model.LabelName("peer"): model.LabelValue(peer),
}
}
func promResourceType(resource *pb.Resource) model.LabelName {
l5dLabel := k8s.KindToL5DLabel(resource.Type)
return model.LabelName(l5dLabel)
}
func (s *grpcServer) getPrometheusMetrics(ctx context.Context, requestQueries map[promType]string, latencyQueries map[promType]string) ([]promResult, error) {
resultChan := make(chan promResult)
for pt, query := range requestQueries {
go func(typ promType, promQuery string) {
resultVector, err := s.queryProm(ctx, promQuery)
resultChan <- promResult{
prom: typ,
vec: resultVector,
err: err,
}
}(pt, query)
}
for quantile, query := range latencyQueries {
go func(qt promType, promQuery string) {
resultVector, err := s.queryProm(ctx, promQuery)
resultChan <- promResult{
prom: qt,
vec: resultVector,
err: err,
}
}(quantile, query)
}
// process results, receive one message per prometheus query type
var err error
results := []promResult{}
for i := 0; i < len(latencyQueries)+len(requestQueries); i++ {
result := <-resultChan
if result.err != nil {
log.Errorf("queryProm failed with: %s", result.err)
err = result.err
} else {
results = append(results, result)
}
}
if err != nil {
return nil, err
}
return results, nil
}