-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
261 lines (231 loc) · 6.61 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
*
* 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.
*
* @author Aeneas Rekkas <aeneas+oss@aeneas.io>
* @copyright 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
* @license Apache-2.0
*/
package metricsx
import (
"crypto/sha256"
"encoding/hex"
"net"
"net/http"
"net/url"
"runtime"
"strings"
"sync"
"time"
"io"
"math/rand"
"github.com/pborman/uuid"
"github.com/segmentio/analytics-go"
"github.com/sirupsen/logrus"
"github.com/urfave/negroni"
)
type MetricsManager struct {
sync.RWMutex `json:"-"`
start time.Time `json:"-"`
Segment analytics.Client `json:"-"`
Logger logrus.FieldLogger `json:"-"`
shouldCommit bool `json:"-"`
salt string
whitelistedURLs []string `json:"-"`
sampling float64 `json:"-"`
rng io.Reader `json:"-"`
ID string `json:"id"`
UpTime int64 `json:"uptime"`
MemoryStatistics *MemoryStatistics `json:"memory"`
BuildVersion string `json:"buildVersion"`
BuildHash string `json:"buildHash"`
BuildTime string `json:"buildTime"`
InstanceID string `json:"instanceId"`
ServiceName string `json:"serviceName"`
}
func Hash(value string) string {
hash := sha256.New()
hash.Write([]byte(value))
return hex.EncodeToString(hash.Sum(nil))
}
func NewMetricsManager(
id string,
enable bool,
writeKey string,
whitelistedURLs []string,
logger logrus.FieldLogger,
serviceName string,
sampling float64,
endpoint string,
) *MetricsManager {
segment, err := analytics.NewWithConfig(writeKey, analytics.Config{
Interval: time.Hour * 24,
BatchSize: 100,
})
if err != nil {
logger.WithError(err).Fatalf("Unable to initialise segment.")
return nil
}
mm := &MetricsManager{
InstanceID: uuid.New(),
Segment: segment,
Logger: logger,
MemoryStatistics: &MemoryStatistics{},
ID: id,
start: time.Now().UTC(),
salt: uuid.New(),
shouldCommit: enable,
whitelistedURLs: whitelistedURLs,
ServiceName: serviceName,
sampling: sampling,
}
return mm
}
func NewMetricsManagerWithConfig(
id string,
enable bool,
writeKey string,
whitelistedURLs []string,
logger logrus.FieldLogger,
serviceName string,
sampling float64,
config analytics.Config,
) *MetricsManager {
segment, err := analytics.NewWithConfig(writeKey, config)
if err != nil {
logger.WithError(err).Fatalf("Unable to initialise segment.")
return nil
}
mm := &MetricsManager{
InstanceID: uuid.New(),
Segment: segment,
Logger: logger,
MemoryStatistics: &MemoryStatistics{},
ID: id,
start: time.Now().UTC(),
salt: uuid.New(),
shouldCommit: enable,
whitelistedURLs: whitelistedURLs,
ServiceName: serviceName,
sampling: sampling,
}
return mm
}
func (sw *MetricsManager) RegisterSegment(version, hash, buildTime string) {
sw.Lock()
defer sw.Unlock()
if !sw.shouldCommit {
sw.Logger.Info("Detected local environment, skipping telemetry commit")
return
}
if err := Retry(sw.Logger, time.Minute*5, time.Hour, func() error {
return sw.Segment.Enqueue(analytics.Identify{
UserId: sw.ID,
Traits: analytics.NewTraits().
Set("goarch", runtime.GOARCH).
Set("goos", runtime.GOOS).
Set("numCpu", runtime.NumCPU()).
Set("runtimeVersion", runtime.Version()).
Set("version", version).
Set("Hash", hash).
Set("buildTime", buildTime).
Set("service", sw.ServiceName).
Set("instanceId", sw.InstanceID),
Context: &analytics.Context{
IP: net.IPv4(0, 0, 0, 0),
},
})
}); err != nil {
sw.Logger.WithError(err).Debug("Could not commit anonymized environment information")
}
sw.Logger.Debug("Transmitted anonymized environment information")
}
func (sw *MetricsManager) CommitMemoryStatistics() {
if !sw.shouldCommit {
sw.Logger.Info("Detected local environment, skipping telemetry commit")
return
}
for {
sw.MemoryStatistics.Update()
if err := sw.Segment.Enqueue(analytics.Track{
UserId: sw.ID,
Event: "memstats",
Properties: analytics.Properties(sw.MemoryStatistics.ToMap()),
Context: &analytics.Context{IP: net.IPv4(0, 0, 0, 0)},
}); err != nil {
sw.Logger.WithError(err).Debug("Could not commit anonymized telemetry data")
} else {
sw.Logger.Debug("Telemetry data transmitted")
}
time.Sleep(time.Hour * 24)
}
}
func (sw *MetricsManager) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
start := time.Now()
next(rw, r)
if !sw.shouldCommit || rand.Float64() > sw.sampling {
return
}
latency := time.Now().UTC().Sub(start.UTC()) / time.Millisecond
scheme := "https:"
if r.TLS == nil {
scheme = "http:"
}
path := sw.anonymizePath(r.URL.Path, sw.salt)
query := sw.anonymizeQuery(r.URL.Query(), sw.salt)
// Collecting request info
res := rw.(negroni.ResponseWriter)
status := res.Status()
size := res.Size()
sw.Segment.Enqueue(analytics.Page{
UserId: sw.ID,
Name: path,
Properties: analytics.
NewProperties().
SetURL(scheme+"//"+sw.ID+path+"?"+query).
SetPath(path).
SetName(path).
Set("status", status).
Set("size", size).
Set("latency", latency).
Set("instance", sw.InstanceID).
Set("service", sw.ServiceName).
Set("method", r.Method),
Context: &analytics.Context{IP: net.IPv4(0, 0, 0, 0)},
})
}
func (sw *MetricsManager) anonymizePath(path string, salt string) string {
paths := sw.whitelistedURLs
path = strings.ToLower(path)
for _, p := range paths {
p = strings.ToLower(p)
if len(path) == len(p) && path[:len(p)] == strings.ToLower(p) {
return p
} else if len(path) > len(p) && path[:len(p)+1] == strings.ToLower(p)+"/" {
return path[:len(p)] + "/" + Hash(path[len(p):]+"|"+salt)
}
}
return ""
}
func (sw *MetricsManager) anonymizeQuery(query url.Values, salt string) string {
for _, q := range query {
for i, s := range q {
if s != "" {
s = Hash(s + "|" + salt)
q[i] = s
}
}
}
return query.Encode()
}