Skip to content

Commit c16d77d

Browse files
authored
chore: update how we get request IDs (#313)
* update how we get request IDs * test that we log properly * also test that the tracer is injected
1 parent 4e45678 commit c16d77d

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

router/router_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"strconv"
77
"testing"
88

9+
"github.com/netlify/netlify-commons/testutil"
10+
"github.com/netlify/netlify-commons/tracing"
911
"github.com/opentracing/opentracing-go"
1012
"github.com/opentracing/opentracing-go/mocktracer"
1113
"github.com/sirupsen/logrus"
@@ -79,11 +81,13 @@ func TestTracing(t *testing.T) {
7981
}()
8082

8183
noop := func(w http.ResponseWriter, r *http.Request) error {
84+
assert.NotNil(t, tracing.GetTracer(r))
8285
w.WriteHeader(http.StatusOK)
8386
return nil
8487
}
8588

86-
r := New(logrus.WithField("test", t.Name()), OptEnableTracing("some-service"))
89+
tl, logHook := testutil.TestLogger(t)
90+
r := New(tl, OptEnableTracing("some-service"))
8791

8892
r.Method(http.MethodPatch, "/patch", noop)
8993
r.Delete("/abc/{def}", noop)
@@ -110,6 +114,7 @@ func TestTracing(t *testing.T) {
110114
for name, scene := range scenes {
111115
t.Run(name, func(t *testing.T) {
112116
mt.Reset()
117+
logHook.Reset()
113118

114119
rec := httptest.NewRecorder()
115120
r.ServeHTTP(rec, httptest.NewRequest(scene.method, scene.path, nil))
@@ -121,6 +126,8 @@ func TestTracing(t *testing.T) {
121126
assert.Equal(t, scene.resourceName, spans[0].Tag(ext.ResourceName))
122127
assert.Equal(t, strconv.Itoa(http.StatusOK), spans[0].Tag(ext.HTTPCode))
123128
}
129+
// should be a starting and finished request for each request
130+
assert.Len(t, logHook.AllEntries(), 2)
124131
})
125132
}
126133
}

tracing/context.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package tracing
33
import (
44
"context"
55
"net/http"
6+
7+
uuid "github.com/satori/go.uuid"
68
)
79

810
type contextKey string
@@ -33,7 +35,10 @@ func GetRequestID(r *http.Request) string {
3335
if id := GetRequestIDFromContext(r.Context()); id != "" {
3436
return id
3537
}
36-
return r.Header.Get(HeaderRequestUUID)
38+
if rid := r.Header.Get(HeaderRequestUUID); rid != "" {
39+
return rid
40+
}
41+
return uuid.NewV4().String()
3742
}
3843

3944
func GetRequestIDFromContext(ctx context.Context) string {

tracing/logging.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@ import (
88
"github.com/sirupsen/logrus"
99
)
1010

11-
const (
12-
logKey = contextKey("nf-log-key")
13-
)
14-
15-
func requestLogger(r *http.Request, log logrus.FieldLogger) (logrus.FieldLogger, string) {
11+
func RequestLogger(r *http.Request, log logrus.FieldLogger) (logrus.FieldLogger, string) {
1612
if r.Header.Get(HeaderNFDebugLogging) != "" {
1713
logger := logrus.New()
1814
logger.SetLevel(logrus.DebugLevel)
1915

2016
if entry, ok := log.(*logrus.Entry); ok {
2117
log = logger.WithFields(entry.Data)
18+
logger.Hooks = entry.Logger.Hooks
2219
}
2320
}
2421

tracing/req_tracer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type RequestTracer struct {
2727

2828
func NewTracer(w http.ResponseWriter, r *http.Request, log logrus.FieldLogger, service, resource string) (http.ResponseWriter, *http.Request, *RequestTracer) {
2929
var reqID string
30-
log, reqID = requestLogger(r, log)
30+
log, reqID = RequestLogger(r, log)
3131

3232
r, span := WrapWithSpan(r, reqID, service, resource)
3333
trackWriter := &trackingWriter{

0 commit comments

Comments
 (0)