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

Added Attribute Support for ZeroLog #935

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions v3/integrations/logcontext-v2/zerologWriter/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func main() {
time.Sleep(time.Microsecond * 100)

txnLogger.Printf("Ending transaction %s.", txnName)
txnLogger.Info().
Str("foo", "bar").
Bool("answer", true).
Msg("This is an info message with attributes.")

txn.End()

app.Shutdown(10 * time.Second)
Expand Down
16 changes: 12 additions & 4 deletions v3/integrations/logcontext-v2/zerologWriter/zerolog-writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func parseJSONLogData(log []byte) newrelic.LogData {
data := newrelic.LogData{}
data.Message = string(log)
data.Timestamp = time.Now().UnixMilli()

data.Attributes = make(map[string]any)
for i := 0; i < len(log)-1; {
// get key; always a string field
key, valStart := getKey(log, i)
Expand All @@ -70,11 +70,19 @@ func parseJSONLogData(log []byte) newrelic.LogData {
if i >= len(log)-1 {
return data
}
// TODO: once we update the logging spec to support custom attributes, capture these

if isStringValue(log, valStart) {
_, next = getStringValue(log, valStart+1)
strVal := ""
strVal, next = getStringValue(log, valStart+1)
// If the key is message skip adding it to attributes
if key != "message" {
data.Attributes[key] = strVal

}
} else if isNumberValue(log, valStart) {
_, next = getNumberValue(log, valStart)
numVal := ""
numVal, next = getNumberValue(log, valStart)
data.Attributes[key] = numVal
} else {
return data
}
Expand Down
123 changes: 107 additions & 16 deletions v3/integrations/logcontext-v2/zerologWriter/zerolog-writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/newrelic/go-agent/v3/internal"

"github.com/newrelic/go-agent/v3/internal/integrationsupport"
"github.com/newrelic/go-agent/v3/internal/logcontext"
"github.com/newrelic/go-agent/v3/internal/sysinfo"
Expand Down Expand Up @@ -183,9 +184,10 @@ func TestParseLogDataEscapes(t *testing.T) {
for _, test := range tests {
logger.Info().Msg(test.logMessage)
wantLog = append(wantLog, internal.WantLog{
Severity: zerolog.LevelInfoValue,
Message: test.expectMessage,
Timestamp: internal.MatchAnyUnixMilli,
Severity: zerolog.LevelInfoValue,
Message: test.expectMessage,
Timestamp: internal.MatchAnyUnixMilli,
Attributes: make(map[string]interface{}),
})

}
Expand Down Expand Up @@ -218,9 +220,10 @@ func TestE2E(t *testing.T) {

app.ExpectLogEvents(t, []internal.WantLog{
{
Severity: zerolog.LevelInfoValue,
Message: `{"level":"info","message":"Hello World!"}`,
Timestamp: internal.MatchAnyUnixMilli,
Severity: zerolog.LevelInfoValue,
Message: `{"level":"info","message":"Hello World!"}`,
Timestamp: internal.MatchAnyUnixMilli,
Attributes: make(map[string]interface{}),
},
})
}
Expand Down Expand Up @@ -254,11 +257,12 @@ func TestE2EWithContext(t *testing.T) {

app.ExpectLogEvents(t, []internal.WantLog{
{
Severity: zerolog.LevelInfoValue,
Message: `{"level":"info","message":"Hello World!"}`,
Timestamp: internal.MatchAnyUnixMilli,
TraceID: traceID,
SpanID: spanID,
Severity: zerolog.LevelInfoValue,
Message: `{"level":"info","message":"Hello World!"}`,
Timestamp: internal.MatchAnyUnixMilli,
TraceID: traceID,
SpanID: spanID,
Attributes: make(map[string]interface{}),
},
})
}
Expand Down Expand Up @@ -292,11 +296,12 @@ func TestE2EWithTxn(t *testing.T) {

app.ExpectLogEvents(t, []internal.WantLog{
{
Severity: zerolog.LevelInfoValue,
Message: `{"level":"info","message":"Hello World!"}`,
Timestamp: internal.MatchAnyUnixMilli,
TraceID: traceID,
SpanID: spanID,
Severity: zerolog.LevelInfoValue,
Message: `{"level":"info","message":"Hello World!"}`,
Timestamp: internal.MatchAnyUnixMilli,
TraceID: traceID,
SpanID: spanID,
Attributes: make(map[string]interface{}),
},
})

Expand All @@ -311,3 +316,89 @@ func BenchmarkParseLogLevel(b *testing.B) {
parseJSONLogData(log)
}
}

func TestE2EWAttributes(t *testing.T) {
app := integrationsupport.NewTestApp(
integrationsupport.SampleEverythingReplyFn,
newrelic.ConfigAppLogDecoratingEnabled(true),
newrelic.ConfigAppLogForwardingEnabled(true),
)
buf := bytes.NewBuffer([]byte{})
a := New(buf, app.Application)
a.DebugLogging(true)

txn := app.Application.StartTransaction("test")

// create logger with txn context
txnWriter := a.WithTransaction(txn)
logger := zerolog.New(txnWriter)

logger.Info().
Str("foo", "bar").
Msg("Hello World!")
traceID := txn.GetLinkingMetadata().TraceID
spanID := txn.GetLinkingMetadata().SpanID
txn.End() // must end txn to dump logs into harvest

logcontext.ValidateDecoratedOutput(t, buf, &logcontext.DecorationExpect{
EntityGUID: integrationsupport.TestEntityGUID,
Hostname: host,
EntityName: integrationsupport.SampleAppName,
})
app.ExpectLogEvents(t, []internal.WantLog{
{
Severity: zerolog.LevelInfoValue,
Message: `{"level":"info","foo":"bar","message":"Hello World!"}`,
Timestamp: internal.MatchAnyUnixMilli,
TraceID: traceID,
SpanID: spanID,
Attributes: map[string]any{
"foo": "bar",
},
},
})

}

func TestE2EWAttributesInt(t *testing.T) {
app := integrationsupport.NewTestApp(
integrationsupport.SampleEverythingReplyFn,
newrelic.ConfigAppLogDecoratingEnabled(true),
newrelic.ConfigAppLogForwardingEnabled(true),
)
buf := bytes.NewBuffer([]byte{})
a := New(buf, app.Application)
a.DebugLogging(true)

txn := app.Application.StartTransaction("test")

// create logger with txn context
txnWriter := a.WithTransaction(txn)
logger := zerolog.New(txnWriter)

logger.Info().
Int("foo", 30).
Msg("Hello World!")
traceID := txn.GetLinkingMetadata().TraceID
spanID := txn.GetLinkingMetadata().SpanID
txn.End() // must end txn to dump logs into harvest

logcontext.ValidateDecoratedOutput(t, buf, &logcontext.DecorationExpect{
EntityGUID: integrationsupport.TestEntityGUID,
Hostname: host,
EntityName: integrationsupport.SampleAppName,
})
app.ExpectLogEvents(t, []internal.WantLog{
{
Severity: zerolog.LevelInfoValue,
Message: `{"level":"info","foo":30,"message":"Hello World!"}`,
Timestamp: internal.MatchAnyUnixMilli,
TraceID: traceID,
SpanID: spanID,
Attributes: map[string]any{
"foo": 30,
},
},
})

}
Loading