diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-11-01 21:39:42 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-11-01 21:39:42 -0700 |
commit | 0f07efbf6ad59793c96fb1d7ffdd68ddff7bd7f4 (patch) | |
tree | 46889767ba216bd9cbbbc3d239dc7f88af22ebad | |
parent | 6ee469616d56b069dd68391577f0cd9411abc46a (diff) |
adding analytics tracking to API calls
-rw-r--r-- | system/api/analytics/init.go | 29 | ||||
-rw-r--r-- | system/api/server.go | 10 |
2 files changed, 16 insertions, 23 deletions
diff --git a/system/api/analytics/init.go b/system/api/analytics/init.go index eaac246..3af1407 100644 --- a/system/api/analytics/init.go +++ b/system/api/analytics/init.go @@ -27,25 +27,20 @@ var ( ) // Record queues an apiRequest for metrics -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 - - next.ServeHTTP(res, req) +func Record(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 } // Close exports the abillity to close our db file. Should be called with defer diff --git a/system/api/server.go b/system/api/server.go index bfd8469..4468394 100644 --- a/system/api/server.go +++ b/system/api/server.go @@ -2,17 +2,15 @@ package api import ( "net/http" - - a "github.com/bosssauce/ponzu/system/api/analytics" ) // Run adds Handlers to default http listener for API func Run() { - http.HandleFunc("/api/types", CORS(a.Record(typesHandler))) + http.HandleFunc("/api/types", CORS(Record(typesHandler))) - http.HandleFunc("/api/posts", CORS(a.Record(postsHandler))) + http.HandleFunc("/api/posts", CORS(Record(postsHandler))) - http.HandleFunc("/api/post", CORS(a.Record(postHandler))) + http.HandleFunc("/api/post", CORS(Record(postHandler))) - http.HandleFunc("/api/external/posts", CORS(a.Record(externalPostsHandler))) + http.HandleFunc("/api/external/posts", CORS(Record(externalPostsHandler))) } |