github.com/mendersoftware/go-lib-micro@v0.0.0-20240304135804-e8e39c59b148/requestid/middleware.go (about) 1 // Copyright 2023 Northern.tech AS 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package requestid 15 16 import ( 17 "github.com/ant0ine/go-json-rest/rest" 18 "github.com/gin-gonic/gin" 19 "github.com/google/uuid" 20 21 "github.com/mendersoftware/go-lib-micro/log" 22 "github.com/mendersoftware/go-lib-micro/requestlog" 23 ) 24 25 const RequestIdHeader = "X-MEN-RequestID" 26 27 type MiddlewareOptions struct { 28 // GenerateRequestID decides whether a request ID should 29 // be generated when none exists. (default: true) 30 GenerateRequestID *bool 31 } 32 33 func NewMiddlewareOptions() *MiddlewareOptions { 34 return new(MiddlewareOptions) 35 } 36 37 func (opt *MiddlewareOptions) SetGenerateRequestID(gen bool) *MiddlewareOptions { 38 opt.GenerateRequestID = &gen 39 return opt 40 } 41 42 // Middleware provides requestid middleware for the gin-gonic framework. 43 func Middleware(opts ...*MiddlewareOptions) gin.HandlerFunc { 44 opt := NewMiddlewareOptions(). 45 SetGenerateRequestID(true) 46 for _, o := range opts { 47 if o == nil { 48 continue 49 } 50 if o.GenerateRequestID != nil { 51 opt.GenerateRequestID = o.GenerateRequestID 52 } 53 } 54 return func(c *gin.Context) { 55 ctx := c.Request.Context() 56 57 requestID := c.GetHeader(RequestIdHeader) 58 if requestID == "" && *opt.GenerateRequestID { 59 uid, _ := uuid.NewRandom() 60 requestID = uid.String() 61 } 62 ctx = WithContext(ctx, requestID) 63 64 logger := log.FromContext(ctx) 65 if logger != nil { 66 logger = logger.F(log.Ctx{"request_id": requestID}) 67 ctx = log.WithContext(ctx, logger) 68 } 69 c.Header(RequestIdHeader, requestID) 70 c.Request = c.Request.WithContext(ctx) 71 } 72 } 73 74 // RequestIdMiddleware sets the X-MEN-RequestID header if it's not present, and and adds the request id to the request's logger's context. 75 type RequestIdMiddleware struct { 76 } 77 78 // MiddlewareFunc makes RequestIdMiddleware implement the Middleware interface. 79 func (mw *RequestIdMiddleware) MiddlewareFunc(h rest.HandlerFunc) rest.HandlerFunc { 80 return func(w rest.ResponseWriter, r *rest.Request) { 81 logger := requestlog.GetRequestLogger(r) 82 83 reqId := r.Header.Get(RequestIdHeader) 84 if reqId == "" { 85 uid, _ := uuid.NewRandom() 86 reqId = uid.String() 87 } 88 89 r = SetReqId(r, reqId) 90 91 // enrich log context 92 if logger != nil { 93 logger = logger.F(log.Ctx{"request_id": reqId}) 94 r = requestlog.SetRequestLogger(r, logger) 95 } 96 97 //return the reuqest ID in response too, the client can log it 98 //for end-to-end req tracing 99 w.Header().Add(RequestIdHeader, reqId) 100 101 h(w, r) 102 } 103 }