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

unit test ignore txn and sugared zap and fix for pgx5 pool example #865

Merged
merged 6 commits into from
Apr 4, 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
31 changes: 31 additions & 0 deletions v3/integrations/logcontext-v2/nrzap/nrzap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,37 @@ func TestBackgroundLogger(t *testing.T) {
})
}

func TestBackgroundLoggerSugared(t *testing.T) {
app := integrationsupport.NewTestApp(integrationsupport.SampleEverythingReplyFn,
newrelic.ConfigAppLogDecoratingEnabled(true),
newrelic.ConfigAppLogForwardingEnabled(true),
)

core := zapcore.NewCore(zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), zapcore.AddSync(os.Stdout), zap.InfoLevel)

backgroundCore, err := WrapBackgroundCore(core, app.Application)
if err != nil && err != ErrNilApp {
t.Fatal(err)
}

logger := zap.New(backgroundCore).Sugar()

err = errors.New("this is a test error")
msg := "this is a test error message"

// for background logging:
logger.Error(msg, zap.Error(err), zap.String("test-key", "test-val"))
logger.Sync()

app.ExpectLogEvents(t, []internal.WantLog{
{
Severity: zap.ErrorLevel.String(),
Message: `this is a test error message{error 26 0 this is a test error} {test-key 15 0 test-val <nil>}`,
Timestamp: internal.MatchAnyUnixMilli,
},
})
}

func TestBackgroundLoggerNilApp(t *testing.T) {
app := integrationsupport.NewTestApp(integrationsupport.SampleEverythingReplyFn,
newrelic.ConfigAppLogDecoratingEnabled(true),
Expand Down
19 changes: 14 additions & 5 deletions v3/integrations/nrpgx5/example/pgxpool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,28 @@ import (
"os"
"time"

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/newrelic/go-agent/v3/integrations/nrpgx5"
"github.com/newrelic/go-agent/v3/newrelic"
)

func main() {
cfg, err := pgxpool.ParseConfig("postgres://postgres:postgres@localhost:5432")
func NewPgxPool(ctx context.Context, dbURL string) (*pgxpool.Pool, error) {
cfg, err := pgxpool.ParseConfig(dbURL)
if err != nil {
panic(err)
return nil, err
}

cfg.BeforeConnect = func(_ context.Context, config *pgx.ConnConfig) error {
config.Tracer = nrpgx5.NewTracer()
return nil
}

cfg.ConnConfig.Tracer = nrpgx5.NewTracer()
db, err := pgxpool.NewWithConfig(context.Background(), cfg)
return pgxpool.NewWithConfig(ctx, cfg)
}

func main() {
db, err := NewPgxPool(context.Background(), "postgres://postgres:postgres@localhost:5432")
if err != nil {
panic(err)
}
Expand Down
20 changes: 20 additions & 0 deletions v3/newrelic/internal_txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,26 @@ func TestGetName(t *testing.T) {
}
}

func TestIgnoreTransaction(t *testing.T) {
replyfn := func(reply *internal.ConnectReply) {
reply.SetSampleEverything()
reply.EntityGUID = "entities-are-guid"
reply.TraceIDGenerator = internal.NewTraceIDGenerator(12345)
}
cfgfn := func(cfg *Config) {
cfg.AppName = "app-name"
cfg.DistributedTracer.Enabled = true
}
app := testApp(replyfn, cfgfn, t)
txn := app.StartTransaction("hello")
txn.Ignore()
txn.SetName("hello世界")
txn.NoticeError(errors.New("hi"))
txn.End()

app.ExpectTxnTraces(t, []internal.WantTxnTrace{})
}

func TestEmptyTransaction(t *testing.T) {
txn := &Transaction{}

Expand Down
Loading