From 16bd935338d6a2041756f9fcbcc2b26f51334e17 Mon Sep 17 00:00:00 2001 From: paul miller Date: Tue, 14 Apr 2026 15:28:16 -0700 Subject: [PATCH 1/2] handle structs better --- appinsights/handler.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/appinsights/handler.go b/appinsights/handler.go index a50ff03..a7522eb 100644 --- a/appinsights/handler.go +++ b/appinsights/handler.go @@ -2,6 +2,8 @@ package appinsights import ( "context" + "encoding/json" + "fmt" "log/slog" "maps" "net/http" @@ -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() } From 79337b3f9908b1837c177ed6d056eb82c2b5a9ee Mon Sep 17 00:00:00 2001 From: paul miller Date: Tue, 14 Apr 2026 15:32:33 -0700 Subject: [PATCH 2/2] test it --- appinsights/handler_public_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/appinsights/handler_public_test.go b/appinsights/handler_public_test.go index e584158..461c90d 100644 --- a/appinsights/handler_public_test.go +++ b/appinsights/handler_public_test.go @@ -2,6 +2,7 @@ package appinsights_test import ( "context" + "encoding/json" "log/slog" "maps" "strings" @@ -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 { @@ -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}`, + }, + }, } }