-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
middleware.go
365 lines (293 loc) · 7.82 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright © 2022 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package metricsx
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"net"
"net/http"
"net/url"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/ory/x/httpx"
"google.golang.org/grpc"
"google.golang.org/grpc/status"
"github.com/ory/x/configx"
"github.com/spf13/cobra"
"github.com/ory/x/cmdx"
"github.com/ory/x/logrusx"
"github.com/ory/x/resilience"
"github.com/pborman/uuid"
analytics "github.com/ory/analytics-go/v4"
)
var instance *Service
var lock sync.Mutex
// Service helps with providing context on metrics.
type Service struct {
optOut bool
salt string
o *Options
context *analytics.Context
c analytics.Client
l *logrusx.Logger
mem *MemoryStatistics
}
// Hash returns a hashed string of the value.
func Hash(value string) string {
hash := sha256.New()
_, err := hash.Write([]byte(value))
if err != nil {
panic(fmt.Sprintf("unable to hash value"))
}
return hex.EncodeToString(hash.Sum(nil))
}
// Options configures the metrics service.
type Options struct {
// Service represents the service name, for example "ory-hydra".
Service string
// ClusterID represents the cluster id, typically a hash of some unique configuration properties.
ClusterID string
// IsDevelopment should be true if we assume that we're in a development environment.
IsDevelopment bool
// WriteKey is the segment API key.
WriteKey string
// WhitelistedPaths represents a list of paths that can be transmitted in clear text to segment.
WhitelistedPaths []string
// BuildVersion represents the build version.
BuildVersion string
// BuildHash represents the build git hash.
BuildHash string
// BuildTime represents the build time.
BuildTime string
// Config overrides the analytics.Config. If nil, sensible defaults will be used.
Config *analytics.Config
// MemoryInterval sets how often memory statistics should be transmitted. Defaults to every 12 hours.
MemoryInterval time.Duration
}
type void struct {
}
func (v *void) Logf(format string, args ...interface{}) {
}
func (v *void) Errorf(format string, args ...interface{}) {
}
// New returns a new metrics service. If one has been instantiated already, no new instance will be created.
func New(
cmd *cobra.Command,
l *logrusx.Logger,
c *configx.Provider,
o *Options,
) *Service {
lock.Lock()
defer lock.Unlock()
if instance != nil {
return instance
}
if o.BuildTime == "" {
o.BuildTime = "unknown"
}
if o.BuildVersion == "" {
o.BuildVersion = "unknown"
}
if o.BuildHash == "" {
o.BuildHash = "unknown"
}
if o.Config == nil {
o.Config = &analytics.Config{
Interval: time.Hour * 24,
}
}
o.Config.Logger = new(void)
if o.MemoryInterval < time.Minute {
o.MemoryInterval = time.Hour * 12
}
segment, err := analytics.NewWithConfig(o.WriteKey, *o.Config)
if err != nil {
l.WithError(err).Fatalf("Unable to initialise software quality assurance features.")
return nil
}
var oi analytics.OSInfo
optOut, err := cmd.Flags().GetBool("sqa-opt-out")
if err != nil {
cmdx.Must(err, `Unable to get command line flag "sqa-opt-out": %s`, err)
}
if !optOut {
optOut = c.Bool("sqa.opt_out")
}
if !optOut {
optOut = c.Bool("sqa_opt_out")
}
if !optOut {
optOut, _ = strconv.ParseBool(os.Getenv("SQA_OPT_OUT"))
}
if !optOut {
optOut, _ = strconv.ParseBool(os.Getenv("SQA-OPT-OUT"))
}
if !optOut {
l.Info("Software quality assurance features are enabled. Learn more at: https://www.ory.sh/docs/ecosystem/sqa")
oi = analytics.OSInfo{
Version: fmt.Sprintf("%s-%s", runtime.GOOS, runtime.GOARCH),
}
}
m := &Service{
optOut: optOut,
salt: uuid.New(),
o: o,
c: segment,
l: l,
mem: new(MemoryStatistics),
context: &analytics.Context{
IP: net.IPv4(0, 0, 0, 0),
App: analytics.AppInfo{
Name: o.Service,
Version: o.BuildVersion,
Build: fmt.Sprintf("%s/%s/%s", o.BuildVersion, o.BuildHash, o.BuildTime),
},
OS: oi,
Traits: analytics.NewTraits().
Set("optedOut", optOut).
Set("instanceId", uuid.New()).
Set("isDevelopment", o.IsDevelopment),
UserAgent: "github.com/ory/x/metricsx.Service/v0.0.1",
},
}
instance = m
go m.Identify()
go m.ObserveMemory()
return m
}
// Identify enables reporting to segment.
func (sw *Service) Identify() {
if err := resilience.Retry(sw.l, time.Minute*5, time.Hour*24*30, func() error {
return sw.c.Enqueue(analytics.Identify{
UserId: sw.o.ClusterID,
Traits: sw.context.Traits,
Context: sw.context,
})
}); err != nil {
sw.l.WithError(err).Debug("Could not commit anonymized environment information")
}
}
// ObserveMemory commits memory statistics to segment.
func (sw *Service) ObserveMemory() {
if sw.optOut {
return
}
for {
sw.mem.Update()
if err := sw.c.Enqueue(analytics.Track{
UserId: sw.o.ClusterID,
Event: "memstats",
Properties: analytics.Properties(sw.mem.ToMap()),
Context: sw.context,
}); err != nil {
sw.l.WithError(err).Debug("Could not commit anonymized telemetry data")
}
time.Sleep(sw.o.MemoryInterval)
}
}
type negroniMiddleware interface {
Size() int
Status() int
}
// ServeHTTP is a middleware for sending meta information to segment.
func (sw *Service) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
var start time.Time
if !sw.optOut {
start = time.Now()
}
next(rw, r)
if sw.optOut {
return
}
latency := time.Since(start) / 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
stat, size := httpx.GetResponseMeta(rw)
if err := sw.c.Enqueue(analytics.Page{
UserId: sw.o.ClusterID,
Name: path,
Properties: analytics.
NewProperties().
SetURL(scheme+"//"+sw.o.ClusterID+path+"?"+query).
SetPath(path).
SetName(path).
Set("status", stat).
Set("size", size).
Set("latency", latency).
Set("method", r.Method),
Context: sw.context,
}); err != nil {
sw.l.WithError(err).Debug("Could not commit anonymized telemetry data")
// do nothing...
}
}
func (sw *Service) UnaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
var start time.Time
if !sw.optOut {
start = time.Now()
}
resp, err := handler(ctx, req)
if sw.optOut {
return resp, err
}
latency := time.Since(start) / time.Millisecond
if err := sw.c.Enqueue(analytics.Page{
UserId: sw.o.ClusterID,
Name: info.FullMethod,
Properties: analytics.
NewProperties().
SetURL("grpc://"+sw.o.ClusterID+info.FullMethod).
SetPath(info.FullMethod).
SetName(info.FullMethod).
Set("status", status.Code(err)).
Set("latency", latency),
Context: sw.context,
}); err != nil {
sw.l.WithError(err).Debug("Could not commit anonymized telemetry data")
// do nothing...
}
return resp, err
}
func (sw *Service) StreamInterceptor(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
// this needs a bit of thought, but we don't have streaming RPCs currently anyway
sw.l.Info("The telemetry stream interceptor is not yet implemented!")
return handler(srv, stream)
}
func (sw *Service) Close() error {
return sw.c.Close()
}
func (sw *Service) anonymizePath(path string, salt string) string {
paths := sw.o.WhitelistedPaths
path = strings.ToLower(path)
for _, p := range paths {
p = strings.ToLower(p)
if path == p {
return p
} else if strings.HasPrefix(path, p) {
return path[:len(p)] + "/" + Hash(path[len(p):]+"|"+salt)
}
}
return "/"
}
func (sw *Service) 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()
}