Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

output trace in slog and override correlation header name #1986

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/rekor-server/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"time"

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

rootCmd.PersistentFlags().String("http-request-id-header-name", middleware.RequestIDHeader, "name of HTTP Request Header to use as request correlation ID")
rootCmd.PersistentFlags().String("trace-string-prefix", "", "if set, this will be used to prefix the 'trace' field when outputting structured logs")

if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
log.Logger.Fatal(err)
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/rekor-server/app/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

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

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

// overrides the correlation ID printed in logs, if config is set
middleware.RequestIDHeader = viper.GetString("http-request-id-header-name")

if viper.GetBool("gcp_cloud_profiling.enabled") {
cfg := profiler.Config{
Service: viper.GetString("gcp_cloud_profiling.service"),
Expand Down
14 changes: 12 additions & 2 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package log

import (
"context"
"fmt"
"log"

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

var traceStringPrefix string

func init() {
ConfigureLogger("dev")
ConfigureLogger("dev", "")
}

func ConfigureLogger(logType string) {
func ConfigureLogger(logType, traceStrPrefix string) {
var cfg zap.Config
if logType == "prod" {
cfg = zap.NewProductionConfig()
Expand All @@ -51,6 +54,10 @@ func ConfigureLogger(logType string) {
log.Fatalln("createLogger", err)
}
Logger = logger.Sugar()

if traceStrPrefix != "" {
traceStringPrefix = traceStrPrefix
}
}

func encodeLevel() zapcore.LevelEncoder {
Expand Down Expand Up @@ -109,6 +116,9 @@ func ContextLogger(ctx context.Context) *zap.SugaredLogger {
if ctxRequestID, ok := ctx.Value(middleware.RequestIDKey).(string); ok {
requestID := operation{ctxRequestID}
proposedLogger = proposedLogger.With(zap.Object("operation", requestID))
if traceStringPrefix != "" {
proposedLogger = proposedLogger.With(zap.String("trace", fmt.Sprintf("%s/%s", traceStringPrefix, ctxRequestID)))
}
}
}
return proposedLogger
Expand Down
Loading