Skip to content

Commit

Permalink
track feature use + use syslog as option
Browse files Browse the repository at this point in the history
  • Loading branch information
untoldone committed May 13, 2015
1 parent 3954519 commit c94ac57
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 1 deletion.
34 changes: 34 additions & 0 deletions api/features.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package api

import (
"time"
"net/http"
"github.com/gorilla/context"
)

func AddFeature(r *http.Request, feature string) {
rawFeatures, ok := context.GetOk(r, "features")

if ok {
features := rawFeatures.(map[string]int)
features[feature] += 1
} else {
context.Set(r, "features", map[string]int{ feature: 1 })
}
}

func GetFeatures(r *http.Request) map[string]interface{} {
result := map[string]interface{} {}

if features, ok := context.GetOk(r, "features"); ok {
result["features"] = features
}

if identifier, ok := context.GetOk(r, "api_key"); ok {
result["identifier"] = identifier
}

result["timestamp"] = time.Now().UTC().Format(time.RFC3339Nano)

return result
}
32 changes: 32 additions & 0 deletions api/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package api

import (
"os"
"log"
"io"
"io/ioutil"
"log/syslog"
"github.com/spf13/viper"
)

var statsLogger *log.Logger

func StatsLogger() *log.Logger {
if statsLogger == nil {
var statsWriter io.Writer

logStyle := viper.GetString("statsLogger")
switch logStyle {
case "syslog":
statsWriter, _ = syslog.New(syslog.LOG_NOTICE, "bloomapi-stats")
case "stdout":
statsWriter = os.Stdout
default:
statsWriter = ioutil.Discard
}

statsLogger = log.New(statsWriter, "", 0)
}

return statsLogger
}
17 changes: 17 additions & 0 deletions bloomapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"os"
"fmt"
"log"
"log/syslog"
"github.com/spf13/viper"
"github.com/untoldone/bloomapi/cmd"
)
Expand Down Expand Up @@ -33,6 +35,21 @@ func main() {
os.Exit(1)
}

logStyle := viper.GetString("generalLogger")
switch logStyle {
case "syslog":
logwriter, e := syslog.New(syslog.LOG_NOTICE, "bloomapi")
if e != nil {
fmt.Println("Error initializing syslog")
os.Exit(1)
}
log.SetOutput(logwriter)
case "stdout":
default:
fmt.Println("Invalid logger:", logStyle, "please select 'syslog' or 'stdout'")
os.Exit(1)
}

switch arg {
case "server":
cmd.Server()
Expand Down
2 changes: 2 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func Server() {

// Router setup
router := mux.NewRouter()
router.KeepContext = true

// For Backwards Compatibility Feb 13, 2015
router.HandleFunc("/api/search", handler_compat.NpiSearchHandler).Methods("GET")
Expand All @@ -35,6 +36,7 @@ func Server() {
n.UseHandler(router)

// Post-Router Middleware setup
n.Use(middleware.NewRecordFeatures())
n.Use(middleware.NewClearContext())

log.Println("Running Server")
Expand Down
2 changes: 2 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ searchHosts = [
"localhost"
]
bloomapiPort = 3005
statsLogger = "syslog" # values can be: 'syslog' or 'stdout'
generalLogger = "syslog"
3 changes: 3 additions & 0 deletions handler/item_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func ItemHandler (w http.ResponseWriter, req *http.Request) {
source := strings.ToLower(vars["source"])
id := vars["id"]

api.AddFeature(req, "handler:item")
api.AddFeature(req, source)

bloomConn := api.Conn()
searchConn := bloomConn.SearchConnection()

Expand Down
3 changes: 3 additions & 0 deletions handler/search_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func phraseMatches (paramSets []*SearchParamSet, r *http.Request) []interface{}
elasticPhrases := make([]interface{}, len(paramSets))
for index, set := range paramSets {
shouldValues := make([]map[string]interface{}, len(set.Values))
api.AddFeature(r, "op:" + set.Op)
for vIndex, value := range set.Values {
switch (set.Op) {
case "eq":
Expand Down Expand Up @@ -118,6 +119,8 @@ func Search(sourceType string, params *SearchParams, r *http.Request) (map[strin
},
}


api.AddFeature(r, "sort")
api.AddMessage(r, experimentalSort)
}

Expand Down
3 changes: 3 additions & 0 deletions handler/search_source_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ func SearchSourceHandler (w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
source := strings.ToLower(vars["source"])

api.AddFeature(req, source)
api.AddFeature(req, "handler:search")

conn := api.Conn()
apiKey, ok := context.Get(req, "api_key").(string)
if !ok {
Expand Down
4 changes: 3 additions & 1 deletion handler/sources_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ type dataSource struct {
func SourcesHandler (w http.ResponseWriter, req *http.Request) {
conn := api.Conn()
apiKey, ok := context.Get(req, "api_key").(string)
log.Println(apiKey)

api.AddFeature(req, "handler:sources")

if !ok {
apiKey = ""
}
Expand Down
3 changes: 3 additions & 0 deletions handler_compat/npi_item_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ func NpiItemHandler (w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
npi := vars["npi"]

api.AddFeature(req, "usgov.hhs.npi")
api.AddFeature(req, "handler:legacynpi:item")

conn := api.Conn().SearchConnection()

result, err := conn.Search("usgov.hhs.npi", "main", nil, map[string]interface{} {
Expand Down
3 changes: 3 additions & 0 deletions handler_compat/npi_search_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ func NpiSearchHandler (w http.ResponseWriter, req *http.Request) {
return
}

api.AddFeature(req, "usgov.hhs.npi")
api.AddFeature(req, "handler:legacynpi:search")

results, err := handler.Search("usgov.hhs.npi", params, req)
if err != nil {
switch err.(type) {
Expand Down
21 changes: 21 additions & 0 deletions middleware/record_features.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package middleware

import (
"net/http"
"encoding/json"
"github.com/untoldone/bloomapi/api"
)

type RecordFeatures struct {}

func NewRecordFeatures() *RecordFeatures {
return &RecordFeatures{}
}

func (s *RecordFeatures) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
features := api.GetFeatures(r)
j, _ := json.Marshal(features)
featureJson := string(j)
api.StatsLogger().Println(featureJson)
next(rw, r)
}

0 comments on commit c94ac57

Please sign in to comment.