Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(deps): Update github.com/gwos/tcg/sdk from v8.7.2 to v8.8.0 #15947

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ymkins
Copy link
Contributor

@ymkins ymkins commented Sep 27, 2024

Summary

[outputs.groundwork] update SDK

TCG SDK that provides client used for sending data to Groundwork Monitoring has updated.
Modern version got fixes and features. Internal logger was changed to slog for simplify integrations.

PR provides telegraf.Logger adapter for slog.Logger used in SDK. It allows to output SDK messages with Telegraf logger.

Also PR provides improvement to filter out non-UTF symbols in payloads.

Checklist

  • No AI generated code was used in this PR

Related issues

resolves #15945

@telegraf-tiger telegraf-tiger bot added the chore label Sep 27, 2024
@ymkins
Copy link
Contributor Author

ymkins commented Sep 27, 2024

$ make lint plugins/outputs/groundwork/
golangci-lint run
plugins/outputs/groundwork/slog/slog.go:6:2  depguard  import 'log/slog' is not allowed from list 'main': Use injected telegraf.Logger instead
1 issues:
* depguard: 1
make: *** [Makefile:191: lint] Error 1

This package implements telegraf.Logger adapter for slog.Logger used in library. It's used to output library messages with Telegraf logger.

Copy link
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for you work @ymkins! I think you should tailor the implementation of the adapter to the concrete use-case here. Furthermore, I'm not convinced that logging library-internal messages with their log-level is the right approach. I would log all library-internal messages as level Trace to allow used to silence them as usually those should not be interesting for anyone...

plugins/outputs/groundwork/groundwork_test.go Outdated Show resolved Hide resolved
plugins/outputs/groundwork/groundwork.go Outdated Show resolved Hide resolved
plugins/outputs/groundwork/groundwork.go Outdated Show resolved Hide resolved
plugins/outputs/groundwork/groundwork_test.go Outdated Show resolved Hide resolved
plugins/outputs/groundwork/slog/slog.go Outdated Show resolved Hide resolved
Comment on lines 58 to 77
attrs := make([]slog.Attr, 0, 2+len(h.attrs)+r.NumAttrs())
attrs = append(attrs,
slog.String(h.MessageFieldName, r.Message),
slog.String(h.GroupsFieldName, strings.Join(h.groups, ",")),
)
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)
}
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)))
return true
}
attrs = append(attrs, attr)
return true
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to construct an array of attributes instead of directly constructing the message for logging?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we just preparing log data: adjusting datatype for json.RawMessage for better formatting. Then we letting actual logger make his work.
In output we getting well-structured message.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was wondering why you don't directly format it into []string instead of keeping them as []slog.Attr...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are non-string Attrs. Convert them here to string, then actual writer will convert it to bytes.. does it make sense? Onetime conversion in writer should be better.

plugins/outputs/groundwork/slog/slog.go Outdated Show resolved Hide resolved
plugins/outputs/groundwork/slog/slog.go Outdated Show resolved Hide resolved
@srebhan srebhan self-assigned this Sep 30, 2024
Update the implementation of the adapter for address review comments
Copy link
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update @ymkins! Some more comments, with the most sever being the request to not work-around logging in tests.

Comment on lines 55 to 63
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
}()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be necessary! Please make sure the tests pass without this! Using testutil.Logger should be sufficient as otherwise there is something fishy going on... Same for the instances below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines +12 to +19
// NewLogger creates telegraf.Logger adapter for slog.Logger
func NewLogger(l telegraf.Logger) *slog.Logger {
return slog.New(&TlgHandler{Log: l})
}

// TlgHandler translates slog.Record into telegraf.Logger call
// inspired by https://github.com/golang/example/blob/master/slog-handler-guide/README.md
type TlgHandler struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't export these things! Maybe even kill off the NewLogger function completely as it only wraps a single line. However, I see the value in keeping log/slog only in here...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, there is the value to keep log/slog only in here
and done TlgHandler => tlgHandler

Comment on lines 43 to 49
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)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this conversion be already done in WithAttrs? This would save a few cycles and simplify code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@telegraf-tiger
Copy link
Contributor

telegraf-tiger bot commented Oct 3, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[outputs.groundwork] update SDK
3 participants