-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cloudapi: propagate operation/external id
Signed-off-by: Lukas Zapletal <lzap+git@redhat.com>
- Loading branch information
Showing
7 changed files
with
168 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package common | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type ContextHook struct{} | ||
|
||
func (h *ContextHook) Levels() []logrus.Level { | ||
return []logrus.Level{ | ||
logrus.DebugLevel, | ||
logrus.InfoLevel, | ||
logrus.WarnLevel, | ||
logrus.ErrorLevel, | ||
logrus.FatalLevel, | ||
logrus.PanicLevel, | ||
} | ||
} | ||
|
||
func (h *ContextHook) Fire(e *logrus.Entry) error { | ||
if e.Context == nil { | ||
return nil | ||
} | ||
|
||
if val := e.Context.Value(operationIDKeyCtx); val != nil { | ||
e.Data["operation_id"] = val | ||
} | ||
if val := e.Context.Value(externalIDKeyCtx); val != nil { | ||
e.Data["external_id"] = val | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
const ExternalIDKey string = "externalID" | ||
const externalIDKeyCtx ctxKey = ctxKey(ExternalIDKey) | ||
|
||
// Extracts HTTP header X-External-Id and sets it as a request context value | ||
func ExternalIDMiddleware(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
eid := c.Request().Header.Get("X-External-Id") | ||
if eid == "" { | ||
return next(c) | ||
} | ||
|
||
eid = strings.TrimSuffix(eid, "\n") | ||
c.Set(ExternalIDKey, eid) | ||
|
||
ctx := c.Request().Context() | ||
ctx = context.WithValue(ctx, externalIDKeyCtx, eid) | ||
c.SetRequest(c.Request().WithContext(ctx)) | ||
|
||
return next(c) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package common | ||
|
||
import ( | ||
"github.com/labstack/echo/v4" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// Store context in request logger to propagate correlation ids | ||
func LoggerMiddleware(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
c.SetLogger(&EchoLogrusLogger{ | ||
Logger: logrus.StandardLogger(), | ||
Ctx: c.Request().Context(), | ||
}) | ||
|
||
return next(c) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters