introducing requestID to http stack

pull/1340/head
Cory LaNou 2015-01-21 15:36:50 -07:00
parent 29a784efef
commit 7a329c6c2c
3 changed files with 17 additions and 2 deletions

View File

@ -8,6 +8,8 @@ import (
"strings"
"time"
"code.google.com/p/go-uuid/uuid"
"github.com/influxdb/influxdb"
)
@ -111,6 +113,16 @@ func cors(inner http.Handler) http.Handler {
})
}
func requestID(inner http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
uid := uuid.NewUUID()
r.Header.Set("Request-Id", uid.String())
w.Header().Set("Request-Id", r.Header.Get("Request-Id"))
inner.ServeHTTP(w, r)
})
}
func logging(inner http.Handler, name string, weblog *log.Logger) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()

View File

@ -85,6 +85,7 @@ func NewHandler(s *influxdb.Server, requireAuthentication bool) *Handler {
handler = r.handlerFunc
handler = authorize(handler, h, requireAuthentication)
handler = cors(handler)
handler = requestID(handler)
handler = logging(handler, r.name, weblog)
handler = recovery(handler, r.name, weblog) // make sure recovery is always last

View File

@ -50,7 +50,8 @@ func (l *responseLogger) Size() int {
// Common Log Format: http://en.wikipedia.org/wiki/Common_Log_Format
// buildLogLine creates a common log format, plus referrer and user agent at the end
// buildLogLine creates a common log format
// in addittion to the common fields, we also append referrer, user agent and request ID
func buildLogLine(l *responseLogger, r *http.Request, start time.Time) string {
username := "-"
url := r.URL
@ -68,7 +69,7 @@ func buildLogLine(l *responseLogger, r *http.Request, start time.Time) string {
uri := url.RequestURI()
return fmt.Sprintf(
"%s %s %s %s %s %s %s %d %d %s %s",
"%s %s %s %s %s %s %s %d %d %s %s %s",
host,
"-",
username,
@ -80,5 +81,6 @@ func buildLogLine(l *responseLogger, r *http.Request, start time.Time) string {
l.Size(),
r.Referer(),
r.UserAgent(),
r.Header.Get("Request-Id"),
)
}