-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware.go
219 lines (182 loc) · 5.31 KB
/
middleware.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
// NEGRONI-CLOUDWATCH: Middleware for recording data to AWS Cloudwatch.
// Copyright (C) 2016, Colin Steele
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package negronicloudwatch
import (
"fmt"
"net/http"
"net/url"
"time"
"github.com/codegangsta/negroni"
"github.com/gorilla/context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
cw "github.com/aws/aws-sdk-go/service/cloudwatch"
)
type key int
const PutMetric key = 0
// Middleware handler
type Middleware struct {
// CW Service
Service *cw.CloudWatch
// CW namespace
Namespace string
// Latency metric name
LatencyMetricName string
Before BeforeFunc
After AfterFunc
PutMetric func(data []*cw.MetricDatum)
clock timer
// Exclude URLs from logging
excludeURLs map[string]struct{}
}
func New(region, namespace string) *Middleware {
m := Middleware{
Service: cw.New(session.New(), aws.NewConfig().WithRegion(region).WithMaxRetries(5)),
Namespace: namespace,
LatencyMetricName: "Latency",
Before: DefaultBefore,
After: DefaultAfter,
clock: &realClock{},
excludeURLs: map[string]struct{}{},
}
m.PutMetric = func(data []*cw.MetricDatum) {
putMetric(&m, data)
}
return &m
}
//
//
//
// []*cw.MetricDatum{
// {
// MetricName: aws.String("MetricName"),
// Dimensions: []*cw.Dimension{
// {
// Name: aws.String("DimensionName"),
// Value: aws.String("DimensionValue"),
// },
// },
// StatisticValues: &cw.StatisticSet{
// Maximum: aws.Float64(1.0),
// Minimum: aws.Float64(1.0),
// SampleCount: aws.Float64(1.0),
// Sum: aws.Float64(1.0),
// },
// Timestamp: aws.Time(time.Now()),
// Unit: aws.String("StandardUnit"),
// Value: aws.Float64(1.0),
// },
// }
func putMetric(m *Middleware, data []*cw.MetricDatum) {
params := &cw.PutMetricDataInput{
MetricData: data,
Namespace: aws.String(m.Namespace),
}
_, err := m.Service.PutMetricData(params)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
fmt.Println(awsErr.Code())
// if "NoSuchBucket" == awsErr.Code() {
// return *resp
// }
} else {
fmt.Println(err.Error())
}
return
}
}
type timer interface {
Now() time.Time
Since(time.Time) time.Duration
}
type realClock struct{}
func (rc *realClock) Now() time.Time {
return time.Now()
}
func (rc *realClock) Since(t time.Time) time.Duration {
return time.Since(t)
}
// ExcludeURL adds a new URL u to be ignored during logging. The URL u is parsed, hence the returned error
func (m *Middleware) ExcludeURL(u string) error {
if _, err := url.Parse(u); err != nil {
return err
}
m.excludeURLs[u] = struct{}{}
return nil
}
// ExcludedURLs returns the list of excluded URLs for this middleware
func (m *Middleware) ExcludedURLs() []string {
urls := []string{}
for k, _ := range m.excludeURLs {
urls = append(urls, k)
}
return urls
}
func (m *Middleware) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
if m.Before == nil {
m.Before = DefaultBefore
}
if m.After == nil {
m.After = DefaultAfter
}
if _, ok := m.excludeURLs[r.URL.Path]; ok {
return
}
start := m.clock.Now()
// Try to get the real IP
remoteAddr := r.RemoteAddr
if realIP := r.Header.Get("X-Real-IP"); realIP != "" {
remoteAddr = realIP
}
m.Before(m, r, remoteAddr)
next(rw, r)
latency := m.clock.Since(start)
res := rw.(negroni.ResponseWriter)
m.After(m, r, res, latency, remoteAddr)
}
// Called before handler
type BeforeFunc func(*Middleware, *http.Request, string)
// AfterFunc is the func type called after calling the next func in
// the middleware chain
type AfterFunc func(*Middleware, *http.Request, negroni.ResponseWriter, time.Duration, string)
// DefaultBefore is the default func assigned to *Middleware.Before
func DefaultBefore(m *Middleware, req *http.Request, remoteAddr string) {
context.Set(req, PutMetric, m.PutMetric)
}
// DefaultAfter is the default func assigned to *Middleware.After
func DefaultAfter(m *Middleware, req *http.Request, res negroni.ResponseWriter, latency time.Duration, remoteAddr string) {
ms := float64(latency.Nanoseconds() * 1000)
m.PutMetric([]*cw.MetricDatum{
{
MetricName: aws.String(m.LatencyMetricName),
Dimensions: []*cw.Dimension{
{
Name: aws.String("RequestURI"),
Value: aws.String(req.RequestURI),
},
{
Name: aws.String("RemoteAddr"),
Value: aws.String(remoteAddr),
},
},
Timestamp: aws.Time(time.Now()),
Unit: aws.String("Microseconds"),
Value: aws.Float64(ms),
},
})
context.Clear(req)
}