Skip to content

Commit 19cd558

Browse files
authored
output trace in slog and override correlation header name (#1986)
Signed-off-by: Bob Callaway <[email protected]>
1 parent a0453d5 commit 19cd558

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

cmd/rekor-server/app/root.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"time"
2424

25+
"github.com/go-chi/chi/middleware"
2526
homedir "github.com/mitchellh/go-homedir"
2627
"github.com/sigstore/rekor/pkg/log"
2728
"github.com/spf13/cobra"
@@ -131,6 +132,9 @@ Memory and file-based signers should only be used for testing.`)
131132
rootCmd.PersistentFlags().Int("search_index.mysql.max_open_connections", 0, "maximum open connections")
132133
rootCmd.PersistentFlags().Int("search_index.mysql.max_idle_connections", 0, "maximum idle connections")
133134

135+
rootCmd.PersistentFlags().String("http-request-id-header-name", middleware.RequestIDHeader, "name of HTTP Request Header to use as request correlation ID")
136+
rootCmd.PersistentFlags().String("trace-string-prefix", "", "if set, this will be used to prefix the 'trace' field when outputting structured logs")
137+
134138
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
135139
log.Logger.Fatal(err)
136140
}

cmd/rekor-server/app/serve.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"time"
2222

2323
"cloud.google.com/go/profiler"
24+
"github.com/go-chi/chi/middleware"
2425
"github.com/go-openapi/loads"
2526
"github.com/prometheus/client_golang/prometheus/promhttp"
2627
"github.com/spf13/cobra"
@@ -63,7 +64,7 @@ var serveCmd = &cobra.Command{
6364
Long: `Starts a http server and serves the configured api`,
6465
Run: func(cmd *cobra.Command, args []string) {
6566
// Setup the logger to dev/prod
66-
log.ConfigureLogger(viper.GetString("log_type"))
67+
log.ConfigureLogger(viper.GetString("log_type"), viper.GetString("trace-string-prefix"))
6768

6869
// workaround for https://github.com/sigstore/rekor/issues/68
6970
// from https://github.com/golang/glog/commit/fca8c8854093a154ff1eb580aae10276ad6b1b5f
@@ -76,6 +77,9 @@ var serveCmd = &cobra.Command{
7677
}
7778
log.Logger.Infof("starting rekor-server @ %v", viStr)
7879

80+
// overrides the correlation ID printed in logs, if config is set
81+
middleware.RequestIDHeader = viper.GetString("http-request-id-header-name")
82+
7983
if viper.GetBool("gcp_cloud_profiling.enabled") {
8084
cfg := profiler.Config{
8185
Service: viper.GetString("gcp_cloud_profiling.service"),

pkg/log/log.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package log
1717

1818
import (
1919
"context"
20+
"fmt"
2021
"log"
2122

2223
"github.com/go-chi/chi/middleware"
@@ -27,11 +28,13 @@ import (
2728
// Logger set the default logger to development mode
2829
var Logger *zap.SugaredLogger
2930

31+
var traceStringPrefix string
32+
3033
func init() {
31-
ConfigureLogger("dev")
34+
ConfigureLogger("dev", "")
3235
}
3336

34-
func ConfigureLogger(logType string) {
37+
func ConfigureLogger(logType, traceStrPrefix string) {
3538
var cfg zap.Config
3639
if logType == "prod" {
3740
cfg = zap.NewProductionConfig()
@@ -51,6 +54,10 @@ func ConfigureLogger(logType string) {
5154
log.Fatalln("createLogger", err)
5255
}
5356
Logger = logger.Sugar()
57+
58+
if traceStrPrefix != "" {
59+
traceStringPrefix = traceStrPrefix
60+
}
5461
}
5562

5663
func encodeLevel() zapcore.LevelEncoder {
@@ -109,6 +116,9 @@ func ContextLogger(ctx context.Context) *zap.SugaredLogger {
109116
if ctxRequestID, ok := ctx.Value(middleware.RequestIDKey).(string); ok {
110117
requestID := operation{ctxRequestID}
111118
proposedLogger = proposedLogger.With(zap.Object("operation", requestID))
119+
if traceStringPrefix != "" {
120+
proposedLogger = proposedLogger.With(zap.String("trace", fmt.Sprintf("%s/%s", traceStringPrefix, ctxRequestID)))
121+
}
112122
}
113123
}
114124
return proposedLogger

0 commit comments

Comments
 (0)