-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #531 from newrelic/develop
Release 3.17.0
- Loading branch information
Showing
55 changed files
with
2,524 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Zerolog In Context | ||
|
||
This plugin for zerolog implements the logs in context tooling for the go agent. This hook | ||
function can be added to any zerolog logger, and will automatically collect the log data | ||
from zerolog, and send it to New Relic through the go agent. The following Logging features | ||
are supported by this plugin in the current release: | ||
|
||
| Logging Feature | Supported | | ||
| ------- | --------- | | ||
| Forwarding | :heavy_check_mark: | | ||
| Metrics | :heavy_check_mark: | | ||
| Enrichment | :x: | | ||
|
||
## Installation | ||
|
||
The nrzerolog plugin, and the go-agent need to be integrated into your code | ||
in order to use this tool. Make sure to set `newrelic.ConfigAppLogForwardingEnabled(true)` | ||
in your config settings for the application. This will enable log forwarding | ||
in the go agent. If you want to disable metrics, set `newrelic.ConfigAppLogMetricsEnabled(false),`. | ||
Note that the agent sets the default number of logs per harverst cycle to 10000, but that | ||
number may be reduced by the server. You can manually set this number by setting | ||
`newrelic.ConfigAppLogForwardingMaxSamplesStored(123),`. | ||
|
||
The following example will shows how to install and set up your code to send logs to new relic from zerolog. | ||
|
||
```go | ||
|
||
import ( | ||
"github.com/rs/zerolog" | ||
"github.com/newrelic/go-agent/v3/newrelic" | ||
"github.com/newrelic/go-agent/v3/integrations/logcontext-v2/nrzerolog" | ||
) | ||
|
||
func main() { | ||
// Initialize a zerolog logger | ||
baseLogger := zerolog.New(os.Stdout) | ||
|
||
app, err := newrelic.NewApplication( | ||
newrelic.ConfigFromEnvironment(), | ||
newrelic.ConfigAppName("NRZerolog Example"), | ||
newrelic.ConfigInfoLogger(os.Stdout), | ||
newrelic.ConfigAppLogForwarding(true), | ||
) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
// Send logs to New Relic outside of a transaction | ||
nrHook := nrzerolog.NewRelicHook{ | ||
App: app, | ||
} | ||
|
||
// Wrap logger with New Relic Hook | ||
nrLogger := baseLogger.Hook(nrHook) | ||
nrLogger.Info().Msg("Hello World") | ||
|
||
// Send logs to New Relic inside of a transaction | ||
txn := app.StartTransaction("My Transaction") | ||
ctx := newrelic.NewContext(context.Background(), txn) | ||
|
||
nrTxnHook := nrzerolog.NewRelicHook{ | ||
App: app, | ||
Context: ctx, | ||
} | ||
|
||
txnLogger := baseLogger.Hook(nrTxnHook) | ||
txnLogger.Debug().Msg("This is a transaction log") | ||
|
||
txn.End() | ||
} | ||
``` | ||
|
||
## Usage | ||
|
||
Please enable the agent to ingest your logs by calling newrelic.ConfigAppLogForwardingEnabled(true), | ||
when setting up your application. This is not enabled by default. | ||
|
||
This integration for the zerolog logging frameworks uses a built in feature | ||
of the zerolog framework called hook functions. Zerolog loggers can be modified | ||
to have hook functions run on them before each time a write is executed. When a | ||
logger is hooked, meaning a hook function was added to that logger with the Hook() | ||
funciton, a copy of that logger is created with those changes. Note that zerolog | ||
will *never* attempt to verify that any hook functions have not been not duplicated, or | ||
that fields are not repeated in any way. As a result, we recommend that you create | ||
a base logger that is configured in the way you prefer to use zerolog. Then you | ||
create hooked loggers to send log data to New Relic from that base logger. | ||
|
||
The plugin captures the log level, and the message from zerolog. It will also collect | ||
distributed tracing data from your transaction context. At the moment the hook function is | ||
called in zerolog, a timestamp will be generated for your log. In most cases, this | ||
timestamp will be the same as the time posted in the zerolog log message, however it is possible that | ||
there could be a slight offset depending on the the performance of your system. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/newrelic/go-agent/v3/integrations/logcontext-v2/nrzerolog" | ||
"github.com/newrelic/go-agent/v3/newrelic" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
func main() { | ||
baseLogger := zerolog.New(os.Stdout) | ||
|
||
app, err := newrelic.NewApplication( | ||
newrelic.ConfigFromEnvironment(), | ||
newrelic.ConfigAppName("NRZerolog Example"), | ||
newrelic.ConfigInfoLogger(os.Stdout), | ||
newrelic.ConfigAppLogForwardingEnabled(true), | ||
) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
app.WaitForConnection(5 * time.Second) | ||
|
||
nrHook := nrzerolog.NewRelicHook{ | ||
App: app, | ||
} | ||
|
||
nrLogger := baseLogger.Hook(nrHook) | ||
nrLogger.Info().Msg("Hello World") | ||
|
||
// With transaction context | ||
txn := app.StartTransaction("My Transaction") | ||
ctx := newrelic.NewContext(context.Background(), txn) | ||
|
||
nrTxnHook := nrzerolog.NewRelicHook{ | ||
App: app, | ||
Context: ctx, | ||
} | ||
|
||
txnLogger := baseLogger.Hook(nrTxnHook) | ||
txnLogger.Debug().Msg("This is a transaction log") | ||
|
||
txn.End() | ||
|
||
nrLogger.Info().Msg("Goodbye") | ||
app.Shutdown(10 * time.Second) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module github.com/newrelic/go-agent/v3/integrations/logcontext-v2/nrzerolog | ||
|
||
go 1.15 | ||
|
||
require ( | ||
github.com/newrelic/go-agent/v3 v3.16.0 | ||
github.com/rs/zerolog v1.26.1 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package nrzerolog | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/newrelic/go-agent/v3/internal" | ||
"github.com/newrelic/go-agent/v3/newrelic" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
func init() { internal.TrackUsage("integration", "logcontext", "zerolog") } | ||
|
||
type NewRelicHook struct { | ||
App *newrelic.Application | ||
Context context.Context | ||
} | ||
|
||
func (h NewRelicHook) Run(e *zerolog.Event, level zerolog.Level, msg string) { | ||
var txn *newrelic.Transaction | ||
if h.Context != nil { | ||
txn = newrelic.FromContext(h.Context) | ||
} | ||
|
||
logLevel := "" | ||
if level == zerolog.NoLevel { | ||
logLevel = newrelic.LogSeverityUnknown | ||
} else { | ||
logLevel = level.String() | ||
} | ||
|
||
data := newrelic.LogData{ | ||
Severity: logLevel, | ||
Message: msg, | ||
} | ||
|
||
if txn != nil { | ||
txn.RecordLog(data) | ||
} else { | ||
h.App.RecordLog(data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.