Skip to content

Commit

Permalink
restore previous implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiangreco committed Nov 28, 2024
1 parent 5855296 commit 8fc3ded
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 7 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ require (
github.com/stretchr/testify v1.10.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
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2463,8 +2463,6 @@ 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=
Expand Down
111 changes: 111 additions & 0 deletions internal/runtime/logging/slgk_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package logging

import (
"context"
"log/slog"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
)

var _ slog.Handler = (*SlogGoKitHandler)(nil)

type SlogGoKitHandler struct {
logger log.Logger
group string
preformatted []any
}

func NewSlogGoKitHandler(logger log.Logger) *SlogGoKitHandler {
return &SlogGoKitHandler{
logger: logger,
}
}

func (h SlogGoKitHandler) Enabled(ctx context.Context, level slog.Level) bool {
return true
}

func (h SlogGoKitHandler) 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)
}

pairs := make([]any, 0, 2*record.NumAttrs())
pairs = append(pairs, "msg", record.Message)
pairs = append(pairs, h.preformatted...)

record.Attrs(func(attr slog.Attr) bool {
pairs = appendPair(pairs, h.group, attr)
return true
})

return logger.Log(pairs...)
}

func (h SlogGoKitHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
pairs := make([]any, 0, 2*len(attrs))
for _, attr := range attrs {
pairs = appendPair(pairs, h.group, attr)
}

if h.preformatted != nil {
pairs = append(h.preformatted, pairs...)
}

return &SlogGoKitHandler{
logger: h.logger,
preformatted: pairs,
group: h.group,
}
}

func (h SlogGoKitHandler) WithGroup(name string) slog.Handler {
if name == "" {
return h
}

group := name
if h.group != "" {
group = h.group + "." + group
}

return &SlogGoKitHandler{
logger: h.logger,
preformatted: h.preformatted,
group: group,
}
}

func appendPair(pairs []any, groupPrefix string, attr slog.Attr) []any {
if attr.Equal(slog.Attr{}) {
return pairs
}

switch attr.Value.Kind() {
case slog.KindGroup:
if attr.Key != "" {
groupPrefix = groupPrefix + "." + attr.Key
}
for _, a := range attr.Value.Group() {
pairs = appendPair(pairs, groupPrefix, a)
}
default:
key := attr.Key
if groupPrefix != "" {
key = groupPrefix + "." + key
}

pairs = append(pairs, key, attr.Value)
}

return pairs
}
50 changes: 50 additions & 0 deletions internal/runtime/logging/slgk_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package logging_test

import (
"bytes"
"log/slog"
"strings"
"testing"

"github.com/go-kit/log"
"github.com/grafana/alloy/internal/runtime/logging"
"github.com/stretchr/testify/require"
)

func TestSlogGokitAdapter_UpdateLevel(t *testing.T) {
buffer := bytes.NewBuffer(nil)
baseLogger, err := logging.New(buffer, logging.Options{Level: logging.LevelInfo, Format: logging.FormatLogfmt})
require.NoError(t, err)

gkLogger := log.With(baseLogger, "test", "test")
gkLogger.Log("msg", "hello")
require.Contains(t, buffer.String(), "ts=")
noTimestamp := strings.Join(strings.Split(buffer.String(), " ")[1:], " ")
require.Equal(t, "level=info msg=hello test=test\n", noTimestamp)

sLogger := slog.New(logging.NewSlogGoKitHandler(gkLogger))
buffer.Reset()
sLogger.Info("hello")
require.Contains(t, buffer.String(), "ts=")
noTimestamp = strings.Join(strings.Split(buffer.String(), " ")[1:], " ")
require.Equal(t, "level=info msg=hello test=test\n", noTimestamp)

buffer.Reset()
sLogger.Debug("hello")
require.Equal(t, "", buffer.String())

err = baseLogger.Update(logging.Options{Level: logging.LevelDebug, Format: logging.FormatLogfmt})
require.NoError(t, err)

buffer.Reset()
sLogger.Info("hello")
require.Contains(t, buffer.String(), "ts=")
noTimestamp = strings.Join(strings.Split(buffer.String(), " ")[1:], " ")
require.Equal(t, "level=info msg=hello test=test\n", noTimestamp)

buffer.Reset()
sLogger.Debug("hello")
require.Contains(t, buffer.String(), "ts=")
noTimestamp = strings.Join(strings.Split(buffer.String(), " ")[1:], " ")
require.Equal(t, "level=debug msg=hello test=test\n", noTimestamp)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"log/slog"
"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"
config_util "github.com/prometheus/common/config"

"github.com/grafana/alloy/internal/runtime/logging"
"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"
Expand Down Expand Up @@ -124,7 +124,7 @@ func New(log log.Logger, c *Config) (integrations.Integration, error) {
}

scrapers := GetScrapers(c)
logger := slog.New(slgk.NewGoKitHandler(log, nil))
logger := slog.New(logging.NewSlogGoKitHandler(log))
exporter := collector.New(context.Background(), string(dsn), scrapers, logger, collector.Config{
LockTimeout: c.LockWaitTimeout,
SlowLogFilter: c.LogSlowFilter,
Expand Down

0 comments on commit 8fc3ded

Please sign in to comment.