Skip to content

Commit

Permalink
feat(cloudrequestlog): use slog.Level for config values
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
odsod committed Oct 7, 2024
1 parent c26c347 commit f454180
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 43 deletions.
76 changes: 38 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 4 additions & 3 deletions cloudrequestlog/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package cloudrequestlog

import (
"go.uber.org/zap/zapcore"
"log/slog"

"google.golang.org/grpc/codes"
)

Expand All @@ -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
}
4 changes: 2 additions & 2 deletions cloudrequestlog/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit f454180

Please sign in to comment.