Skip to content

Commit

Permalink
bug fix for nil io.Writer bug and new test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
iamemilio committed Feb 14, 2024
1 parent 72dd1ca commit 55a5c6b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 28 deletions.
9 changes: 6 additions & 3 deletions v3/integrations/logcontext-v2/nrslog/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func JSONHandler(app *newrelic.Application, w io.Writer, opts *slog.HandlerOptio

// WithTransaction creates a new Slog Logger object to be used for logging within a given transaction.
func WithTransaction(txn *newrelic.Transaction, logger *slog.Logger) *slog.Logger {
if txn == nil {
if txn == nil || logger == nil {
return logger
}

Expand Down Expand Up @@ -88,8 +88,11 @@ func (h *NRHandler) WithTransaction(txn *newrelic.Transaction) NRHandler {
txn: txn,
}

writer := h.w.WithTransaction(txn)
handler.addWriter(&writer)
if h.w != nil {
writer := h.w.WithTransaction(txn)
handler.addWriter(&writer)
}

return handler
}

Expand Down
86 changes: 73 additions & 13 deletions v3/integrations/logcontext-v2/nrslog/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"log/slog"
"strings"
"testing"

"github.com/newrelic/go-agent/v3/internal"
Expand Down Expand Up @@ -128,12 +129,11 @@ func TestHandlerTransactionCtx(t *testing.T) {
log.Debug(backgroundMsg)
txn.End()

/*
logcontext.ValidateDecoratedOutput(t, out, &logcontext.DecorationExpect{
EntityGUID: integrationsupport.TestEntityGUID,
Hostname: host,
EntityName: integrationsupport.SampleAppName,
}) */
logcontext.ValidateDecoratedOutput(t, out, &logcontext.DecorationExpect{
EntityGUID: integrationsupport.TestEntityGUID,
Hostname: host,
EntityName: integrationsupport.SampleAppName,
})

app.ExpectLogEvents(t, []internal.WantLog{
{
Expand Down Expand Up @@ -171,13 +171,6 @@ func TestHandlerTransactionsAndBackground(t *testing.T) {
log.Warn(messageBackground)
txn.End()

/*
logcontext.ValidateDecoratedOutput(t, out, &logcontext.DecorationExpect{
EntityGUID: integrationsupport.TestEntityGUID,
Hostname: host,
EntityName: integrationsupport.SampleAppName,
}) */

app.ExpectLogEvents(t, []internal.WantLog{
{
Severity: slog.LevelInfo.String(),
Expand All @@ -198,3 +191,70 @@ func TestHandlerTransactionsAndBackground(t *testing.T) {
},
})
}

func TestWithAttributes(t *testing.T) {
app := integrationsupport.NewTestApp(integrationsupport.SampleEverythingReplyFn,
newrelic.ConfigAppLogDecoratingEnabled(false),
newrelic.ConfigAppLogForwardingEnabled(true),
)
out := bytes.NewBuffer([]byte{})
handler := TextHandler(app.Application, out, &slog.HandlerOptions{})
log := slog.New(handler)
message := "Hello World!"
log = log.With(slog.String("string key", "val"), slog.Int("int key", 1))

log.Info(message)

log1 := string(out.String())

txn := app.StartTransaction("hi")
txnLog := WithTransaction(txn, log)
txnLog.Info(message)
txn.End()

log2 := string(out.String())

attrString := `"string key"=val "int key"=1`
if !strings.Contains(log1, attrString) {
t.Errorf("expected %s to contain %s", log1, attrString)
}

if !strings.Contains(log2, attrString) {
t.Errorf("expected %s to contain %s", log2, attrString)
}

}

func TestWithGroup(t *testing.T) {
app := integrationsupport.NewTestApp(integrationsupport.SampleEverythingReplyFn,
newrelic.ConfigAppLogDecoratingEnabled(false),
newrelic.ConfigAppLogForwardingEnabled(true),
)
out := bytes.NewBuffer([]byte{})
handler := TextHandler(app.Application, out, &slog.HandlerOptions{})
log := slog.New(handler)
message := "Hello World!"
log = log.With(slog.Group("test group", slog.String("string key", "val")))
log = log.WithGroup("test group")

log.Info(message)

log1 := string(out.String())

txn := app.StartTransaction("hi")
txnLog := WithTransaction(txn, log)
txnLog.Info(message)
txn.End()

log2 := string(out.String())

attrString := `"test group.string key"=val`
if !strings.Contains(log1, attrString) {
t.Errorf("expected %s to contain %s", log1, attrString)
}

if !strings.Contains(log2, attrString) {
t.Errorf("expected %s to contain %s", log2, attrString)
}

}
12 changes: 0 additions & 12 deletions v3/integrations/logcontext-v2/nrslog/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package nrslog

import (
"bytes"
"context"
"io"

"github.com/newrelic/go-agent/v3/newrelic"
Expand Down Expand Up @@ -42,17 +41,6 @@ func (b *LogWriter) WithTransaction(txn *newrelic.Transaction) LogWriter {
}
}

// WithTransaction duplicates the current NewRelicWriter and sets the transaction to the transaction parsed from ctx
func (b *LogWriter) WithContext(ctx context.Context) LogWriter {
txn := newrelic.FromContext(ctx)
return LogWriter{
out: b.out,
app: b.app,
debug: b.debug,
txn: txn,
}
}

// EnrichLog attempts to enrich a log with New Relic linking metadata. If it fails,
// it will return the original log line unless debug=true, otherwise it will print
// an error on a following line.
Expand Down

0 comments on commit 55a5c6b

Please sign in to comment.