mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-26 13:43:47 +03:00
caddyhttp: Add uuid
to access logs when used (#5859)
This commit is contained in:
parent
dc12bd9743
commit
3d7d60f7cf
3 changed files with 22 additions and 0 deletions
|
@ -64,6 +64,7 @@ func placeholderShorthands() []string {
|
||||||
"{remote_port}", "{http.request.remote.port}",
|
"{remote_port}", "{http.request.remote.port}",
|
||||||
"{scheme}", "{http.request.scheme}",
|
"{scheme}", "{http.request.scheme}",
|
||||||
"{uri}", "{http.request.uri}",
|
"{uri}", "{http.request.uri}",
|
||||||
|
"{uuid}", "{http.request.uuid}",
|
||||||
"{tls_cipher}", "{http.request.tls.cipher_suite}",
|
"{tls_cipher}", "{http.request.tls.cipher_suite}",
|
||||||
"{tls_version}", "{http.request.tls.version}",
|
"{tls_version}", "{http.request.tls.version}",
|
||||||
"{tls_client_fingerprint}", "{http.request.tls.client.fingerprint}",
|
"{tls_client_fingerprint}", "{http.request.tls.client.fingerprint}",
|
||||||
|
|
|
@ -151,6 +151,18 @@ func (e *ExtraLogFields) Add(field zap.Field) {
|
||||||
e.fields = append(e.fields, field)
|
e.fields = append(e.fields, field)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set sets a field in the list of extra fields to log.
|
||||||
|
// If the field already exists, it is replaced.
|
||||||
|
func (e *ExtraLogFields) Set(field zap.Field) {
|
||||||
|
for i := range e.fields {
|
||||||
|
if e.fields[i].Key == field.Key {
|
||||||
|
e.fields[i] = field
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
e.fields = append(e.fields, field)
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// Variable name used to indicate that this request
|
// Variable name used to indicate that this request
|
||||||
// should be omitted from the access logs
|
// should be omitted from the access logs
|
||||||
|
|
|
@ -40,6 +40,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
|
||||||
"github.com/caddyserver/caddy/v2"
|
"github.com/caddyserver/caddy/v2"
|
||||||
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
||||||
|
@ -157,9 +158,17 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
|
||||||
case "http.request.duration_ms":
|
case "http.request.duration_ms":
|
||||||
start := GetVar(req.Context(), "start_time").(time.Time)
|
start := GetVar(req.Context(), "start_time").(time.Time)
|
||||||
return time.Since(start).Seconds() * 1e3, true // multiply seconds to preserve decimal (see #4666)
|
return time.Since(start).Seconds() * 1e3, true // multiply seconds to preserve decimal (see #4666)
|
||||||
|
|
||||||
case "http.request.uuid":
|
case "http.request.uuid":
|
||||||
|
// fetch the UUID for this request
|
||||||
id := GetVar(req.Context(), "uuid").(*requestID)
|
id := GetVar(req.Context(), "uuid").(*requestID)
|
||||||
|
|
||||||
|
// set it to this request's access log
|
||||||
|
extra := req.Context().Value(ExtraLogFieldsCtxKey).(*ExtraLogFields)
|
||||||
|
extra.Set(zap.String("uuid", id.String()))
|
||||||
|
|
||||||
return id.String(), true
|
return id.String(), true
|
||||||
|
|
||||||
case "http.request.body":
|
case "http.request.body":
|
||||||
if req.Body == nil {
|
if req.Body == nil {
|
||||||
return "", true
|
return "", true
|
||||||
|
|
Loading…
Reference in a new issue