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
Zerolog in Context #507
Merged
nr-swilloughby
merged 47 commits into
newrelic:develop
from
iamemilio:zerolog_in_context
Jun 29, 2022
Merged
Zerolog in Context #507
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
b10a13e
harvest log events
iamemilio 9565130
added logging in context metrics
iamemilio 8be7166
More Configurability and Testing
iamemilio 2fdbfbf
Zerolog logs in context support
iamemilio cc5e7cc
RecordLog documentation
iamemilio 6b7c071
Supportability Metrics
iamemilio 22adddd
button up zerolog plugin
iamemilio 514119b
correct config settings in readme and example
iamemilio d71870a
remove changes to short-lived-process-example
iamemilio 96b4b58
stress-tests
iamemilio f0b6b53
dont use time.Now().UnixMillisecond() due to old versions of go not s…
iamemilio 69e13d2
clean up the implementation of record logs to make it more user friendly
iamemilio c87eba3
clean up vet issues
iamemilio eaef803
priority inheretence
iamemilio d4806f8
fixing scale tests
iamemilio 320057e
make the linter happy for logging constants
iamemilio bcf0c6e
some clean up
iamemilio 317b6a0
remove scale tests
iamemilio 6ffb77a
Async Log Event Support
iamemilio 138d1bd
support timestamps on older go versions
iamemilio 1d326f4
Safe Reads
iamemilio 7f28d60
turns out that the synchronization is handled already
iamemilio 3607461
avoid alloc when creating logEvent
iamemilio d33379f
improve efficiency of getting harvest data by avoiding memAlloc
iamemilio cbc801f
Revert "improve efficiency of getting harvest data by avoiding memAlloc"
iamemilio 77fc73b
better benchmarks for log events
iamemilio e6083ed
more granular benchmarks for pinpointing json buffer writing runtime …
iamemilio 5594e30
logging readme improved
iamemilio 6cf172d
return go.mod to upstream
iamemilio 2e6700e
remove comparison to write string
iamemilio 0901501
group struct fields by type to avoid overallocation of contiguous memory
iamemilio f3b5b5b
piggyback log events on transactions to pool their priorities
iamemilio 561ab2d
style update for zerolog in context
iamemilio 900b6f4
if statements checking booleans simplified to just check value
iamemilio 9660625
safeguard memory for transaction log events
iamemilio ace7225
made go vet happy :)
iamemilio 458f9d0
autogenerate timestamps instead of failing
iamemilio 6f02ca7
cleanup and testing for autotimestamps
iamemilio 1f766f4
Fix Spelling mistakes
iamemilio 814b1d6
simplify supportability for zerolog
iamemilio 33f7217
a ConfigZerologEnabled() function call was separated from its heard
iamemilio d916ec5
app log trace id was set to span id
iamemilio 92a4a37
Additional Testing covering Application Function Calls
iamemilio ad1a864
fix sample size selection for logging
iamemilio 91ca115
cleaner server example
iamemilio d1385ff
make it more user friendly to record transaction logs
iamemilio f137814
background logs
iamemilio File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see span.id and trace.id being put on the log lines -- is there more enrichment that needs to be supported? Also, there are the three common attributes of entity GUID, entity name, and hostname.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By enrichment, I mean that we do not support enriching the local logs that get printed to console in this release. We add all of the enrichments to the logs, including entity GUID, entity name, and hostname at harvest