forked from distribution/distribution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_metrics.go
194 lines (161 loc) · 4.47 KB
/
http_metrics.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
package gorelic
import (
metrics "github.com/yvasiyarov/go-metrics"
"github.com/yvasiyarov/newrelic_platform_go"
"net/http"
"time"
)
type tHTTPHandlerFunc func(http.ResponseWriter, *http.Request)
type tHTTPHandler struct {
originalHandler http.Handler
originalHandlerFunc tHTTPHandlerFunc
isFunc bool
timer metrics.Timer
}
var httpTimer metrics.Timer
func newHTTPHandlerFunc(h tHTTPHandlerFunc) *tHTTPHandler {
return &tHTTPHandler{
isFunc: true,
originalHandlerFunc: h,
}
}
func newHTTPHandler(h http.Handler) *tHTTPHandler {
return &tHTTPHandler{
isFunc: false,
originalHandler: h,
}
}
func (handler *tHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
startTime := time.Now()
defer handler.timer.UpdateSince(startTime)
if handler.isFunc {
handler.originalHandlerFunc(w, req)
} else {
handler.originalHandler.ServeHTTP(w, req)
}
}
type baseTimerMetrica struct {
dataSource metrics.Timer
name string
units string
}
func (metrica *baseTimerMetrica) GetName() string {
return metrica.name
}
func (metrica *baseTimerMetrica) GetUnits() string {
return metrica.units
}
type timerRate1Metrica struct {
*baseTimerMetrica
}
func (metrica *timerRate1Metrica) GetValue() (float64, error) {
return metrica.dataSource.Rate1(), nil
}
type timerRateMeanMetrica struct {
*baseTimerMetrica
}
func (metrica *timerRateMeanMetrica) GetValue() (float64, error) {
return metrica.dataSource.RateMean(), nil
}
type timerMeanMetrica struct {
*baseTimerMetrica
}
func (metrica *timerMeanMetrica) GetValue() (float64, error) {
return metrica.dataSource.Mean() / float64(time.Millisecond), nil
}
type timerMinMetrica struct {
*baseTimerMetrica
}
func (metrica *timerMinMetrica) GetValue() (float64, error) {
return float64(metrica.dataSource.Min()) / float64(time.Millisecond), nil
}
type timerMaxMetrica struct {
*baseTimerMetrica
}
func (metrica *timerMaxMetrica) GetValue() (float64, error) {
return float64(metrica.dataSource.Max()) / float64(time.Millisecond), nil
}
type timerPercentile75Metrica struct {
*baseTimerMetrica
}
func (metrica *timerPercentile75Metrica) GetValue() (float64, error) {
return metrica.dataSource.Percentile(0.75) / float64(time.Millisecond), nil
}
type timerPercentile90Metrica struct {
*baseTimerMetrica
}
func (metrica *timerPercentile90Metrica) GetValue() (float64, error) {
return metrica.dataSource.Percentile(0.90) / float64(time.Millisecond), nil
}
type timerPercentile95Metrica struct {
*baseTimerMetrica
}
func (metrica *timerPercentile95Metrica) GetValue() (float64, error) {
return metrica.dataSource.Percentile(0.95) / float64(time.Millisecond), nil
}
func addHTTPMericsToComponent(component newrelic_platform_go.IComponent, timer metrics.Timer) {
rate1 := &timerRate1Metrica{
baseTimerMetrica: &baseTimerMetrica{
name: "http/throughput/1minute",
units: "rps",
dataSource: timer,
},
}
component.AddMetrica(rate1)
rateMean := &timerRateMeanMetrica{
baseTimerMetrica: &baseTimerMetrica{
name: "http/throughput/rateMean",
units: "rps",
dataSource: timer,
},
}
component.AddMetrica(rateMean)
responseTimeMean := &timerMeanMetrica{
baseTimerMetrica: &baseTimerMetrica{
name: "http/responseTime/mean",
units: "ms",
dataSource: timer,
},
}
component.AddMetrica(responseTimeMean)
responseTimeMax := &timerMaxMetrica{
baseTimerMetrica: &baseTimerMetrica{
name: "http/responseTime/max",
units: "ms",
dataSource: timer,
},
}
component.AddMetrica(responseTimeMax)
responseTimeMin := &timerMinMetrica{
baseTimerMetrica: &baseTimerMetrica{
name: "http/responseTime/min",
units: "ms",
dataSource: timer,
},
}
component.AddMetrica(responseTimeMin)
responseTimePercentile75 := &timerPercentile75Metrica{
baseTimerMetrica: &baseTimerMetrica{
name: "http/responseTime/percentile75",
units: "ms",
dataSource: timer,
},
}
component.AddMetrica(responseTimePercentile75)
responseTimePercentile90 := &timerPercentile90Metrica{
baseTimerMetrica: &baseTimerMetrica{
name: "http/responseTime/percentile90",
units: "ms",
dataSource: timer,
},
}
component.AddMetrica(responseTimePercentile90)
responseTimePercentile95 := &timerPercentile95Metrica{
baseTimerMetrica: &baseTimerMetrica{
name: "http/responseTime/percentile95",
units: "ms",
dataSource: timer,
},
}
component.AddMetrica(responseTimePercentile95)
}