Skip to content

Commit

Permalink
Add slogAdapter (#6405)
Browse files Browse the repository at this point in the history
* Add slogAdapter

Signed-off-by: SungJin1212 <[email protected]>

* Fix test race condition due to EnableExperimentalFunctions

Signed-off-by: SungJin1212 <[email protected]>

---------

Signed-off-by: SungJin1212 <[email protected]>
  • Loading branch information
SungJin1212 authored Dec 9, 2024
1 parent c9e3d5e commit 7e444e7
Show file tree
Hide file tree
Showing 14 changed files with 532 additions and 5 deletions.
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 {
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

0 comments on commit 7e444e7

Please sign in to comment.