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

Add slogAdapter #6405

Merged
merged 2 commits into from
Dec 9, 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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ require (
github.com/cespare/xxhash/v2 v2.3.0
github.com/google/go-cmp v0.6.0
github.com/sercand/kuberesolver/v5 v5.1.1
github.com/tjhop/slog-gokit v0.1.2
go.opentelemetry.io/collector/pdata v1.21.0
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
google.golang.org/protobuf v1.35.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,8 @@ github.com/thanos-io/promql-engine v0.0.0-20241203103240-2f49f80c7c68 h1:cChM/Fb
github.com/thanos-io/promql-engine v0.0.0-20241203103240-2f49f80c7c68/go.mod h1:wx0JlRZtsB2S10JYUgeg5GqLfMxw31SzArP+28yyE00=
github.com/thanos-io/thanos v0.37.1 h1:PuTMql3S/i5UWlBT0WbCDwwL6Kc6Jf7DLHt2rdj4ivY=
github.com/thanos-io/thanos v0.37.1/go.mod h1:5Ni7Uc1Bc8UCGOYmZ/2f/LVvDkZKNDdqDJZqjDFG+rI=
github.com/tjhop/slog-gokit v0.1.2 h1:pmQI4SvU9h4gA0vIQsdhJQSqQg4mOmsPykG2/PM3j1I=
github.com/tjhop/slog-gokit v0.1.2/go.mod h1:8fhlcp8C8ELbg3GCyKv06tgt4B5sDq2P1r2DQAu1HuM=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/uber/jaeger-client-go v2.30.0+incompatible h1:D6wyKGCecFaSRUpo8lCVbaOOb6ThwMmTEbhRwtKR97o=
github.com/uber/jaeger-client-go v2.30.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ var (
)

func TestRoundTrip(t *testing.T) {
t.Parallel()
s := httptest.NewServer(
middleware.AuthenticateUser.Wrap(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
2 changes: 0 additions & 2 deletions pkg/querier/tripperware/queryrange/split_by_interval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ func TestSplitByDay(t *testing.T) {
}

func Test_evaluateAtModifier(t *testing.T) {
t.Parallel()
const (
start, end = int64(1546300800), int64(1646300800)
)
Expand Down Expand Up @@ -386,7 +385,6 @@ func Test_evaluateAtModifier(t *testing.T) {
} {
tt := tt
t.Run(tt.in, func(t *testing.T) {
t.Parallel()
out, err := evaluateAtModifierFunction(tt.in, start, end)
if tt.expectedErrorCode != 0 {
require.Error(t, err)
Expand Down
6 changes: 4 additions & 2 deletions pkg/util/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func InitLogger(cfg *server.Config) {

// PrometheusLogger exposes Prometheus counters for each of go-kit's log levels.
type PrometheusLogger struct {
logger log.Logger
logger log.Logger
logLevel logging.Level
}

// NewPrometheusLogger creates a new instance of PrometheusLogger which exposes
Expand Down Expand Up @@ -92,7 +93,8 @@ func newPrometheusLoggerFrom(logger log.Logger, logLevel logging.Level, keyvals
logMessages.WithLabelValues(level.String())
}
return &PrometheusLogger{
logger: logger,
logger: logger,
logLevel: logLevel,
}
}

Expand Down
30 changes: 30 additions & 0 deletions pkg/util/log/slog_adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package log

import (
"log/slog"

"github.com/go-kit/log"
sloggk "github.com/tjhop/slog-gokit"
)

// GoKitLogToSlog convert go-kit/log to slog
// usage: logutil.GoKitLogToSlog(gokitLogger)
func GoKitLogToSlog(logger log.Logger) *slog.Logger {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should name this function to PrometheusLoggerToSlot and have another function for GoKitLogToSlog which takes an additional level parameter?

levelVar := slog.LevelVar{}
promLogger, ok := logger.(*PrometheusLogger)
if !ok {
levelVar.Set(slog.LevelDebug)
} else {
switch promLogger.logLevel.String() {
case "debug":
levelVar.Set(slog.LevelDebug)
case "info":
levelVar.Set(slog.LevelInfo)
case "warn":
levelVar.Set(slog.LevelWarn)
case "error":
levelVar.Set(slog.LevelError)
}
}
return slog.New(sloggk.NewGoKitHandler(logger, &levelVar))
}
33 changes: 33 additions & 0 deletions pkg/util/log/slog_adapter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package log

import (
"context"
"log/slog"
"testing"

"github.com/stretchr/testify/require"
"github.com/weaveworks/common/server"
)

func Test_SlogAdapter_LogLevel(t *testing.T) {
ctx := context.Background()
logLevels := []string{"debug", "info", "warn", "error"}
slogLevels := []slog.Level{slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError}

for i, lv := range logLevels {
cfg := &server.Config{}
require.NoError(t, cfg.LogLevel.Set(lv))
InitLogger(cfg)

slog := GoKitLogToSlog(Logger)
for j, slogLv := range slogLevels {
if i <= j {
t.Logf("[go-kit log level: %v, slog level: %v] slog should be enabled", lv, slogLv)
require.True(t, slog.Enabled(ctx, slogLv))
} else {
t.Logf("[go-kit log level: %v, slog level: %v] slog should be disabled", lv, slogLv)
require.False(t, slog.Enabled(ctx, slogLv))
}
}
}
}
29 changes: 29 additions & 0 deletions vendor/github.com/tjhop/slog-gokit/.goreleaser.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

201 changes: 201 additions & 0 deletions vendor/github.com/tjhop/slog-gokit/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/tjhop/slog-gokit/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading