Skip to content

Commit 3e43a18

Browse files
authored
FFM-12009 Extract AppID header and log it out (#364)
* FFM-112009 **What** - Extracts the the Harness-SDK-ApplicationID header from the request and sets it in the request context - Extracts the appID from the context and logs it in the logging middleware and the standard logger. **Why** - Makes it easier to link requests to specific applications **Testing** - Manually tested locally
1 parent dcc2667 commit 3e43a18

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

log/log.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,30 @@ func ExtractRequestValuesFromContext(ctx context.Context) []interface{} {
231231
values = append(values, reqID)
232232
}
233233

234+
appID := getAppID(ctx)
235+
if appID != "" {
236+
values = append(values, "appID")
237+
values = append(values, appID)
238+
}
239+
234240
return values
235241
}
236242

237243
type contextKey string
238244

239245
// RequestIDKey is the key we associate with the requestID that we set in the request context
240-
const RequestIDKey contextKey = "requestID"
246+
const (
247+
RequestIDKey contextKey = "requestID"
248+
AppIDKey contextKey = "appID"
249+
)
241250

242251
// getRequestID extracts the requestID value from the context if it exists.
243252
func getRequestID(ctx context.Context) string {
244253
requestID, _ := ctx.Value(RequestIDKey).(string)
245254
return requestID
246255
}
256+
257+
func getAppID(ctx context.Context) string {
258+
appID, _ := ctx.Value(AppIDKey).(string)
259+
return appID
260+
}

middleware/middleware.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ type keyLookUp interface {
4040
func NewEchoLoggingMiddleware(l log.Logger) echo.MiddlewareFunc {
4141
return middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
4242
LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
43-
l.Info("request", "component", "LoggingMiddleware", "method", v.Method, "path", v.RoutePath, "status", v.Status, "took", v.Latency.String(), "reqID", v.RequestID)
43+
appID := c.Request().Context().Value(log.AppIDKey)
44+
l.Info("request", "component", "LoggingMiddleware", "method", v.Method, "path", v.RoutePath, "status", v.Status, "took", v.Latency.String(), "reqID", v.RequestID, "appID", appID)
4445
return nil
4546
},
4647
LogLatency: true,
@@ -107,9 +108,10 @@ func isKeyInCache(ctx context.Context, logger log.Logger, repo keyLookUp, claims
107108
return exists
108109
}
109110

110-
// NewEchoRequestIDMiddleware returns an echo middleware that either uses a
111-
// provided requestID from the header or generates one and adds it to the request
112-
// context.
111+
const harnessSDKAppIDHeader = "Harness-SDK-ApplicationID"
112+
113+
// NewEchoRequestIDMiddleware extracts X-Request_Id and Harness-SDK-ApplicationID
114+
// headers from the request and adds them to the context.
113115
func NewEchoRequestIDMiddleware() echo.MiddlewareFunc {
114116
return func(next echo.HandlerFunc) echo.HandlerFunc {
115117
return func(c echo.Context) error {
@@ -121,10 +123,15 @@ func NewEchoRequestIDMiddleware() echo.MiddlewareFunc {
121123
requestUUID, _ := uuid.NewRandom()
122124
reqID = requestUUID.String()
123125
}
124-
125126
req = req.WithContext(context.WithValue(req.Context(), log.RequestIDKey, reqID))
126-
c.SetRequest(req)
127127

128+
appID := req.Header.Get(harnessSDKAppIDHeader)
129+
if appID == "" {
130+
appID = "unknown"
131+
}
132+
req = req.WithContext(context.WithValue(req.Context(), log.AppIDKey, appID))
133+
134+
c.SetRequest(req)
128135
resp.Header().Set(echo.HeaderXRequestID, reqID)
129136
return next(c)
130137
}

0 commit comments

Comments
 (0)