MCPcopy Create free account
hub / github.com/NVIDIA/nvcf / loggingMiddleware

Function loggingMiddleware

src/libraries/go/lib/pkg/core/httpservice.go:484–508  ·  view source on GitHub ↗
(rootCtx context.Context)

Source from the content-addressed store, hash-verified

482func telemetryAttributesMiddleware(additionalAttributes []attribute.KeyValue) mux.MiddlewareFunc {
483 return func(next http.Handler) http.Handler {
484 return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
485 trace.SpanFromContext(r.Context()).SetAttributes(additionalAttributes...)
486 next.ServeHTTP(rw, r)
487 })
488 }
489}
490
491func loggingMiddleware(rootCtx context.Context) mux.MiddlewareFunc {
492 return func(next http.Handler) http.Handler {
493 return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
494 reqID := nextRequestID(rootCtx, &httpReqSequenceID)
495 logFields := map[string]interface{}{
496 "path": r.URL.Path,
497 "method": r.Method,
498 "ip": r.RemoteAddr,
499 "http_req_id": reqID,
500 }
501 // Retrieve the span and trace IDs if they exist and log them
502 spanCtx := trace.SpanContextFromContext(r.Context())
503 if spanCtx.HasSpanID() {
504 logFields["span_id"] = spanCtx.SpanID().String()
505 }
506 if spanCtx.HasTraceID() {
507 logFields["trace_id"] = spanCtx.TraceID().String()
508 }
509
510 // Create new logger with fields and pass along
511 requestLogger := GetLogger(rootCtx).WithFields(logFields)

Callers 1

NewHTTPMiddlewareFunction · 0.85

Calls 5

nextRequestIDFunction · 0.85
GetLoggerFunction · 0.85
WithLoggerFunction · 0.70
StringMethod · 0.65
ServeHTTPMethod · 0.45

Tested by

no test coverage detected