-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
stats_reporter.go
185 lines (165 loc) · 5.68 KB
/
stats_reporter.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
/*
Copyright 2018 Google Inc. All Rights Reserved.
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 activator
import (
"context"
"errors"
"strconv"
"time"
"github.com/knative/pkg/metrics/metricskey"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
)
// Measurement represents the type of the autoscaler metric to be reported
type Measurement int
const (
//RequestCountM is the request count when Activator proxy the request
RequestCountM = iota
// ResponseTimeInMsecM is the response time in millisecond
ResponseTimeInMsecM
)
var (
measurements = []*stats.Float64Measure{
RequestCountM: stats.Float64(
"request_count",
"The number of requests that are routed to Activator",
stats.UnitNone),
ResponseTimeInMsecM: stats.Float64(
"request_latencies",
"The response time in millisecond",
stats.UnitNone),
}
)
// StatsReporter defines the interface for sending activator metrics
type StatsReporter interface {
ReportRequestCount(ns, service, config, rev string, responseCode, numTries int, v float64) error
ReportResponseTime(ns, service, config, rev string, responseCode int, d time.Duration) error
}
// Reporter holds cached metric objects to report autoscaler metrics
type Reporter struct {
initialized bool
namespaceTagKey tag.Key
serviceTagKey tag.Key
configTagKey tag.Key
revisionTagKey tag.Key
responseCodeKey tag.Key
responseCodeClassKey tag.Key
numTriesKey tag.Key
}
// NewStatsReporter creates a reporter that collects and reports activator metrics
func NewStatsReporter() (*Reporter, error) {
var r = &Reporter{}
// Create the tag keys that will be used to add tags to our measurements.
nsTag, err := tag.NewKey(metricskey.LabelNamespaceName)
if err != nil {
return nil, err
}
r.namespaceTagKey = nsTag
serviceTag, err := tag.NewKey(metricskey.LabelServiceName)
if err != nil {
return nil, err
}
r.serviceTagKey = serviceTag
configTag, err := tag.NewKey(metricskey.LabelConfigurationName)
if err != nil {
return nil, err
}
r.configTagKey = configTag
revTag, err := tag.NewKey(metricskey.LabelRevisionName)
if err != nil {
return nil, err
}
r.revisionTagKey = revTag
responseCodeTag, err := tag.NewKey("response_code")
if err != nil {
return nil, err
}
r.responseCodeKey = responseCodeTag
responseCodeClassTag, err := tag.NewKey("response_code_class")
if err != nil {
return nil, err
}
r.responseCodeClassKey = responseCodeClassTag
numTriesTag, err := tag.NewKey("num_tries")
if err != nil {
return nil, err
}
r.numTriesKey = numTriesTag
// Create view to see our measurements.
err = view.Register(
&view.View{
Description: "The number of requests that are routed to Activator",
Measure: measurements[RequestCountM],
Aggregation: view.Sum(),
TagKeys: []tag.Key{r.namespaceTagKey, r.serviceTagKey, r.configTagKey, r.revisionTagKey, r.responseCodeKey, r.responseCodeClassKey, r.numTriesKey},
},
&view.View{
Description: "The response time in millisecond",
Measure: measurements[ResponseTimeInMsecM],
Aggregation: view.Distribution(1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 11000, 12000, 13000, 14000, 15000),
TagKeys: []tag.Key{r.namespaceTagKey, r.serviceTagKey, r.configTagKey, r.revisionTagKey, r.responseCodeClassKey, r.responseCodeKey},
},
)
if err != nil {
return nil, err
}
r.initialized = true
return r, nil
}
// ReportRequestCount captures request count metric with value v.
func (r *Reporter) ReportRequestCount(ns, service, config, rev string, responseCode, numTries int, v float64) error {
if !r.initialized {
return errors.New("StatsReporter is not initialized yet")
}
ctx, err := tag.New(
context.Background(),
tag.Insert(r.namespaceTagKey, ns),
tag.Insert(r.serviceTagKey, service),
tag.Insert(r.configTagKey, config),
tag.Insert(r.revisionTagKey, rev),
tag.Insert(r.responseCodeKey, strconv.Itoa(responseCode)),
tag.Insert(r.responseCodeClassKey, getResponseCodeClass(responseCode)),
tag.Insert(r.numTriesKey, strconv.Itoa(numTries)))
if err != nil {
return err
}
stats.Record(ctx, measurements[RequestCountM].M(v))
return nil
}
// ReportResponseTime captures response time requests
func (r *Reporter) ReportResponseTime(ns, service, config, rev string, responseCode int, d time.Duration) error {
if !r.initialized {
return errors.New("StatsReporter is not initialized yet")
}
ctx, err := tag.New(
context.Background(),
tag.Insert(r.namespaceTagKey, ns),
tag.Insert(r.serviceTagKey, service),
tag.Insert(r.configTagKey, config),
tag.Insert(r.revisionTagKey, rev),
tag.Insert(r.responseCodeKey, strconv.Itoa(responseCode)),
tag.Insert(r.responseCodeClassKey, getResponseCodeClass(responseCode)))
if err != nil {
return err
}
// convert time.Duration in nanoseconds to milliseconds
stats.Record(ctx, measurements[ResponseTimeInMsecM].M(float64(d/time.Millisecond)))
return nil
}
// getResponseCodeClass converts response code to a string of response code class.
// e.g. The response code class is "5xx" for response code 503.
func getResponseCodeClass(responseCode int) string {
// Get the hundred digit of the response code and concatenate "xx"
return strconv.Itoa((responseCode/100)%10) + "xx"
}