Skip to content

Commit

Permalink
Add slogAdapter
Browse files Browse the repository at this point in the history
Signed-off-by: SungJin1212 <[email protected]>
  • Loading branch information
SungJin1212 committed Dec 6, 2024
1 parent 7191ecb commit 6e8e031
Show file tree
Hide file tree
Showing 13 changed files with 534 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* [FEATURE] Chunk Cache: Support multi level cache and add metrics. #6249
* [FEATURE] Distributor: Accept multiple HA Tracker pairs in the same request. #6256
* [FEATURE] Ruler: Add support for per-user external labels #6340
* [ENHANCEMENT] Log: Add a slogAdapter to upgrade Prometheus v3.0.0. #6405
* [ENHANCEMENT] Store Gateway: Add a hedged request to reduce the tail latency. #6388
* [ENHANCEMENT] Ingester: Add metrics to track succeed/failed native histograms. #6370
* [ENHANCEMENT] Query Frontend/Querier: Add an experimental flag `-querier.enable-promql-experimental-functions` to enable experimental promQL functions. #6355
Expand Down
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
6 changes: 3 additions & 3 deletions pkg/util/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ var (
// Logger is a shared go-kit logger.
// TODO: Change all components to take a non-global logger via their constructors.
// Prefer accepting a non-global logger as an argument.
Logger = log.NewNopLogger()

Logger = log.NewNopLogger()
SlogAdapter = slogAdapter{} // usage: util_log.SlogAdapter.GoKitLogToSlog(gokitLogger)
logMessages = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "log_messages_total",
Help: "Total number of log messages.",
Expand All @@ -52,7 +52,7 @@ func InitLogger(cfg *server.Config) {

// when use util_log.Logger, skip 6 stack frames.
Logger = newPrometheusLoggerFrom(l, cfg.LogLevel, "caller", log.Caller(6))

SlogAdapter = newSlogAdapter(cfg.LogLevel)
// cfg.Log wraps log function, skip 7 stack frames to get caller information.
// this works in go 1.12, but doesn't work in versions earlier.
// it will always shows the wrapper function generated by compiler
Expand Down
32 changes: 32 additions & 0 deletions pkg/util/log/slog_adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package log

import (
"log/slog"

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

type slogAdapter struct {
logging.Level
}

func newSlogAdapter(level logging.Level) slogAdapter {
return slogAdapter{level}
}

func (s *slogAdapter) GoKitLogToSlog(logger log.Logger) *slog.Logger {
levelVar := slog.LevelVar{}
switch s.Level.String() {
case "info":
levelVar.Set(slog.LevelInfo)
case "warn":
levelVar.Set(slog.LevelWarn)
case "error":
levelVar.Set(slog.LevelError)
case "debug":
levelVar.Set(slog.LevelDebug)
}
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 := SlogAdapter.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 6e8e031

Please sign in to comment.