Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions appinsights/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package appinsights

import (
"context"
"encoding/json"
"fmt"
"log/slog"
"maps"
"net/http"
Expand Down Expand Up @@ -219,6 +221,15 @@ func addAttributeToMap(m map[string]string, keyPrefix string, a slog.Attr) {
case slog.KindGroup:
addAttributeGroupToMap(m, keyPrefix, a)
return
case slog.KindAny:
switch v := a.Value.Any().(type) {
case json.RawMessage:
value = string(v)
case []byte:
value = string(v)
default:
value = fmt.Sprint(v)
}
default:
value = a.Value.String()
}
Expand Down
29 changes: 29 additions & 0 deletions appinsights/handler_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package appinsights_test

import (
"context"
"encoding/json"
"log/slog"
"maps"
"strings"
Expand Down Expand Up @@ -199,6 +200,12 @@ type attrTestCase struct {
properties attrMap
}

type pet struct {
Name string
Breed string
Age int
}

func (c *attrTestCase) args() []any {
args := make([]any, 0, len(c.attrs)*2)
for _, a := range c.attrs {
Expand Down Expand Up @@ -295,6 +302,28 @@ func attrsCases() []attrTestCase {
},
attrMap{},
},
{
"any struct",
[]slog.Attr{
slog.Any("pet", pet{
Name: "Mochi",
Breed: "Beagle",
Age: 7,
}),
},
attrMap{
"pet": "{Mochi Beagle 7}",
},
},
{
"raw json",
[]slog.Attr{
slog.Any("usage", json.RawMessage(`{"prompt_tokens":448,"total_tokens":1877}`)),
},
attrMap{
"usage": `{"prompt_tokens":448,"total_tokens":1877}`,
},
},
}
}

Expand Down