Skip to content

Commit

Permalink
Use error.Error() string for log attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsolberg committed Aug 27, 2024
1 parent b838457 commit 553af91
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
6 changes: 6 additions & 0 deletions v3/newrelic/attributes_from_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
11 changes: 7 additions & 4 deletions v3/newrelic/log_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package newrelic

import (
"errors"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -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 {
Expand All @@ -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())
}
}
Expand Down

0 comments on commit 553af91

Please sign in to comment.