-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enrich Dex logs with real IP and request ID (#3661)
Signed-off-by: m.nabokikh <[email protected]> Signed-off-by: Maksim Nabokikh <[email protected]> Co-authored-by: Márk Sági-Kazár <[email protected]>
- Loading branch information
1 parent
6ceb265
commit 2256607
Showing
11 changed files
with
333 additions
and
187 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
"strings" | ||
|
||
"github.com/dexidp/dex/server" | ||
) | ||
|
||
var logFormats = []string{"json", "text"} | ||
|
||
func newLogger(level slog.Level, format string) (*slog.Logger, error) { | ||
var handler slog.Handler | ||
switch strings.ToLower(format) { | ||
case "", "text": | ||
handler = slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{ | ||
Level: level, | ||
}) | ||
case "json": | ||
handler = slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{ | ||
Level: level, | ||
}) | ||
default: | ||
return nil, fmt.Errorf("log format is not one of the supported values (%s): %s", strings.Join(logFormats, ", "), format) | ||
} | ||
|
||
return slog.New(newRequestContextHandler(handler)), nil | ||
} | ||
|
||
var _ slog.Handler = requestContextHandler{} | ||
|
||
type requestContextHandler struct { | ||
handler slog.Handler | ||
} | ||
|
||
func newRequestContextHandler(handler slog.Handler) slog.Handler { | ||
return requestContextHandler{ | ||
handler: handler, | ||
} | ||
} | ||
|
||
func (h requestContextHandler) Enabled(ctx context.Context, level slog.Level) bool { | ||
return h.handler.Enabled(ctx, level) | ||
} | ||
|
||
func (h requestContextHandler) Handle(ctx context.Context, record slog.Record) error { | ||
if v, ok := ctx.Value(server.RequestKeyRemoteIP).(string); ok { | ||
record.AddAttrs(slog.String(string(server.RequestKeyRemoteIP), v)) | ||
} | ||
|
||
if v, ok := ctx.Value(server.RequestKeyRequestID).(string); ok { | ||
record.AddAttrs(slog.String(string(server.RequestKeyRequestID), v)) | ||
} | ||
|
||
return h.handler.Handle(ctx, record) | ||
} | ||
|
||
func (h requestContextHandler) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
return requestContextHandler{h.handler.WithAttrs(attrs)} | ||
} | ||
|
||
func (h requestContextHandler) WithGroup(name string) slog.Handler { | ||
return h.handler.WithGroup(name) | ||
} |
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
Oops, something went wrong.