From 553af91625f330809d9ade5dfeef329a76963a0d Mon Sep 17 00:00:00 2001 From: Eric Solberg Date: Wed, 14 Aug 2024 19:05:43 -0500 Subject: [PATCH] Use error.Error() string for log attributes --- v3/newrelic/attributes_from_internal.go | 6 ++++++ v3/newrelic/log_events_test.go | 11 +++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/v3/newrelic/attributes_from_internal.go b/v3/newrelic/attributes_from_internal.go index 430c5752a..c2278a5f7 100644 --- a/v3/newrelic/attributes_from_internal.go +++ b/v3/newrelic/attributes_from_internal.go @@ -461,6 +461,12 @@ func writeAttributeValueJSON(w *jsonFieldsWriter, key string, val interface{}) { v = v[:maxAttributeLengthBytes] } w.stringField(key, v) + case error: + value := v.Error() + if len(value) > maxAttributeLengthBytes { + value = value[:maxAttributeLengthBytes] + } + w.stringField(key, value) case bool: if v { w.rawField(key, `true`) diff --git a/v3/newrelic/log_events_test.go b/v3/newrelic/log_events_test.go index e359ef55a..1c9a3abc5 100644 --- a/v3/newrelic/log_events_test.go +++ b/v3/newrelic/log_events_test.go @@ -4,6 +4,7 @@ package newrelic import ( + "errors" "fmt" "testing" "time" @@ -88,12 +89,13 @@ func TestBasicLogEventWithAttributes(t *testing.T) { C: c{"hello"}, } - events := newLogEvents(testCommonAttributes, loggingConfigEnabled(5)) + events := newLogEvents(testCommonAttributes, loggingConfigEnabled(6)) events.Add(sampleLogEvent(0.5, infoLevel, "message1", map[string]any{"two": "hi"})) events.Add(sampleLogEvent(0.5, infoLevel, "message2", map[string]any{"struct": st})) events.Add(sampleLogEvent(0.5, infoLevel, "message3", map[string]any{"map": map[string]string{"hi": "hello"}})) events.Add(sampleLogEvent(0.5, infoLevel, "message4", map[string]any{"slice": []string{"hi", "hello", "test"}})) events.Add(sampleLogEvent(0.5, infoLevel, "message5", map[string]any{"array": [2]int{1, 2}})) + events.Add(sampleLogEvent(0.5, infoLevel, "message6", map[string]any{"error": errors.New("test error")})) json, err := events.CollectorJSON(agentRunID) if nil != err { @@ -105,15 +107,16 @@ func TestBasicLogEventWithAttributes(t *testing.T) { `{"level":"INFO","message":"message2","timestamp":123456,"attributes":{"struct":"{\"A\":\"a\",\"B\":1,\"C\":{\"D\":\"hello\"}}"}},` + `{"level":"INFO","message":"message3","timestamp":123456,"attributes":{"map":"{\"hi\":\"hello\"}"}},` + `{"level":"INFO","message":"message4","timestamp":123456,"attributes":{"slice":"[\"hi\",\"hello\",\"test\"]"}},` + - `{"level":"INFO","message":"message5","timestamp":123456,"attributes":{"array":"[1,2]"}}]}]` + `{"level":"INFO","message":"message5","timestamp":123456,"attributes":{"array":"[1,2]"}},` + + `{"level":"INFO","message":"message6","timestamp":123456,"attributes":{"error":"test error"}}]}]` if string(json) != expected { t.Error("actual not equal to expected:\n", string(json), "\n", expected) } - if events.numSeen != 5 { + if events.numSeen != 6 { t.Error(events.numSeen) } - if events.NumSaved() != 5 { + if events.NumSaved() != 6 { t.Error(events.NumSaved()) } }