Skip to content

Commit

Permalink
chore(outputs.groundwork): Simplify log-adapter: part2
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlo Sumkin committed Oct 3, 2024
1 parent 92ce908 commit cd108cf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 38 deletions.
39 changes: 9 additions & 30 deletions plugins/outputs/groundwork/groundwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,8 @@ func TestWriteWithDefaults(t *testing.T) {
require.NoError(t, err)
}))

testLogger := func() telegraf.Logger {
if err := logger.SetupLogging(&logger.Config{Debug: true}); err != nil {
return nil
}
l := new(testutil.Logger)
log.Logger = NewLogger(l).WithGroup("tcg.sdk")
return l
}()

i := Groundwork{
Log: testLogger,
Log: new(testutil.Logger),
Server: server.URL,
AgentID: defaultTestAgentID,
DefaultHost: defaultHost,
Expand All @@ -76,6 +67,8 @@ func TestWriteWithDefaults(t *testing.T) {
},
}

require.NoError(t, logger.SetupLogging(&logger.Config{Debug: true}))
log.Logger = NewLogger(i.Log).WithGroup("tcg.sdk")
err := i.Write([]telegraf.Metric{intMetric})
require.NoError(t, err)

Expand Down Expand Up @@ -111,17 +104,8 @@ func TestWriteWithFields(t *testing.T) {
require.NoError(t, err)
}))

testLogger := func() telegraf.Logger {
if err := logger.SetupLogging(&logger.Config{Debug: true}); err != nil {
return nil
}
l := new(testutil.Logger)
log.Logger = NewLogger(l).WithGroup("tcg.sdk")
return l
}()

i := Groundwork{
Log: testLogger,
Log: new(testutil.Logger),
Server: server.URL,
AgentID: defaultTestAgentID,
DefaultHost: defaultHost,
Expand All @@ -137,6 +121,8 @@ func TestWriteWithFields(t *testing.T) {
},
}

require.NoError(t, logger.SetupLogging(&logger.Config{Debug: true}))
log.Logger = NewLogger(i.Log).WithGroup("tcg.sdk")
err := i.Write([]telegraf.Metric{floatMetric})
require.NoError(t, err)

Expand Down Expand Up @@ -189,17 +175,8 @@ func TestWriteWithTags(t *testing.T) {
require.NoError(t, err)
}))

testLogger := func() telegraf.Logger {
if err := logger.SetupLogging(&logger.Config{Debug: true}); err != nil {
return nil
}
l := new(testutil.Logger)
log.Logger = NewLogger(l).WithGroup("tcg.sdk")
return l
}()

i := Groundwork{
Log: testLogger,
Log: new(testutil.Logger),
Server: server.URL,
AgentID: defaultTestAgentID,
DefaultHost: defaultHost,
Expand All @@ -215,6 +192,8 @@ func TestWriteWithTags(t *testing.T) {
},
}

require.NoError(t, logger.SetupLogging(&logger.Config{Debug: true}))
log.Logger = NewLogger(i.Log).WithGroup("tcg.sdk")
err := i.Write([]telegraf.Metric{floatMetric})
require.NoError(t, err)

Expand Down
19 changes: 11 additions & 8 deletions plugins/outputs/groundwork/log_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,8 @@ func (h *TlgHandler) Handle(_ context.Context, r slog.Record) error {
slog.String("logger", strings.Join(h.groups, ",")),
slog.String("message", r.Message),
)
for _, attr := range h.attrs {
if v, ok := attr.Value.Any().(json.RawMessage); ok {
attrs = append(attrs, slog.String(attr.Key, string(v)))
continue
}
attrs = append(attrs, attr)
}
attrs = append(attrs, h.attrs...)

r.Attrs(func(attr slog.Attr) bool {
if v, ok := attr.Value.Any().(json.RawMessage); ok {
attrs = append(attrs, slog.String(attr.Key, string(v)))
Expand All @@ -70,7 +65,15 @@ func (h *TlgHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
nested := &TlgHandler{Log: h.Log}
nested.attrs = append(nested.attrs, h.attrs...)
nested.groups = append(nested.groups, h.groups...)
nested.attrs = append(nested.attrs, attrs...)

for _, attr := range attrs {
if v, ok := attr.Value.Any().(json.RawMessage); ok {
nested.attrs = append(nested.attrs, slog.String(attr.Key, string(v)))
continue
}
nested.attrs = append(nested.attrs, attr)
}

return nested
}

Expand Down

0 comments on commit cd108cf

Please sign in to comment.