Skip to content

Commit

Permalink
chore: update how we get request IDs (#313)
Browse files Browse the repository at this point in the history
* update how we get request IDs

* test that we log properly

* also test that the tracer is injected
  • Loading branch information
rybit authored Jan 24, 2022
1 parent 4e45678 commit c16d77d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
9 changes: 8 additions & 1 deletion router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strconv"
"testing"

"github.com/netlify/netlify-commons/testutil"
"github.com/netlify/netlify-commons/tracing"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -79,11 +81,13 @@ func TestTracing(t *testing.T) {
}()

noop := func(w http.ResponseWriter, r *http.Request) error {
assert.NotNil(t, tracing.GetTracer(r))
w.WriteHeader(http.StatusOK)
return nil
}

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

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

rec := httptest.NewRecorder()
r.ServeHTTP(rec, httptest.NewRequest(scene.method, scene.path, nil))
Expand All @@ -121,6 +126,8 @@ func TestTracing(t *testing.T) {
assert.Equal(t, scene.resourceName, spans[0].Tag(ext.ResourceName))
assert.Equal(t, strconv.Itoa(http.StatusOK), spans[0].Tag(ext.HTTPCode))
}
// should be a starting and finished request for each request
assert.Len(t, logHook.AllEntries(), 2)
})
}
}
Expand Down
7 changes: 6 additions & 1 deletion tracing/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package tracing
import (
"context"
"net/http"

uuid "github.com/satori/go.uuid"
)

type contextKey string
Expand Down Expand Up @@ -33,7 +35,10 @@ func GetRequestID(r *http.Request) string {
if id := GetRequestIDFromContext(r.Context()); id != "" {
return id
}
return r.Header.Get(HeaderRequestUUID)
if rid := r.Header.Get(HeaderRequestUUID); rid != "" {
return rid
}
return uuid.NewV4().String()
}

func GetRequestIDFromContext(ctx context.Context) string {
Expand Down
7 changes: 2 additions & 5 deletions tracing/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ import (
"github.com/sirupsen/logrus"
)

const (
logKey = contextKey("nf-log-key")
)

func requestLogger(r *http.Request, log logrus.FieldLogger) (logrus.FieldLogger, string) {
func RequestLogger(r *http.Request, log logrus.FieldLogger) (logrus.FieldLogger, string) {
if r.Header.Get(HeaderNFDebugLogging) != "" {
logger := logrus.New()
logger.SetLevel(logrus.DebugLevel)

if entry, ok := log.(*logrus.Entry); ok {
log = logger.WithFields(entry.Data)
logger.Hooks = entry.Logger.Hooks
}
}

Expand Down
2 changes: 1 addition & 1 deletion tracing/req_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type RequestTracer struct {

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

r, span := WrapWithSpan(r, reqID, service, resource)
trackWriter := &trackingWriter{
Expand Down

0 comments on commit c16d77d

Please sign in to comment.