fix requestid

This commit is contained in:
A.Unger
2021-08-18 11:10:50 +02:00
parent a7d89c7065
commit 1dd4644370
3 changed files with 17 additions and 10 deletions

View File

@@ -1,10 +1,12 @@
package middleware
import (
"context"
"net/http"
"time"
"github.com/go-chi/chi/middleware"
chimiddleware "github.com/go-chi/chi/middleware"
"github.com/owncloud/ocis/ocis-pkg/log"
)
@@ -18,7 +20,7 @@ func AccessLog(logger log.Logger) func(http.Handler) http.Handler {
logger.Info().
Str("proto", r.Proto).
Str("request", r.Header.Get("X-Request-ID")).
Str("request", ExtractRequestID(r.Context())).
Str("remote-addr", r.RemoteAddr).
Str("method", r.Method).
Int("status", wrap.Status()).
@@ -29,3 +31,14 @@ func AccessLog(logger log.Logger) func(http.Handler) http.Handler {
})
}
}
// ExtractRequestID extracts the request ID from the context. Since we now use the go-chi middleware to write the request
// id, this is propagated using the context, therefore read it from there.
func ExtractRequestID(ctx context.Context) string {
var requestId string
if v, ok := ctx.Value(chimiddleware.RequestIDKey).(string); ok {
requestId = v
}
return requestId
}

View File

@@ -11,6 +11,8 @@ import (
"strings"
"time"
"github.com/owncloud/ocis/proxy/pkg/middleware"
"go.opentelemetry.io/otel/attribute"
"github.com/owncloud/ocis/ocis-pkg/log"
@@ -229,7 +231,7 @@ func (p *MultiHostReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request
span.SetAttributes(
attribute.KeyValue{
Key: "x-request-id",
Value: attribute.StringValue(r.Header.Get("X-Request-ID")),
Value: attribute.StringValue(middleware.ExtractRequestID(r.Context())),
})
pkgtrace.Propagator.Inject(ctx, propagation.HeaderCarrier(r.Header))