summaryrefslogtreecommitdiff
path: root/pkg/api/server/handler_rid.go
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2021-09-12 02:07:36 -0400
committerGitHub <noreply@github.com>2021-09-12 02:07:36 -0400
commitf785d8d1940a6cf3ac76f4c333d157bd9d4c76b7 (patch)
tree215fcf8cac010819b632cc4c564025413b93728c /pkg/api/server/handler_rid.go
parent72662f790bad0a3dc26a6a1498b6daa936702bb6 (diff)
parentdeaf9692432bb6a9353fe56cecb6cddf0401a78c (diff)
downloadpodman-f785d8d1940a6cf3ac76f4c333d157bd9d4c76b7.tar.gz
podman-f785d8d1940a6cf3ac76f4c333d157bd9d4c76b7.tar.bz2
podman-f785d8d1940a6cf3ac76f4c333d157bd9d4c76b7.zip
Merge pull request #11517 from jwhonce/issues/10053
Refactor API server emphasis on logging
Diffstat (limited to 'pkg/api/server/handler_rid.go')
-rw-r--r--pkg/api/server/handler_rid.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/pkg/api/server/handler_rid.go b/pkg/api/server/handler_rid.go
new file mode 100644
index 000000000..b624b99a6
--- /dev/null
+++ b/pkg/api/server/handler_rid.go
@@ -0,0 +1,34 @@
+package server
+
+import (
+ "fmt"
+ "net/http"
+
+ "github.com/containers/podman/v3/pkg/api/types"
+ "github.com/google/uuid"
+ "github.com/gorilla/handlers"
+ "github.com/gorilla/mux"
+ "github.com/sirupsen/logrus"
+)
+
+// referenceIDHandler adds X-Reference-Id Header allowing event correlation
+// and Apache style request logging
+func referenceIDHandler() mux.MiddlewareFunc {
+ return func(h http.Handler) http.Handler {
+ return handlers.CombinedLoggingHandler(logrus.StandardLogger().Out,
+ http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ rid := r.Header.Get("X-Reference-Id")
+ if rid == "" {
+ if c := r.Context().Value(types.ConnKey); c == nil {
+ rid = uuid.New().String()
+ } else {
+ rid = fmt.Sprintf("%p", c)
+ }
+ }
+
+ r.Header.Set("X-Reference-Id", rid)
+ w.Header().Set("X-Reference-Id", rid)
+ h.ServeHTTP(w, r)
+ }))
+ }
+}