Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use error.Error() string for log attributes #947

Merged
merged 1 commit into from
Sep 5, 2024
Merged
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
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
Loading