parent
a8f5328bb7
commit
7a3aa8746f
|
@ -1,7 +1,5 @@
|
||||||
language: go
|
language: go
|
||||||
go:
|
go:
|
||||||
- "1.7"
|
|
||||||
- "1.8"
|
|
||||||
- "1.9"
|
- "1.9"
|
||||||
- "1.10"
|
- "1.10"
|
||||||
- "1.11"
|
- "1.11"
|
||||||
|
@ -9,6 +7,7 @@ go:
|
||||||
- "1.13"
|
- "1.13"
|
||||||
- "1.14"
|
- "1.14"
|
||||||
- "1.15"
|
- "1.15"
|
||||||
|
- "1.16"
|
||||||
- "master"
|
- "master"
|
||||||
matrix:
|
matrix:
|
||||||
allow_failures:
|
allow_failures:
|
||||||
|
|
|
@ -138,6 +138,11 @@ func IDFromCtx(ctx context.Context) (id xid.ID, ok bool) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CtxWithID adds the given xid.ID to the context
|
||||||
|
func CtxWithID(ctx context.Context, id xid.ID) context.Context {
|
||||||
|
return context.WithValue(ctx, idKey{}, id)
|
||||||
|
}
|
||||||
|
|
||||||
// RequestIDHandler returns a handler setting a unique id to the request which can
|
// RequestIDHandler returns a handler setting a unique id to the request which can
|
||||||
// be gathered using IDFromRequest(req). This generated id is added as a field to the
|
// be gathered using IDFromRequest(req). This generated id is added as a field to the
|
||||||
// logger using the passed fieldKey as field name. The id is also added as a response
|
// logger using the passed fieldKey as field name. The id is also added as a response
|
||||||
|
@ -154,7 +159,7 @@ func RequestIDHandler(fieldKey, headerName string) func(next http.Handler) http.
|
||||||
id, ok := IDFromRequest(r)
|
id, ok := IDFromRequest(r)
|
||||||
if !ok {
|
if !ok {
|
||||||
id = xid.New()
|
id = xid.New()
|
||||||
ctx = context.WithValue(ctx, idKey{}, id)
|
ctx = CtxWithID(ctx, id)
|
||||||
r = r.WithContext(ctx)
|
r = r.WithContext(ctx)
|
||||||
}
|
}
|
||||||
if fieldKey != "" {
|
if fieldKey != "" {
|
||||||
|
|
|
@ -4,16 +4,16 @@ package hlog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"reflect"
|
"github.com/rs/xid"
|
||||||
|
|
||||||
"net/http/httptest"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/internal/cbor"
|
"github.com/rs/zerolog/internal/cbor"
|
||||||
)
|
)
|
||||||
|
@ -262,3 +262,16 @@ func BenchmarkDataRace(b *testing.B) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCtxWithID(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
id, _ := xid.FromString(`c0umremcie6smuu506pg`)
|
||||||
|
|
||||||
|
want := context.Background()
|
||||||
|
want = context.WithValue(want, idKey{}, id)
|
||||||
|
|
||||||
|
if got := CtxWithID(ctx, id); !reflect.DeepEqual(got, want) {
|
||||||
|
t.Errorf("CtxWithID() = %v, want %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue