Browse Source

Change requestId to requestID.

Coda Hale 12 years ago
parent
commit
0b12f5954e
3 changed files with 25 additions and 26 deletions
  1. +9
    -9
      web/middleware/logger.go
  2. +5
    -5
      web/middleware/recoverer.go
  3. +11
    -12
      web/middleware/request_id.go

+ 9
- 9
web/middleware/logger.go View File

@ -17,9 +17,9 @@ import (
// Logger prints a request ID if one is provided. // Logger prints a request ID if one is provided.
func Logger(c *web.C, h http.Handler) http.Handler { func Logger(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) { fn := func(w http.ResponseWriter, r *http.Request) {
reqId := GetReqId(*c)
reqID := GetReqID(*c)
printStart(reqId, r)
printStart(reqID, r)
lw := wrapWriter(w) lw := wrapWriter(w)
@ -28,17 +28,17 @@ func Logger(c *web.C, h http.Handler) http.Handler {
lw.maybeWriteHeader() lw.maybeWriteHeader()
t2 := time.Now() t2 := time.Now()
printEnd(reqId, lw, t2.Sub(t1))
printEnd(reqID, lw, t2.Sub(t1))
} }
return http.HandlerFunc(fn) return http.HandlerFunc(fn)
} }
func printStart(reqId string, r *http.Request) {
func printStart(reqID string, r *http.Request) {
var buf bytes.Buffer var buf bytes.Buffer
if reqId != "" {
cW(&buf, bBlack, "[%s] ", reqId)
if reqID != "" {
cW(&buf, bBlack, "[%s] ", reqID)
} }
buf.WriteString("Started ") buf.WriteString("Started ")
cW(&buf, bMagenta, "%s ", r.Method) cW(&buf, bMagenta, "%s ", r.Method)
@ -49,11 +49,11 @@ func printStart(reqId string, r *http.Request) {
log.Print(buf.String()) log.Print(buf.String())
} }
func printEnd(reqId string, w writerProxy, dt time.Duration) {
func printEnd(reqID string, w writerProxy, dt time.Duration) {
var buf bytes.Buffer var buf bytes.Buffer
if reqId != "" {
cW(&buf, bBlack, "[%s] ", reqId)
if reqID != "" {
cW(&buf, bBlack, "[%s] ", reqID)
} }
buf.WriteString("Returning ") buf.WriteString("Returning ")
if w.status() < 200 { if w.status() < 200 {


+ 5
- 5
web/middleware/recoverer.go View File

@ -16,11 +16,11 @@ import (
// Recoverer prints a request ID if one is provided. // Recoverer prints a request ID if one is provided.
func Recoverer(c *web.C, h http.Handler) http.Handler { func Recoverer(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) { fn := func(w http.ResponseWriter, r *http.Request) {
reqId := GetReqId(*c)
reqID := GetReqID(*c)
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
printPanic(reqId, err)
printPanic(reqID, err)
debug.PrintStack() debug.PrintStack()
http.Error(w, http.StatusText(500), 500) http.Error(w, http.StatusText(500), 500)
} }
@ -32,11 +32,11 @@ func Recoverer(c *web.C, h http.Handler) http.Handler {
return http.HandlerFunc(fn) return http.HandlerFunc(fn)
} }
func printPanic(reqId string, err interface{}) {
func printPanic(reqID string, err interface{}) {
var buf bytes.Buffer var buf bytes.Buffer
if reqId != "" {
cW(&buf, bBlack, "[%s] ", reqId)
if reqID != "" {
cW(&buf, bBlack, "[%s] ", reqID)
} }
cW(&buf, bRed, "panic: %#v", err) cW(&buf, bRed, "panic: %#v", err)


+ 11
- 12
web/middleware/request_id.go View File

@ -13,7 +13,7 @@ import (
) )
// Key to use when setting the request ID. // Key to use when setting the request ID.
const RequestIdKey = "reqId"
const RequestIDKey = "reqID"
var prefix string var prefix string
var reqid uint64 var reqid uint64
@ -33,18 +33,18 @@ func init() {
prefix = fmt.Sprintf("%s/%s", hostname, b64[0:8]) prefix = fmt.Sprintf("%s/%s", hostname, b64[0:8])
} }
// RequestId is a middleware that injects a request ID into the context of each
// RequestID is a middleware that injects a request ID into the context of each
// request. A request ID is a string of the form "host.example.com/random-0001", // request. A request ID is a string of the form "host.example.com/random-0001",
// where "random" is a base62 random string that uniquely identifies this go // where "random" is a base62 random string that uniquely identifies this go
// process, and where the last number is an atomically incremented request // process, and where the last number is an atomically incremented request
// counter. // counter.
func RequestId(c *web.C, h http.Handler) http.Handler {
func RequestID(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) { fn := func(w http.ResponseWriter, r *http.Request) {
if c.Env == nil { if c.Env == nil {
c.Env = make(map[string]interface{}) c.Env = make(map[string]interface{})
} }
myid := atomic.AddUint64(&reqid, 1) myid := atomic.AddUint64(&reqid, 1)
c.Env[RequestIdKey] = fmt.Sprintf("%s-%06d", prefix, myid)
c.Env[RequestIDKey] = fmt.Sprintf("%s-%06d", prefix, myid)
h.ServeHTTP(w, r) h.ServeHTTP(w, r)
} }
@ -52,19 +52,18 @@ func RequestId(c *web.C, h http.Handler) http.Handler {
return http.HandlerFunc(fn) return http.HandlerFunc(fn)
} }
// Get a request ID from the given context if one is present. Returns the empty
// string if a request ID cannot be found.
func GetReqId(c web.C) string {
// GetReqID returns a request ID from the given context if one is present.
// Returns the empty string if a request ID cannot be found.
func GetReqID(c web.C) string {
if c.Env == nil { if c.Env == nil {
return "" return ""
} }
v, ok := c.Env[RequestIdKey]
v, ok := c.Env[RequestIDKey]
if !ok { if !ok {
return "" return ""
} }
if reqId, ok := v.(string); ok {
return reqId
} else {
return ""
if reqID, ok := v.(string); ok {
return reqID
} }
return ""
} }

Loading…
Cancel
Save