-
Notifications
You must be signed in to change notification settings - Fork 64
/
http_metrics.go
142 lines (122 loc) · 3.61 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
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
httpStatusCounters map[int]metrics.Counter
}
//A wrapper over a http.ResponseWriter object
type responseWriterWrapper struct {
originalWriter http.ResponseWriter
statusCode int
}
var httpTimer metrics.Timer
func (reponseWriterWrapper *responseWriterWrapper) Header() http.Header {
return reponseWriterWrapper.originalWriter.Header()
}
func (reponseWriterWrapper *responseWriterWrapper) Write(bytes []byte) (int, error) {
return reponseWriterWrapper.originalWriter.Write(bytes)
}
func (reponseWriterWrapper *responseWriterWrapper) WriteHeader(code int) {
reponseWriterWrapper.originalWriter.WriteHeader(code)
reponseWriterWrapper.statusCode = code
}
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)
responseWriterWrapper := responseWriterWrapper{originalWriter: w, statusCode: http.StatusOK}
if handler.isFunc {
handler.originalHandlerFunc(&responseWriterWrapper, req)
} else {
handler.originalHandler.ServeHTTP(&responseWriterWrapper, req)
}
if statusCounter := handler.httpStatusCounters[responseWriterWrapper.statusCode]; statusCounter != nil {
statusCounter.Inc(1)
}
}
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)
}