diff options
| author | Aleksey Kosov <rusyak777@list.ru> | 2025-05-21 17:57:39 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-21 07:57:39 -0700 |
| commit | 5182d46e22d2458b16f1f2fb0358f6b5f3e18b5d (patch) | |
| tree | 1f95adace7c8954512150f205005a347aff3653b /weed/server | |
| parent | 140b7a7402109a55072458e42a32bc1ef4a608a9 (diff) | |
| download | seaweedfs-5182d46e22d2458b16f1f2fb0358f6b5f3e18b5d.tar.xz seaweedfs-5182d46e22d2458b16f1f2fb0358f6b5f3e18b5d.zip | |
Added middleware for processing request_id grpc and http requests (#6805)
Diffstat (limited to 'weed/server')
| -rw-r--r-- | weed/server/common.go | 21 | ||||
| -rw-r--r-- | weed/server/filer_server.go | 8 | ||||
| -rw-r--r-- | weed/server/master_server.go | 24 | ||||
| -rw-r--r-- | weed/server/volume_server.go | 10 |
4 files changed, 42 insertions, 21 deletions
diff --git a/weed/server/common.go b/weed/server/common.go index 5dad9d81b..516f8bf1c 100644 --- a/weed/server/common.go +++ b/weed/server/common.go @@ -3,9 +3,12 @@ package weed_server import ( "bufio" "bytes" + "context" "encoding/json" "errors" "fmt" + "github.com/google/uuid" + "google.golang.org/grpc/metadata" "io" "io/fs" "mime/multipart" @@ -421,3 +424,21 @@ func ProcessRangeRequest(r *http.Request, w http.ResponseWriter, totalSize int64 } return nil } + +func requestIDMiddleware(h http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + reqID := r.Header.Get(util.RequestIdHttpHeader) + if reqID == "" { + reqID = uuid.New().String() + } + + ctx := context.WithValue(r.Context(), util.RequestIDKey, reqID) + ctx = metadata.NewOutgoingContext(ctx, + metadata.New(map[string]string{ + util.RequestIDKey: reqID, + })) + + w.Header().Set(util.RequestIdHttpHeader, reqID) + h(w, r.WithContext(ctx)) + } +} diff --git a/weed/server/filer_server.go b/weed/server/filer_server.go index 090c795fa..57af97d84 100644 --- a/weed/server/filer_server.go +++ b/weed/server/filer_server.go @@ -189,13 +189,13 @@ func NewFilerServer(defaultMux, readonlyMux *http.ServeMux, option *FilerOption) handleStaticResources(defaultMux) if !option.DisableHttp { - defaultMux.HandleFunc("/healthz", fs.filerHealthzHandler) - defaultMux.HandleFunc("/", fs.filerGuard.WhiteList(fs.filerHandler)) + defaultMux.HandleFunc("/healthz", requestIDMiddleware(fs.filerHealthzHandler)) + defaultMux.HandleFunc("/", fs.filerGuard.WhiteList(requestIDMiddleware(fs.filerHandler))) } if defaultMux != readonlyMux { handleStaticResources(readonlyMux) - readonlyMux.HandleFunc("/healthz", fs.filerHealthzHandler) - readonlyMux.HandleFunc("/", fs.filerGuard.WhiteList(fs.readonlyFilerHandler)) + readonlyMux.HandleFunc("/healthz", requestIDMiddleware(fs.filerHealthzHandler)) + readonlyMux.HandleFunc("/", fs.filerGuard.WhiteList(requestIDMiddleware(fs.readonlyFilerHandler))) } existingNodes := fs.filer.ListExistingPeerUpdates(context.Background()) diff --git a/weed/server/master_server.go b/weed/server/master_server.go index 8621708d2..6569fdbd4 100644 --- a/weed/server/master_server.go +++ b/weed/server/master_server.go @@ -134,24 +134,24 @@ func NewMasterServer(r *mux.Router, option *MasterOption, peers map[string]pb.Se ms.guard = security.NewGuard(append(ms.option.WhiteList, whiteList...), signingKey, expiresAfterSec, readSigningKey, readExpiresAfterSec) handleStaticResources2(r) - r.HandleFunc("/", ms.proxyToLeader(ms.uiStatusHandler)) - r.HandleFunc("/ui/index.html", ms.uiStatusHandler) + r.HandleFunc("/", ms.proxyToLeader(requestIDMiddleware(ms.uiStatusHandler))) + r.HandleFunc("/ui/index.html", requestIDMiddleware(ms.uiStatusHandler)) if !ms.option.DisableHttp { - r.HandleFunc("/dir/assign", ms.proxyToLeader(ms.guard.WhiteList(ms.dirAssignHandler))) - r.HandleFunc("/dir/lookup", ms.guard.WhiteList(ms.dirLookupHandler)) - r.HandleFunc("/dir/status", ms.proxyToLeader(ms.guard.WhiteList(ms.dirStatusHandler))) - r.HandleFunc("/col/delete", ms.proxyToLeader(ms.guard.WhiteList(ms.collectionDeleteHandler))) - r.HandleFunc("/vol/grow", ms.proxyToLeader(ms.guard.WhiteList(ms.volumeGrowHandler))) - r.HandleFunc("/vol/status", ms.proxyToLeader(ms.guard.WhiteList(ms.volumeStatusHandler))) - r.HandleFunc("/vol/vacuum", ms.proxyToLeader(ms.guard.WhiteList(ms.volumeVacuumHandler))) - r.HandleFunc("/submit", ms.guard.WhiteList(ms.submitFromMasterServerHandler)) - r.HandleFunc("/collection/info", ms.guard.WhiteList(ms.collectionInfoHandler)) + r.HandleFunc("/dir/assign", ms.proxyToLeader(ms.guard.WhiteList(requestIDMiddleware(ms.dirAssignHandler)))) + r.HandleFunc("/dir/lookup", ms.guard.WhiteList(requestIDMiddleware(ms.dirLookupHandler))) + r.HandleFunc("/dir/status", ms.proxyToLeader(ms.guard.WhiteList(requestIDMiddleware(ms.dirStatusHandler)))) + r.HandleFunc("/col/delete", ms.proxyToLeader(ms.guard.WhiteList(requestIDMiddleware(ms.collectionDeleteHandler)))) + r.HandleFunc("/vol/grow", ms.proxyToLeader(ms.guard.WhiteList(requestIDMiddleware(ms.volumeGrowHandler)))) + r.HandleFunc("/vol/status", ms.proxyToLeader(ms.guard.WhiteList(requestIDMiddleware(ms.volumeStatusHandler)))) + r.HandleFunc("/vol/vacuum", ms.proxyToLeader(ms.guard.WhiteList(requestIDMiddleware(ms.volumeVacuumHandler)))) + r.HandleFunc("/submit", ms.guard.WhiteList(requestIDMiddleware(ms.submitFromMasterServerHandler))) + r.HandleFunc("/collection/info", ms.guard.WhiteList(requestIDMiddleware(ms.collectionInfoHandler))) /* r.HandleFunc("/stats/health", ms.guard.WhiteList(statsHealthHandler)) r.HandleFunc("/stats/counter", ms.guard.WhiteList(statsCounterHandler)) r.HandleFunc("/stats/memory", ms.guard.WhiteList(statsMemoryHandler)) */ - r.HandleFunc("/{fileId}", ms.redirectHandler) + r.HandleFunc("/{fileId}", requestIDMiddleware(ms.redirectHandler)) } ms.Topo.StartRefreshWritableVolumes( diff --git a/weed/server/volume_server.go b/weed/server/volume_server.go index a3f072eb7..5c5ebc49a 100644 --- a/weed/server/volume_server.go +++ b/weed/server/volume_server.go @@ -115,22 +115,22 @@ func NewVolumeServer(adminMux, publicMux *http.ServeMux, ip string, vs.guard = security.NewGuard(whiteList, signingKey, expiresAfterSec, readSigningKey, readExpiresAfterSec) handleStaticResources(adminMux) - adminMux.HandleFunc("/status", vs.statusHandler) - adminMux.HandleFunc("/healthz", vs.healthzHandler) + adminMux.HandleFunc("/status", requestIDMiddleware(vs.statusHandler)) + adminMux.HandleFunc("/healthz", requestIDMiddleware(vs.healthzHandler)) if signingKey == "" || enableUiAccess { // only expose the volume server details for safe environments - adminMux.HandleFunc("/ui/index.html", vs.uiStatusHandler) + adminMux.HandleFunc("/ui/index.html", requestIDMiddleware(vs.uiStatusHandler)) /* adminMux.HandleFunc("/stats/counter", vs.guard.WhiteList(statsCounterHandler)) adminMux.HandleFunc("/stats/memory", vs.guard.WhiteList(statsMemoryHandler)) adminMux.HandleFunc("/stats/disk", vs.guard.WhiteList(vs.statsDiskHandler)) */ } - adminMux.HandleFunc("/", vs.privateStoreHandler) + adminMux.HandleFunc("/", requestIDMiddleware(vs.privateStoreHandler)) if publicMux != adminMux { // separated admin and public port handleStaticResources(publicMux) - publicMux.HandleFunc("/", vs.publicReadOnlyHandler) + publicMux.HandleFunc("/", requestIDMiddleware(vs.publicReadOnlyHandler)) } go vs.heartbeat() |
