forked from knative/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord.go
67 lines (56 loc) · 2.09 KB
/
record.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
/*
Copyright 2019 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package metrics
import (
"context"
"path"
"go.opencensus.io/stats"
"knative.dev/pkg/metrics/metricskey"
)
// Record decides whether to record one measurement via OpenCensus based on the
// following conditions:
// 1) No package level metrics config. In this case it just proxies to OpenCensus
// based on the assumption that users expect the metrics to be recorded when
// they call this function. Users must ensure metrics config are set before
// using this function to get expected behavior.
// 2) The backend is not Stackdriver.
// 3) The backend is Stackdriver and it is allowed to use custom metrics.
// 4) The backend is Stackdriver and the metric is "knative_revison" built-in metric.
func Record(ctx context.Context, ms stats.Measurement) {
mc := getCurMetricsConfig()
// Condition 1)
if mc == nil {
stats.Record(ctx, ms)
return
}
// Condition 2) and 3)
if !mc.isStackdriverBackend || mc.allowStackdriverCustomMetrics {
stats.Record(ctx, ms)
return
}
// Condition 4)
metricType := path.Join(mc.stackdriverMetricTypePrefix, ms.Measure().Name())
if metricskey.KnativeRevisionMetrics.Has(metricType) {
stats.Record(ctx, ms)
}
}
// Buckets125 generates an array of buckets with approximate powers-of-two
// buckets that also aligns with powers of 10 on every 3rd step. This can
// be used to create a view.Distribution.
func Buckets125(low, high float64) []float64 {
buckets := []float64{low}
for last := low; last < high; last = last * 10 {
buckets = append(buckets, 2*last, 5*last, 10*last)
}
return buckets
}