Skip to content

Commit

Permalink
Pass request in new trace signature
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepeterson committed Jul 21, 2022
1 parent 5db6e34 commit 92c977e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/nanomdm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func main() {
// Currently this just makes a random string. This would be better
// served by e.g. https://github.com/oklog/ulid or something like
// https://opentelemetry.io/ someday.
func newTraceID() string {
func newTraceID(_ *http.Request) string {
b := make([]byte, 8)
rand.Read(b)
return fmt.Sprintf("%x", b)
Expand Down
9 changes: 6 additions & 3 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ type ctxKeyTraceID struct{}

// TraceLoggingMiddleware sets up a trace ID in the request context and
// logs HTTP requests.
func TraceLoggingMiddleware(next http.Handler, logger log.Logger, newTrace func() string) http.HandlerFunc {
func TraceLoggingMiddleware(next http.Handler, logger log.Logger, newTrace func(*http.Request) string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), ctxKeyTraceID{}, newTrace())
ctx = ctxlog.AddFunc(ctx, ctxlog.SimpleStringFunc("trace_id", ctxKeyTraceID{}))
ctx := r.Context()
if newTrace != nil {
ctx = context.WithValue(r.Context(), ctxKeyTraceID{}, newTrace(r))
ctx = ctxlog.AddFunc(ctx, ctxlog.SimpleStringFunc("trace_id", ctxKeyTraceID{}))
}

host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
Expand Down

0 comments on commit 92c977e

Please sign in to comment.