|
| 1 | +package nrzerolog |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/newrelic/go-agent/v3/internal" |
| 10 | + "github.com/newrelic/go-agent/v3/internal/integrationsupport" |
| 11 | + "github.com/newrelic/go-agent/v3/newrelic" |
| 12 | + |
| 13 | + "github.com/rs/zerolog" |
| 14 | +) |
| 15 | + |
| 16 | +func newLogger(out io.Writer, app *newrelic.Application) zerolog.Logger { |
| 17 | + logger := zerolog.New(out) |
| 18 | + return logger.Hook(NewRelicHook{ |
| 19 | + App: app, |
| 20 | + }) |
| 21 | +} |
| 22 | + |
| 23 | +func newTxnLogger(out io.Writer, app *newrelic.Application, ctx context.Context) zerolog.Logger { |
| 24 | + logger := zerolog.New(out) |
| 25 | + return logger.Hook(NewRelicHook{ |
| 26 | + App: app, |
| 27 | + Context: ctx, |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +func BenchmarkZerolog(b *testing.B) { |
| 32 | + log := zerolog.New(bytes.NewBuffer([]byte(""))) |
| 33 | + |
| 34 | + b.ResetTimer() |
| 35 | + b.ReportAllocs() |
| 36 | + |
| 37 | + for i := 0; i < b.N; i++ { |
| 38 | + log.Info().Msg("This is a test log") |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func BenchmarkZerologLoggingDisabled(b *testing.B) { |
| 43 | + app := integrationsupport.NewTestApp(integrationsupport.SampleEverythingReplyFn, newrelic.ConfigAppLogEnabled(false)) |
| 44 | + log := newLogger(bytes.NewBuffer([]byte("")), app.Application) |
| 45 | + |
| 46 | + b.ResetTimer() |
| 47 | + b.ReportAllocs() |
| 48 | + |
| 49 | + for i := 0; i < b.N; i++ { |
| 50 | + log.Info().Msg("This is a test log") |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func BenchmarkZerologLogForwarding(b *testing.B) { |
| 55 | + app := integrationsupport.NewTestApp(integrationsupport.SampleEverythingReplyFn, newrelic.ConfigAppLogForwardingEnabled(true)) |
| 56 | + log := newLogger(bytes.NewBuffer([]byte("")), app.Application) |
| 57 | + |
| 58 | + b.ResetTimer() |
| 59 | + b.ReportAllocs() |
| 60 | + |
| 61 | + for i := 0; i < b.N; i++ { |
| 62 | + log.Info().Msg("This is a test log") |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/* |
| 67 | +
|
| 68 | +func BenchmarkFormattingWithOutTransaction(b *testing.B) { |
| 69 | + app := integrationsupport.NewTestApp(integrationsupport.SampleEverythingReplyFn, newrelic.ConfigAppLogDecoratingEnabled(true)) |
| 70 | + log := newLogger(bytes.NewBuffer([]byte("")), app.Application) |
| 71 | +
|
| 72 | + b.ResetTimer() |
| 73 | + b.ReportAllocs() |
| 74 | +
|
| 75 | + for i := 0; i < b.N; i++ { |
| 76 | + log.Info().Msg("Hello World!") |
| 77 | + } |
| 78 | +} |
| 79 | +
|
| 80 | +func BenchmarkFormattingWithTransaction(b *testing.B) { |
| 81 | + app := integrationsupport.NewTestApp(integrationsupport.SampleEverythingReplyFn, newrelic.ConfigAppLogDecoratingEnabled(true)) |
| 82 | + txn := app.StartTransaction("TestLogDistributedTracingDisabled") |
| 83 | + defer txn.End() |
| 84 | + out := bytes.NewBuffer([]byte{}) |
| 85 | + ctx := newrelic.NewContext(context.Background(), txn) |
| 86 | + log := newTxnLogger(out, app.Application, ctx) |
| 87 | +
|
| 88 | +
|
| 89 | + b.ResetTimer() |
| 90 | + b.ReportAllocs() |
| 91 | +
|
| 92 | + for i := 0; i < b.N; i++ { |
| 93 | + log.Info().Msg("Hello World!") |
| 94 | + } |
| 95 | +} |
| 96 | +*/ |
| 97 | + |
| 98 | +func TestBackgroundLog(t *testing.T) { |
| 99 | + app := integrationsupport.NewTestApp(integrationsupport.SampleEverythingReplyFn, |
| 100 | + newrelic.ConfigAppLogDecoratingEnabled(true), |
| 101 | + newrelic.ConfigAppLogForwardingEnabled(true), |
| 102 | + ) |
| 103 | + out := bytes.NewBuffer([]byte{}) |
| 104 | + log := newLogger(out, app.Application) |
| 105 | + message := "Hello World!" |
| 106 | + log.Info().Msg(message) |
| 107 | + |
| 108 | + // Un-comment when local decorating enabled |
| 109 | + /* |
| 110 | + logcontext.ValidateDecoratedOutput(t, out, &logcontext.DecorationExpect{ |
| 111 | + EntityGUID: integrationsupport.TestEntityGUID, |
| 112 | + Hostname: host, |
| 113 | + EntityName: integrationsupport.SampleAppName, |
| 114 | + }) |
| 115 | + */ |
| 116 | + |
| 117 | + app.ExpectLogEvents(t, []internal.WantLog{ |
| 118 | + { |
| 119 | + Severity: zerolog.InfoLevel.String(), |
| 120 | + Message: message, |
| 121 | + Timestamp: internal.MatchAnyUnixMilli, |
| 122 | + }, |
| 123 | + }) |
| 124 | +} |
| 125 | + |
| 126 | +func TestLogEmptyContext(t *testing.T) { |
| 127 | + app := integrationsupport.NewTestApp(integrationsupport.SampleEverythingReplyFn, |
| 128 | + newrelic.ConfigAppLogDecoratingEnabled(true), |
| 129 | + newrelic.ConfigAppLogForwardingEnabled(true), |
| 130 | + ) |
| 131 | + out := bytes.NewBuffer([]byte{}) |
| 132 | + log := newTxnLogger(out, app.Application, context.Background()) |
| 133 | + message := "Hello World!" |
| 134 | + log.Info().Msg(message) |
| 135 | + |
| 136 | + // Un-comment when local decorating enabled |
| 137 | + /* |
| 138 | + logcontext.ValidateDecoratedOutput(t, out, &logcontext.DecorationExpect{ |
| 139 | + EntityGUID: integrationsupport.TestEntityGUID, |
| 140 | + Hostname: host, |
| 141 | + EntityName: integrationsupport.SampleAppName, |
| 142 | + }) */ |
| 143 | + |
| 144 | + app.ExpectLogEvents(t, []internal.WantLog{ |
| 145 | + { |
| 146 | + Severity: zerolog.InfoLevel.String(), |
| 147 | + Message: message, |
| 148 | + Timestamp: internal.MatchAnyUnixMilli, |
| 149 | + }, |
| 150 | + }) |
| 151 | +} |
| 152 | + |
| 153 | +func TestLogDebugLevel(t *testing.T) { |
| 154 | + app := integrationsupport.NewTestApp(integrationsupport.SampleEverythingReplyFn, |
| 155 | + newrelic.ConfigAppLogDecoratingEnabled(true), |
| 156 | + newrelic.ConfigAppLogForwardingEnabled(true), |
| 157 | + ) |
| 158 | + out := bytes.NewBuffer([]byte{}) |
| 159 | + log := newTxnLogger(out, app.Application, context.Background()) |
| 160 | + message := "Hello World!" |
| 161 | + log.Print(message) |
| 162 | + |
| 163 | + // Un-comment when local decorating enabled |
| 164 | + /* |
| 165 | + logcontext.ValidateDecoratedOutput(t, out, &logcontext.DecorationExpect{ |
| 166 | + EntityGUID: integrationsupport.TestEntityGUID, |
| 167 | + Hostname: host, |
| 168 | + EntityName: integrationsupport.SampleAppName, |
| 169 | + }) */ |
| 170 | + |
| 171 | + app.ExpectLogEvents(t, []internal.WantLog{ |
| 172 | + { |
| 173 | + Severity: zerolog.DebugLevel.String(), |
| 174 | + Message: message, |
| 175 | + Timestamp: internal.MatchAnyUnixMilli, |
| 176 | + }, |
| 177 | + }) |
| 178 | +} |
| 179 | + |
| 180 | +func TestLogInContext(t *testing.T) { |
| 181 | + app := integrationsupport.NewTestApp(integrationsupport.SampleEverythingReplyFn, |
| 182 | + newrelic.ConfigAppLogDecoratingEnabled(true), |
| 183 | + newrelic.ConfigAppLogForwardingEnabled(true), |
| 184 | + ) |
| 185 | + out := bytes.NewBuffer([]byte{}) |
| 186 | + txn := app.StartTransaction("test txn") |
| 187 | + ctx := newrelic.NewContext(context.Background(), txn) |
| 188 | + log := newTxnLogger(out, app.Application, ctx) |
| 189 | + message := "Hello World!" |
| 190 | + log.Info().Msg(message) |
| 191 | + |
| 192 | + // Un-comment when local decorating enabled |
| 193 | + /* |
| 194 | + logcontext.ValidateDecoratedOutput(t, out, &logcontext.DecorationExpect{ |
| 195 | + EntityGUID: integrationsupport.TestEntityGUID, |
| 196 | + Hostname: host, |
| 197 | + EntityName: integrationsupport.SampleAppName, |
| 198 | + TraceID: txn.GetLinkingMetadata().TraceID, |
| 199 | + SpanID: txn.GetLinkingMetadata().SpanID, |
| 200 | + }) |
| 201 | + */ |
| 202 | + |
| 203 | + txn.ExpectLogEvents(t, []internal.WantLog{ |
| 204 | + { |
| 205 | + Severity: zerolog.InfoLevel.String(), |
| 206 | + Message: message, |
| 207 | + Timestamp: internal.MatchAnyUnixMilli, |
| 208 | + SpanID: txn.GetLinkingMetadata().SpanID, |
| 209 | + TraceID: txn.GetLinkingMetadata().TraceID, |
| 210 | + }, |
| 211 | + }) |
| 212 | + |
| 213 | + txn.End() |
| 214 | +} |
0 commit comments