Skip to content

Commit

Permalink
Merge pull request #8 from remychantenay/add-slice-support
Browse files Browse the repository at this point in the history
  • Loading branch information
remychantenay authored Nov 7, 2023
2 parents 7212890 + 1232f8f commit ed6bf20
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
15 changes: 15 additions & 0 deletions slog_otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ func (h OtelHandler) slogAttrToOtelAttr(attr slog.Attr, groupKeys ...string) att
for _, groupAttr := range groupAttrs {
return h.slogAttrToOtelAttr(groupAttr, append(groupKeys, key)...)
}
case slog.KindAny:
switch v := attr.Value.Any().(type) {
case []string:
return attribute.StringSlice(key, v)
case []int:
return attribute.IntSlice(key, v)
case []int64:
return attribute.Int64Slice(key, v)
case []float64:
return attribute.Float64Slice(key, v)
case []bool:
return attribute.BoolSlice(key, v)
default:
return attribute.KeyValue{}
}
default:
return attribute.KeyValue{}
}
Expand Down
35 changes: 34 additions & 1 deletion slog_otel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ func TestOtelHandler(t *testing.T) {
Key: "level",
Value: attribute.StringValue("INFO"),
}, {
Key: "string_slice",
Value: attribute.StringSliceValue([]string{"value_1", "value_2"}),
}, {
Key: "int_slice",
Value: attribute.IntSliceValue([]int{1, 2}),
}, {
Key: "int64_slice",
Value: attribute.Int64SliceValue([]int64{1, 2}),
}, {
Key: "float64_slice",
Value: attribute.Float64SliceValue([]float64{1.0, 2.0}),
}, {
Key: "bool_slice",
Value: attribute.BoolSliceValue([]bool{true, false}),
}, {

Key: "group_1.key_1",
Value: attribute.StringValue("value_1"),
}, {
Expand All @@ -122,7 +138,24 @@ func TestOtelHandler(t *testing.T) {

group1 := slog.Group("group_1", "key_1", "value_1")
group2 := slog.Group("group_2", "key_2", "value_2")
slog.InfoContext(ctx, "adds event to span", "a_key", "a_value", group1, group2)

stringSlice := []string{"value_1", "value_2"}
intSlice := []int{1, 2}
int64Slice := []int64{1, 2}
float64Slice := []float64{1.0, 2.0}
boolSlice := []bool{true, false}

slog.InfoContext(ctx,
"adds event to span",
"string_slice", stringSlice,
"int_slice", intSlice,
"int64_slice", int64Slice,
"float64_slice", float64Slice,
"bool_slice", boolSlice,
"a_key", "a_value",
group1,
group2,
)
}()

spans := spanRecorder.Ended()
Expand Down

0 comments on commit ed6bf20

Please sign in to comment.