diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-11-01 21:36:25 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-11-01 21:36:25 -0700 |
commit | 6ee469616d56b069dd68391577f0cd9411abc46a (patch) | |
tree | 2ea2da04b0726706bd5b0aa81891b02f7fa1ac32 /system/api/analytics/init.go | |
parent | ac647e2f77d05cc015a369832c2d428ec1cd6567 (diff) |
adding analytics tracking to API calls
Diffstat (limited to 'system/api/analytics/init.go')
-rw-r--r-- | system/api/analytics/init.go | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/system/api/analytics/init.go b/system/api/analytics/init.go index c351bed..eaac246 100644 --- a/system/api/analytics/init.go +++ b/system/api/analytics/init.go @@ -15,6 +15,7 @@ import ( type apiRequest struct { URL string `json:"url"` Method string `json:"http_method"` + Origin string `json:"origin"` RemoteAddr string `json:"ip_address"` Timestamp int64 `json:"timestamp"` External bool `json:"external"` @@ -26,19 +27,24 @@ var ( ) // Record queues an apiRequest for metrics -func Record(req *http.Request) { - external := strings.Contains(req.URL.Path, "/external/") - - r := apiRequest{ - URL: req.URL.String(), - Method: req.Method, - RemoteAddr: req.RemoteAddr, - Timestamp: time.Now().Unix() * 1000, - External: external, - } +func Record(next http.HandlerFunc) http.HandlerFunc { + return func(res http.ResponseWriter, req *http.Request) { + external := strings.Contains(req.URL.Path, "/external/") + + r := apiRequest{ + URL: req.URL.String(), + Method: req.Method, + Origin: req.Header.Get("Origin"), + RemoteAddr: req.RemoteAddr, + Timestamp: time.Now().Unix() * 1000, + External: external, + } - // put r on buffered recordChan to take advantage of batch insertion in DB - recordChan <- r + // put r on buffered recordChan to take advantage of batch insertion in DB + recordChan <- r + + next.ServeHTTP(res, req) + } } @@ -64,10 +70,6 @@ func Init() { go serve() - err = store.Update(func(tx *bolt.Tx) error { - - return nil - }) if err != nil { log.Fatalln(err) } @@ -93,6 +95,11 @@ func serve() { reqs = append(reqs, <-recordChan) } + err := batchInsert(reqs) + if err != nil { + log.Println(err) + } + case <-pruneDBTimer.C: default: |