-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
base: master
Are you sure you want to change the base?
Conversation
This package implements telegraf.Logger adapter for slog.Logger used in library. It's used to output library messages with Telegraf logger. |
5058565
to
f94db3c
Compare
There was a problem hiding this 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...
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 | ||
}) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
...
There was a problem hiding this comment.
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.
Update the implementation of the adapter for address review comments
There was a problem hiding this 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.
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 | ||
}() | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
// 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 { |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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
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) | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Download PR build artifacts for linux_amd64.tar.gz, darwin_arm64.tar.gz, and windows_amd64.zip. 📦 Click here to get additional PR build artifactsArtifact URLs |
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
Related issues
resolves #15945