-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
82 lines (63 loc) · 2.27 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
/*
Middleware for Goji that reports HTTP requests to Keen.io.
Add it to your Goji mux m as follows.
m.Use(BuildMiddleWare(myKeenIoProjectId, myKeenIoWriteKey, "apievents", nil))
To add your own data to the events reported add a callback.
callback := func(c *web.C, data map[string]interface{}, r *http.Request) {
data["my_parameter"] = c.Env["important_info"].(string)
}
m.Use(BuildMiddleWare(myKeenIoProjectId, myKeenIoWriteKey, "apievents", callback))
*/
package keengo_goji
import (
"net/http"
"time"
"github.com/philpearl/keengo"
"github.com/zenazn/goji/web"
)
/*
Build a middleware function that reports HTTP requests to Keen.io.
The event the middleware sends is as follows.
{
"url": r.RequestURI,
"path": r.URL.Path,
"method": r.Method,
"status_code": w.Status, // Http status code from response
"duration_ns": time.Since(start).Nanoseconds(),
"user_agent": r.UserAgent(),
"header": r.Header,
}
The callback function allows you to add your own data to the event recorded. Use it as follows to add events to the "apievents" collection.
callback := func(c *web.C, data map[string]interface{}, r *http.Request) {
data["my_parameter"] = c.Env["important_info"].(string)
}
m.Use(BuildMiddleWare(myKeenIoProjectId, myKeenIoWriteKey, "apievents", callback))
*/
func BuildMiddleWare(projectId, writeKey, collectionName string,
callback func(c *web.C, data map[string]interface{}, r *http.Request),
) func(c *web.C, h http.Handler) http.Handler {
sender := keengo.NewSender(projectId, writeKey)
// Return the middleware that references the analytics queue we just made
return func(c *web.C, h http.Handler) http.Handler {
handler := func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
ww := &StatusTrackingResponseWriter{w, http.StatusOK}
h.ServeHTTP(ww, r)
info := map[string]interface{}{
"url": r.RequestURI,
"path": r.URL.Path,
"method": r.Method,
"status_code": ww.Status,
"duration_ns": time.Since(start).Nanoseconds(),
"user_agent": r.UserAgent(),
"header": r.Header,
}
// Get more data for the analytics event
if callback != nil {
callback(c, info, r)
}
sender.Queue(collectionName, info)
}
return http.HandlerFunc(handler)
}
}