-
Notifications
You must be signed in to change notification settings - Fork 295
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 #546 from newrelic/develop
Release 3.18.0
- Loading branch information
Showing
60 changed files
with
1,666 additions
and
214 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"github.com/newrelic/go-agent/v3/integrations/logcontext-v2/nrlogrus" | ||
newrelic "github.com/newrelic/go-agent/v3/newrelic" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func doFunction2(txn *newrelic.Transaction, e *logrus.Entry) { | ||
defer txn.StartSegment("doFunction2").End() | ||
e.Error("In doFunction2") | ||
} | ||
|
||
func doFunction1(txn *newrelic.Transaction, e *logrus.Entry) { | ||
defer txn.StartSegment("doFunction1").End() | ||
e.Trace("In doFunction1") | ||
doFunction2(txn, e) | ||
} | ||
|
||
func main() { | ||
app, err := newrelic.NewApplication( | ||
newrelic.ConfigAppName("Logrus Logs In Context Example"), | ||
newrelic.ConfigLicense(os.Getenv("NEW_RELIC_LICENSE_KEY")), | ||
newrelic.ConfigInfoLogger(os.Stdout), | ||
newrelic.ConfigAppLogForwardingEnabled(true), | ||
|
||
// If you wanted to forward your logs using a log forwarder instead | ||
// newrelic.ConfigAppLogDecoratingEnabled(true), | ||
// newrelic.ConfigAppLogForwardingEnabled(false), | ||
) | ||
if nil != err { | ||
log.Panic("Failed to create application", err) | ||
} | ||
|
||
log := logrus.New() | ||
log.SetLevel(logrus.TraceLevel) | ||
// Enable New Relic log decoration | ||
log.SetFormatter(nrlogrus.NewFormatter(app, &logrus.TextFormatter{})) | ||
log.Trace("waiting for connection to New Relic...") | ||
|
||
err = app.WaitForConnection(10 * time.Second) | ||
if nil != err { | ||
log.Panic("Failed to connect application", err) | ||
} | ||
defer app.Shutdown(10 * time.Second) | ||
log.Info("application connected to New Relic") | ||
log.Debug("Starting transaction now") | ||
txn := app.StartTransaction("main") | ||
|
||
// Add the transaction context to the logger. Only once this happens will | ||
// the logs be properly decorated with all required fields. | ||
e := log.WithContext(newrelic.NewContext(context.Background(), txn)) | ||
|
||
doFunction1(txn, e) | ||
|
||
e.Info("Ending transaction") | ||
txn.End() | ||
} |
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,64 @@ | ||
// Copyright 2020 New Relic Corporation. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package nrlogrus | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/newrelic/go-agent/v3/internal" | ||
newrelic "github.com/newrelic/go-agent/v3/newrelic" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func init() { internal.TrackUsage("integration", "logcontext-v2", "logrus") } | ||
|
||
// ContextFormatter is a `logrus.Formatter` that will format logs for sending | ||
// to New Relic. | ||
type ContextFormatter struct { | ||
app *newrelic.Application | ||
formatter logrus.Formatter | ||
} | ||
|
||
func NewFormatter(app *newrelic.Application, formatter logrus.Formatter) ContextFormatter { | ||
return ContextFormatter{ | ||
app: app, | ||
formatter: formatter, | ||
} | ||
} | ||
|
||
// Format renders a single log entry. | ||
func (f ContextFormatter) Format(e *logrus.Entry) ([]byte, error) { | ||
logData := newrelic.LogData{ | ||
Severity: e.Level.String(), | ||
Message: e.Message, | ||
} | ||
|
||
logBytes, err := f.formatter.Format(e) | ||
if err != nil { | ||
return nil, err | ||
} | ||
logBytes = bytes.TrimRight(logBytes, "\n") | ||
b := bytes.NewBuffer(logBytes) | ||
|
||
ctx := e.Context | ||
var txn *newrelic.Transaction | ||
if ctx != nil { | ||
txn = newrelic.FromContext(ctx) | ||
} | ||
if txn != nil { | ||
txn.RecordLog(logData) | ||
err := newrelic.EnrichLog(b, newrelic.FromTxn(txn)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
f.app.RecordLog(logData) | ||
err := newrelic.EnrichLog(b, newrelic.FromApp(f.app)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
b.WriteString("\n") | ||
return b.Bytes(), nil | ||
} |
Oops, something went wrong.