Merge pull request #471 from spegel-org/fix/logging-handler

Fix handler key in request logging
This commit is contained in:
Philip Laine 2024-05-13 22:36:18 +02:00 committed by GitHub
commit b25b142a8a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 22 deletions

View File

@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- [#460](https://github.com/spegel-org/spegel/pull/460) Fix environment variable for http-bootstrap-addr flag. - [#460](https://github.com/spegel-org/spegel/pull/460) Fix environment variable for http-bootstrap-addr flag.
- [#471](https://github.com/spegel-org/spegel/pull/471) Fix handler key in request logging.
### Security ### Security

View File

@ -107,42 +107,46 @@ func (r *Registry) Server(addr string) *http.Server {
func (r *Registry) handle(rw mux.ResponseWriter, req *http.Request) { func (r *Registry) handle(rw mux.ResponseWriter, req *http.Request) {
start := time.Now() start := time.Now()
handler := req.URL.Path handler := ""
if strings.HasPrefix(handler, "/v2") { path := req.URL.Path
handler = "/v2/*" if strings.HasPrefix(path, "/v2") {
path = "/v2/*"
} }
defer func() { defer func() {
latency := time.Since(start) latency := time.Since(start)
statusCode := strconv.FormatInt(int64(rw.Status()), 10) statusCode := strconv.FormatInt(int64(rw.Status()), 10)
metrics.HttpRequestsInflight.WithLabelValues(handler).Add(-1) metrics.HttpRequestsInflight.WithLabelValues(path).Add(-1)
metrics.HttpRequestDurHistogram.WithLabelValues(handler, req.Method, statusCode).Observe(latency.Seconds()) metrics.HttpRequestDurHistogram.WithLabelValues(path, req.Method, statusCode).Observe(latency.Seconds())
metrics.HttpResponseSizeHistogram.WithLabelValues(handler, req.Method, statusCode).Observe(float64(rw.Size())) metrics.HttpResponseSizeHistogram.WithLabelValues(path, req.Method, statusCode).Observe(float64(rw.Size()))
// Ignore logging requests to healthz to reduce log noise // Ignore logging requests to healthz to reduce log noise
if req.URL.Path == "/healthz" { if req.URL.Path == "/healthz" {
return return
} }
// Logging kvs := []interface{}{
path := req.URL.Path "path", req.URL.Path,
kvs := []interface{}{"path", path, "status", rw.Status(), "method", req.Method, "latency", latency.String(), "ip", getClientIP(req)} "status", rw.Status(),
"method", req.Method,
"latency", latency.String(),
"ip", getClientIP(req),
"handler", handler,
}
if rw.Status() >= 200 && rw.Status() < 300 { if rw.Status() >= 200 && rw.Status() < 300 {
r.log.Info("", kvs...) r.log.Info("", kvs...)
return return
} }
r.log.Error(rw.Error(), "", kvs...) r.log.Error(rw.Error(), "", kvs...)
}() }()
metrics.HttpRequestsInflight.WithLabelValues(path).Add(1)
metrics.HttpRequestsInflight.WithLabelValues(handler).Add(1)
if req.URL.Path == "/healthz" && req.Method == http.MethodGet { if req.URL.Path == "/healthz" && req.Method == http.MethodGet {
r.readyHandler(rw, req) r.readyHandler(rw, req)
return return
} }
if strings.HasPrefix(req.URL.Path, "/v2") && (req.Method == http.MethodGet || req.Method == http.MethodHead) { if strings.HasPrefix(req.URL.Path, "/v2") && (req.Method == http.MethodGet || req.Method == http.MethodHead) {
r.registryHandler(rw, req) handler = r.registryHandler(rw, req)
return return
} }
rw.WriteHeader(http.StatusNotFound) rw.WriteHeader(http.StatusNotFound)
@ -160,10 +164,10 @@ func (r *Registry) readyHandler(rw mux.ResponseWriter, req *http.Request) {
} }
} }
func (r *Registry) registryHandler(rw mux.ResponseWriter, req *http.Request) { func (r *Registry) registryHandler(rw mux.ResponseWriter, req *http.Request) string {
// Quickly return 200 for /v2 to indicate that registry supports v2. // Quickly return 200 for /v2 to indicate that registry supports v2.
if path.Clean(req.URL.Path) == "/v2" { if path.Clean(req.URL.Path) == "/v2" {
return return "registry"
} }
// Parse out path components from request. // Parse out path components from request.
@ -171,7 +175,7 @@ func (r *Registry) registryHandler(rw mux.ResponseWriter, req *http.Request) {
ref, dgst, refType, err := parsePathComponents(registryName, req.URL.Path) ref, dgst, refType, err := parsePathComponents(registryName, req.URL.Path)
if err != nil { if err != nil {
rw.WriteError(http.StatusNotFound, err) rw.WriteError(http.StatusNotFound, err)
return return ""
} }
// Check if latest tag should be resolved // Check if latest tag should be resolved
@ -179,7 +183,7 @@ func (r *Registry) registryHandler(rw mux.ResponseWriter, req *http.Request) {
_, tag, _ := strings.Cut(ref, ":") _, tag, _ := strings.Cut(ref, ":")
if tag == "latest" { if tag == "latest" {
rw.WriteHeader(http.StatusNotFound) rw.WriteHeader(http.StatusNotFound)
return return ""
} }
} }
@ -201,7 +205,7 @@ func (r *Registry) registryHandler(rw mux.ResponseWriter, req *http.Request) {
cacheType = "miss" cacheType = "miss"
} }
metrics.MirrorRequestsTotal.WithLabelValues(registryName, cacheType, sourceType).Inc() metrics.MirrorRequestsTotal.WithLabelValues(registryName, cacheType, sourceType).Inc()
return return "mirror"
} }
// Serve registry endpoints. // Serve registry endpoints.
@ -209,19 +213,22 @@ func (r *Registry) registryHandler(rw mux.ResponseWriter, req *http.Request) {
dgst, err = r.ociClient.Resolve(req.Context(), ref) dgst, err = r.ociClient.Resolve(req.Context(), ref)
if err != nil { if err != nil {
rw.WriteError(http.StatusNotFound, err) rw.WriteError(http.StatusNotFound, err)
return return ""
} }
} }
switch refType { switch refType {
case referenceTypeManifest: case referenceTypeManifest:
r.handleManifest(rw, req, dgst) r.handleManifest(rw, req, dgst)
return "manifest"
case referenceTypeBlob: case referenceTypeBlob:
r.handleBlob(rw, req, dgst) r.handleBlob(rw, req, dgst)
default: return "blob"
// If nothing matches return 404.
rw.WriteHeader(http.StatusNotFound)
} }
// If nothing matches return 404.
rw.WriteHeader(http.StatusNotFound)
return ""
} }
func (r *Registry) handleMirror(rw mux.ResponseWriter, req *http.Request, key string) { func (r *Registry) handleMirror(rw mux.ResponseWriter, req *http.Request, key string) {