Skip to content

Commit

Permalink
otelslog: Transform nil attribute to empty log.Value (#6246)
Browse files Browse the repository at this point in the history
Fixes
#6242
  • Loading branch information
pellared authored Oct 15, 2024
1 parent 32c279a commit 0aad96e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Transform nil attribute values to `log.Value` zero value instead of panicking in `go.opentelemetry.io/contrib/bridges/otellogrus`. (#6237)
- Transform nil attribute values to `log.Value` zero value instead of panicking in `go.opentelemetry.io/contrib/bridges/otelzap`. (#6237)
- Transform nil attribute values to `log.Value` zero value instead of `log.StringValue("<nil>")` in `go.opentelemetry.io/contrib/bridges/otelslog`. (#6246)

<!-- Released section -->
<!-- Don't change this section unless doing release -->
Expand Down
6 changes: 5 additions & 1 deletion bridges/otelslog/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
//
// Attribute values are transformed based on their [slog.Kind]:
//
// - [slog.KindAny] are transformed to [log.StringValue]. The value is
// - [slog.KindAny] non-nil values are transformed to [log.StringValue]
// encoded using [fmt.Sprintf].
// Nil values are transformed to a zero value of [log.Value].
// - [slog.KindBool] are transformed to [log.BoolValue] directly.
// - [slog.KindDuration] are transformed to [log.Int64Value] as nanoseconds.
// - [slog.KindFloat64] are transformed to [log.Float64Value] directly.
Expand Down Expand Up @@ -411,6 +412,9 @@ func (b *kvBuffer) AddAttr(attr slog.Attr) bool {
func convertValue(v slog.Value) log.Value {
switch v.Kind() {
case slog.KindAny:
if v.Any() == nil {
return log.Value{}
}
return log.StringValue(fmt.Sprintf("%+v", v.Any()))
case slog.KindBool:
return log.BoolValue(v.Bool())
Expand Down
2 changes: 2 additions & 0 deletions bridges/otelslog/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ func TestSLogHandler(t *testing.T) {
"string", "str",
"time", now,
"uint64", uint64(3),
"nil", nil,
// KindGroup and KindLogValuer are left for slogtest.TestHandler.
)
},
Expand All @@ -254,6 +255,7 @@ func TestSLogHandler(t *testing.T) {
hasAttr("string", "str"),
hasAttr("time", now.UnixNano()),
hasAttr("uint64", int64(3)),
hasAttr("nil", nil),
}},
},
{
Expand Down

0 comments on commit 0aad96e

Please sign in to comment.