[management] log user agent and return request id (#6380)
This commit is contained in:
@@ -99,6 +99,9 @@ func addFields(entry *logrus.Entry) {
|
|||||||
if ctxAccountID, ok := entry.Context.Value(context.AccountIDKey).(string); ok {
|
if ctxAccountID, ok := entry.Context.Value(context.AccountIDKey).(string); ok {
|
||||||
entry.Data[context.AccountIDKey] = ctxAccountID
|
entry.Data[context.AccountIDKey] = ctxAccountID
|
||||||
}
|
}
|
||||||
|
if ctxUserAgent, ok := entry.Context.Value(context.UserAgentKey).(string); ok {
|
||||||
|
entry.Data[context.UserAgentKey] = ctxUserAgent
|
||||||
|
}
|
||||||
if ctxInitiatorID, ok := entry.Context.Value(context.UserIDKey).(string); ok {
|
if ctxInitiatorID, ok := entry.Context.Value(context.UserIDKey).(string); ok {
|
||||||
entry.Data[context.UserIDKey] = ctxInitiatorID
|
entry.Data[context.UserIDKey] = ctxInitiatorID
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ const (
|
|||||||
RoleKey = nbcontext.RoleKey
|
RoleKey = nbcontext.RoleKey
|
||||||
UserIDKey = nbcontext.UserIDKey
|
UserIDKey = nbcontext.UserIDKey
|
||||||
PeerIDKey = nbcontext.PeerIDKey
|
PeerIDKey = nbcontext.PeerIDKey
|
||||||
|
UserAgentKey = nbcontext.UserAgentKey
|
||||||
)
|
)
|
||||||
|
|
||||||
// RoleFromContext returns the role stored in ctx, or empty string and false if absent.
|
// RoleFromContext returns the role stored in ctx, or empty string and false if absent.
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ const (
|
|||||||
httpRequestCounterPrefix = "management.http.request.counter"
|
httpRequestCounterPrefix = "management.http.request.counter"
|
||||||
httpResponseCounterPrefix = "management.http.response.counter"
|
httpResponseCounterPrefix = "management.http.response.counter"
|
||||||
httpRequestDurationPrefix = "management.http.request.duration.ms"
|
httpRequestDurationPrefix = "management.http.request.duration.ms"
|
||||||
|
|
||||||
|
RequestIDHeader = "X-Request-Id"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WrappedResponseWriter is a wrapper for http.ResponseWriter that allows the
|
// WrappedResponseWriter is a wrapper for http.ResponseWriter that allows the
|
||||||
@@ -172,6 +174,10 @@ func (m *HTTPMiddleware) Handler(h http.Handler) http.Handler {
|
|||||||
reqID := xid.New().String()
|
reqID := xid.New().String()
|
||||||
//nolint
|
//nolint
|
||||||
ctx = context.WithValue(ctx, nbContext.RequestIDKey, reqID)
|
ctx = context.WithValue(ctx, nbContext.RequestIDKey, reqID)
|
||||||
|
//nolint
|
||||||
|
ctx = context.WithValue(ctx, nbContext.UserAgentKey, r.UserAgent())
|
||||||
|
|
||||||
|
rw.Header().Set(RequestIDHeader, reqID)
|
||||||
|
|
||||||
log.WithContext(ctx).Tracef("HTTP request %v: %v %v", reqID, r.Method, r.URL)
|
log.WithContext(ctx).Tracef("HTTP request %v: %v %v", reqID, r.Method, r.URL)
|
||||||
|
|
||||||
|
|||||||
@@ -6,4 +6,5 @@ const (
|
|||||||
RoleKey = "role"
|
RoleKey = "role"
|
||||||
UserIDKey = "userID"
|
UserIDKey = "userID"
|
||||||
PeerIDKey = "peerID"
|
PeerIDKey = "peerID"
|
||||||
|
UserAgentKey = "userAgent"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -5107,31 +5107,63 @@ components:
|
|||||||
responses:
|
responses:
|
||||||
not_found:
|
not_found:
|
||||||
description: Resource not found
|
description: Resource not found
|
||||||
|
headers:
|
||||||
|
X-Request-Id:
|
||||||
|
$ref: '#/components/headers/X-Request-Id'
|
||||||
content: { }
|
content: { }
|
||||||
validation_failed_simple:
|
validation_failed_simple:
|
||||||
description: Validation failed
|
description: Validation failed
|
||||||
|
headers:
|
||||||
|
X-Request-Id:
|
||||||
|
$ref: '#/components/headers/X-Request-Id'
|
||||||
content: { }
|
content: { }
|
||||||
bad_request:
|
bad_request:
|
||||||
description: Bad Request
|
description: Bad Request
|
||||||
|
headers:
|
||||||
|
X-Request-Id:
|
||||||
|
$ref: '#/components/headers/X-Request-Id'
|
||||||
content: { }
|
content: { }
|
||||||
internal_error:
|
internal_error:
|
||||||
description: Internal Server Error
|
description: Internal Server Error
|
||||||
|
headers:
|
||||||
|
X-Request-Id:
|
||||||
|
$ref: '#/components/headers/X-Request-Id'
|
||||||
content: { }
|
content: { }
|
||||||
validation_failed:
|
validation_failed:
|
||||||
description: Validation failed
|
description: Validation failed
|
||||||
|
headers:
|
||||||
|
X-Request-Id:
|
||||||
|
$ref: '#/components/headers/X-Request-Id'
|
||||||
content: { }
|
content: { }
|
||||||
forbidden:
|
forbidden:
|
||||||
description: Forbidden
|
description: Forbidden
|
||||||
|
headers:
|
||||||
|
X-Request-Id:
|
||||||
|
$ref: '#/components/headers/X-Request-Id'
|
||||||
content: { }
|
content: { }
|
||||||
requires_authentication:
|
requires_authentication:
|
||||||
description: Requires authentication
|
description: Requires authentication
|
||||||
|
headers:
|
||||||
|
X-Request-Id:
|
||||||
|
$ref: '#/components/headers/X-Request-Id'
|
||||||
content: { }
|
content: { }
|
||||||
conflict:
|
conflict:
|
||||||
description: Conflict
|
description: Conflict
|
||||||
|
headers:
|
||||||
|
X-Request-Id:
|
||||||
|
$ref: '#/components/headers/X-Request-Id'
|
||||||
content:
|
content:
|
||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/components/schemas/ErrorResponse'
|
$ref: '#/components/schemas/ErrorResponse'
|
||||||
|
headers:
|
||||||
|
X-Request-Id:
|
||||||
|
description: |
|
||||||
|
Unique identifier assigned to the request by the server and set on every
|
||||||
|
response. Useful for correlating client requests with server-side logs.
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
example: cot7r4n3l3vh3qj4qveg
|
||||||
securitySchemes:
|
securitySchemes:
|
||||||
BearerAuth:
|
BearerAuth:
|
||||||
type: http
|
type: http
|
||||||
|
|||||||
Reference in New Issue
Block a user