diff --git a/go.mod b/go.mod index 03d95a2e2d..0d659862f3 100644 --- a/go.mod +++ b/go.mod @@ -183,6 +183,7 @@ require ( github.com/stretchr/testify v1.9.0 github.com/testcontainers/testcontainers-go v0.33.0 github.com/tilinna/clock v1.1.0 + github.com/tjhop/slog-gokit v0.1.2 github.com/ua-parser/uap-go v0.0.0-20240611065828-3a4781585db6 // indirect github.com/uber/jaeger-client-go v2.30.0+incompatible github.com/vincent-petithory/dataurl v1.0.0 diff --git a/go.sum b/go.sum index 30969091cc..56d114e026 100644 --- a/go.sum +++ b/go.sum @@ -1002,8 +1002,6 @@ github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5Nq github.com/go-sql-driver/mysql v0.0.0-20180618115901-749ddf1598b4/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= -github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= -github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= @@ -1247,8 +1245,6 @@ github.com/grafana/loki/pkg/push v0.0.0-20240617182007-6c33561108ad h1:4XAhmVmKb github.com/grafana/loki/pkg/push v0.0.0-20240617182007-6c33561108ad/go.mod h1:lJEF/Wh5MYlmBem6tOYAFObkLsuikfrEf8Iy9AdMPiQ= github.com/grafana/loki/v3 v3.0.0-20240617182007-6c33561108ad h1:eaHz6gH7OM3LWGp1h2QZ+/wkeyjNspk3QwjD4YUWR5g= github.com/grafana/loki/v3 v3.0.0-20240617182007-6c33561108ad/go.mod h1:fKBY3k60xjlGcIdbOaJiy3aWopF65FpQvQWczCp2SZ0= -github.com/grafana/mysqld_exporter v0.12.2-0.20231005125903-364b9c41e595 h1:I9sRknI5ajd8whPOX0nBDXy5B6xUfhItClMy+6R4oqE= -github.com/grafana/mysqld_exporter v0.12.2-0.20231005125903-364b9c41e595/go.mod h1:U8ifHC5pT2WuVTO7ki4KZmWLjfEKfktQiU3bh0J8scw= github.com/grafana/mysqld_exporter v0.16.1-0.20241111085008-47b8ea9fab19 h1:JKMpcPPmhU198g3skPdSf2YvcgUKZ6bf3X1uTXnCZXU= github.com/grafana/mysqld_exporter v0.16.1-0.20241111085008-47b8ea9fab19/go.mod h1:ctzLZ0vK3ow5JZmBfbS261ufC/4vtfuwFileKJea3kg= github.com/grafana/node_exporter v0.18.1-grafana-r01.0.20231004161416-702318429731 h1:vyyIYY2sLpmgFIckJ1vSO/oYkvB0thDF6UiFYp5PThM= @@ -2466,6 +2462,8 @@ github.com/tilinna/clock v1.1.0/go.mod h1:ZsP7BcY7sEEz7ktc0IVy8Us6boDrK8VradlKRU github.com/tinylib/msgp v1.1.5/go.mod h1:eQsjooMTnV42mHu917E26IogZ2930nFyBQdofk10Udg= github.com/tinylib/msgp v1.2.4 h1:yLFeUGostXXSGW5vxfT5dXG/qzkn4schv2I7at5+hVU= github.com/tinylib/msgp v1.2.4/go.mod h1:ykjzy2wzgrlvpDCRc4LA8UXy6D8bzMSuAF3WD57Gok0= +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/tklauser/go-sysconf v0.3.11/go.mod h1:GqXfhXY3kiPa0nAXPDIQIWzJbMCB7AmcWpGR8lSZfqI= github.com/tklauser/go-sysconf v0.3.13 h1:GBUpcahXSpR2xN01jhkNAbTLRk2Yzgggk8IM08lq3r4= github.com/tklauser/go-sysconf v0.3.13/go.mod h1:zwleP4Q4OehZHGn4CYZDipCgg9usW5IJePewFCGVEa0= diff --git a/internal/slogadapter/handler.go b/internal/slogadapter/handler.go deleted file mode 100644 index c4d7e833cc..0000000000 --- a/internal/slogadapter/handler.go +++ /dev/null @@ -1,76 +0,0 @@ -package slogadapter - -import ( - "context" - "log/slog" - - "github.com/go-kit/log" - "github.com/go-kit/log/level" -) - -var _ slog.Handler = (*GoKitHandler)(nil) - -type GoKitHandler struct { - Logger log.Logger - Group string -} - -func (h GoKitHandler) Enabled(ctx context.Context, level slog.Level) bool { - return true -} - -func (h GoKitHandler) Handle(ctx context.Context, record slog.Record) error { - var logger log.Logger - switch record.Level { - case slog.LevelInfo: - logger = level.Info(h.Logger) - case slog.LevelWarn: - logger = level.Warn(h.Logger) - case slog.LevelError: - logger = level.Error(h.Logger) - default: - logger = level.Debug(h.Logger) - } - - if h.Group == "" { - pairs := make([]any, 0, record.NumAttrs()+2) - pairs = append(pairs, "msg", record.Message) - record.Attrs(func(attr slog.Attr) bool { - pairs = append(pairs, attr.Key, attr.Value) - return true - }) - return logger.Log(pairs...) - } - - pairs := make([]any, 0, record.NumAttrs()) - record.Attrs(func(attr slog.Attr) bool { - pairs = append(pairs, attr.Key, attr.Value) - return true - }) - g := slog.Group(h.Group, pairs...) - pairs = []any{"msg", record.Message, g.Key, g.Value} - return logger.Log(pairs...) -} - -func (h GoKitHandler) WithAttrs(attrs []slog.Attr) slog.Handler { - pairs := make([]any, 0, len(attrs)) - for _, attr := range attrs { - pairs = append(pairs, attr.Key, attr.Value) - } - - if h.Group == "" { - return GoKitHandler{Logger: log.With(h.Logger, pairs...)} - } - - g := slog.Group(h.Group, pairs...) - return GoKitHandler{Logger: log.With(h.Logger, g.Key, g.Value)} -} - -func (h GoKitHandler) WithGroup(name string) slog.Handler { - if name == "" { - return h - } - - h.Group = name - return h -} diff --git a/internal/static/integrations/mysqld_exporter/mysqld-exporter.go b/internal/static/integrations/mysqld_exporter/mysqld-exporter.go index 95f2750c0b..57666f509c 100644 --- a/internal/static/integrations/mysqld_exporter/mysqld-exporter.go +++ b/internal/static/integrations/mysqld_exporter/mysqld-exporter.go @@ -8,11 +8,11 @@ import ( "os" config_util "github.com/prometheus/common/config" + slgk "github.com/tjhop/slog-gokit" "github.com/go-kit/log" "github.com/go-kit/log/level" "github.com/go-sql-driver/mysql" - "github.com/grafana/alloy/internal/slogadapter" "github.com/grafana/alloy/internal/static/integrations" integrations_v2 "github.com/grafana/alloy/internal/static/integrations/v2" "github.com/grafana/alloy/internal/static/integrations/v2/metricsutils" @@ -124,7 +124,7 @@ func New(log log.Logger, c *Config) (integrations.Integration, error) { } scrapers := GetScrapers(c) - logger := slog.New(slogadapter.GoKitHandler{Logger: log, Group: ""}) + logger := slog.New(slgk.NewGoKitHandler(log, nil)) exporter := collector.New(context.Background(), string(dsn), scrapers, logger, collector.Config{ LockTimeout: c.LockWaitTimeout, SlowLogFilter: c.LogSlowFilter,