From c02e66ba4b2a44ed07881fdc5076517ce7e1057f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20S=C3=B6derlund?= Date: Mon, 7 Oct 2024 13:26:27 +0200 Subject: [PATCH] feat(cloudrequestlog): use slog.Level for config values The string values are equivalent (debug/info/warn/error) across zap and slog, so although this is technically a breaking change, the risk of breakage is small, and the migration if it happens is simple. --- README.md | 76 +++++++++++++++++------------------ cloudrequestlog/config.go | 7 ++-- cloudrequestlog/middleware.go | 4 +- 3 files changed, 44 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index baf281bf..28c87855 100644 --- a/README.md +++ b/README.md @@ -86,44 +86,44 @@ Usage of grpc-server: Runtime configuration of grpc-server: -CONFIG ENV TYPE DEFAULT ON GCE -cloudrunner PORT int 8080 -cloudrunner K_SERVICE string -cloudrunner K_REVISION string -cloudrunner K_CONFIGURATION string -cloudrunner CLOUD_RUN_JOB string -cloudrunner CLOUD_RUN_EXECUTION string -cloudrunner CLOUD_RUN_TASK_INDEX int -cloudrunner CLOUD_RUN_TASK_ATTEMPT int -cloudrunner CLOUD_RUN_TASK_COUNT int -cloudrunner GOOGLE_CLOUD_PROJECT string -cloudrunner RUNTIME_SERVICEACCOUNT string -cloudrunner SERVICE_VERSION string -cloudrunner LOGGER_DEVELOPMENT bool true false -cloudrunner LOGGER_LEVEL zapcore.Level debug info -cloudrunner LOGGER_REPORTERRORS bool true -cloudrunner PROFILER_ENABLED bool true -cloudrunner PROFILER_MUTEXPROFILING bool -cloudrunner PROFILER_ALLOCFORCEGC bool true -cloudrunner TRACEEXPORTER_ENABLED bool true -cloudrunner TRACEEXPORTER_TIMEOUT time.Duration 10s -cloudrunner TRACEEXPORTER_SAMPLEPROBABILITY float64 0.01 -cloudrunner METRICEXPORTER_ENABLED bool false -cloudrunner METRICEXPORTER_INTERVAL time.Duration 60s -cloudrunner METRICEXPORTER_RUNTIMEINSTRUMENTATION bool true -cloudrunner METRICEXPORTER_HOSTINSTRUMENTATION bool true -cloudrunner METRICEXPORTER_OPENCENSUSPRODUCER bool false -cloudrunner SERVER_TIMEOUT time.Duration 290s -cloudrunner CLIENT_TIMEOUT time.Duration 10s -cloudrunner CLIENT_RETRY_ENABLED bool true -cloudrunner CLIENT_RETRY_INITIALBACKOFF time.Duration 200ms -cloudrunner CLIENT_RETRY_MAXBACKOFF time.Duration 60s -cloudrunner CLIENT_RETRY_MAXATTEMPTS int 5 -cloudrunner CLIENT_RETRY_BACKOFFMULTIPLIER float64 2 -cloudrunner CLIENT_RETRY_RETRYABLESTATUSCODES []codes.Code Unavailable,Unknown -cloudrunner REQUESTLOGGER_MESSAGESIZELIMIT int 1024 -cloudrunner REQUESTLOGGER_CODETOLEVEL map[codes.Code]zapcore.Level -cloudrunner REQUESTLOGGER_STATUSTOLEVEL map[int]zapcore.Level +CONFIG ENV TYPE DEFAULT ON GCE +cloudrunner PORT int 8080 +cloudrunner K_SERVICE string +cloudrunner K_REVISION string +cloudrunner K_CONFIGURATION string +cloudrunner CLOUD_RUN_JOB string +cloudrunner CLOUD_RUN_EXECUTION string +cloudrunner CLOUD_RUN_TASK_INDEX int +cloudrunner CLOUD_RUN_TASK_ATTEMPT int +cloudrunner CLOUD_RUN_TASK_COUNT int +cloudrunner GOOGLE_CLOUD_PROJECT string +cloudrunner RUNTIME_SERVICEACCOUNT string +cloudrunner SERVICE_VERSION string +cloudrunner LOGGER_DEVELOPMENT bool true false +cloudrunner LOGGER_LEVEL zapcore.Level debug info +cloudrunner LOGGER_REPORTERRORS bool true +cloudrunner PROFILER_ENABLED bool true +cloudrunner PROFILER_MUTEXPROFILING bool +cloudrunner PROFILER_ALLOCFORCEGC bool true +cloudrunner TRACEEXPORTER_ENABLED bool true +cloudrunner TRACEEXPORTER_TIMEOUT time.Duration 10s +cloudrunner TRACEEXPORTER_SAMPLEPROBABILITY float64 0.01 +cloudrunner METRICEXPORTER_ENABLED bool false +cloudrunner METRICEXPORTER_INTERVAL time.Duration 60s +cloudrunner METRICEXPORTER_RUNTIMEINSTRUMENTATION bool true +cloudrunner METRICEXPORTER_HOSTINSTRUMENTATION bool true +cloudrunner METRICEXPORTER_OPENCENSUSPRODUCER bool false +cloudrunner SERVER_TIMEOUT time.Duration 290s +cloudrunner CLIENT_TIMEOUT time.Duration 10s +cloudrunner CLIENT_RETRY_ENABLED bool true +cloudrunner CLIENT_RETRY_INITIALBACKOFF time.Duration 200ms +cloudrunner CLIENT_RETRY_MAXBACKOFF time.Duration 60s +cloudrunner CLIENT_RETRY_MAXATTEMPTS int 5 +cloudrunner CLIENT_RETRY_BACKOFFMULTIPLIER float64 2 +cloudrunner CLIENT_RETRY_RETRYABLESTATUSCODES []codes.Code Unavailable,Unknown +cloudrunner REQUESTLOGGER_MESSAGESIZELIMIT int 1024 +cloudrunner REQUESTLOGGER_CODETOLEVEL map[codes.Code]slog.Level +cloudrunner REQUESTLOGGER_STATUSTOLEVEL map[int]slog.Level Build-time configuration of grpc-server: diff --git a/cloudrequestlog/config.go b/cloudrequestlog/config.go index cc38c41f..cbbf1cd3 100644 --- a/cloudrequestlog/config.go +++ b/cloudrequestlog/config.go @@ -1,7 +1,8 @@ package cloudrequestlog import ( - "go.uber.org/zap/zapcore" + "log/slog" + "google.golang.org/grpc/codes" ) @@ -12,7 +13,7 @@ type Config struct { // Default value, 0, means that no messages will be truncated. MessageSizeLimit int `onGCE:"1024"` // CodeToLevel enables overriding the default gRPC code to level conversion. - CodeToLevel map[codes.Code]zapcore.Level + CodeToLevel map[codes.Code]slog.Level // StatusToLevel enables overriding the default HTTP status code to level conversion. - StatusToLevel map[int]zapcore.Level + StatusToLevel map[int]slog.Level } diff --git a/cloudrequestlog/middleware.go b/cloudrequestlog/middleware.go index a1dbf4ef..623661ac 100644 --- a/cloudrequestlog/middleware.go +++ b/cloudrequestlog/middleware.go @@ -254,14 +254,14 @@ func (l *Middleware) applyMessageTransform(message proto.Message) proto.Message func (l *Middleware) codeToLevel(code codes.Code) zapcore.Level { if level, ok := l.Config.CodeToLevel[code]; ok { - return level + return slogToLevel(level) } return CodeToLevel(code) } func (l *Middleware) statusToLevel(status int) zapcore.Level { if level, ok := l.Config.StatusToLevel[status]; ok { - return level + return slogToLevel(level) } switch { case status < http.StatusBadRequest: