Enable logs-in-context for Go

BETA

Combined with New Relic Logs, logs-in-context for the Go agent connects your Logs and APM data in New Relic, giving full context to high-level events, as well as providing high value data to specific log lines. This document explains how to enable this feature and get started using it.

New Relic's logs-in-context (linking logs directly to Applications, APM errors, and APM traces and spans) for New Relic Logs is currently available as a beta feature. Your use of the early access service is at your own risk. New Relic disclaims all warranties, express or implied, regarding the beta services.

Compatibility and requirements

To use New Relic Logs with the Go agent, ensure your configuration meets the following requirements:

New Relic Logs must be enabled and a compatible logging plugin must be installed prior to enabling logs-in-context. For more information, see Introduction to New Relic Logs.

Enable logs-in-context with New Relic Logs

To enable New Relic logs-in-context with Go:

Before enabling logs-in-context, you must have the appropriate feature flags. To be added, please contact rholzschuh@newrelic.com.

  1. Install or update the Go agent.
  2. Enable and configure the logging using Logrus plugin.
  3. Optional: Configure the Logrus plugin.
  4. Check for logging data.

Install or update the Go agent

Install or update to the most recent Go agent version, and enable Distributed tracing.

Enable and configure the logging using Logrus plugin

To enable and configure the Logrus plugin:

  1. This step assumes that you have imported the following packages in files where Logrus is configured:

    import (
        log "github.com/sirupsen/logrus"
        "github.com/newrelic/go-agent/_integrations/logcontext/nrlogrusplugin"
    )
  2. Set the Logrus formatter to nrlogrusplugin.ContextFormatter:

    logger := log.New()
    logger.SetFormatter(nrlogrusplugin.ContextFormatter{})
    
    Or, if you are using the Logrus standard logger:
    log.SetFormatter(nrlogrusplugin.ContextFormatter{})

    The logger will now look for a newrelic.Transaction inside its context and decorate logs accordingly.

  3. Update your standard logging call to include contextual logging and pass this to the logger.

    For example, instead of logger.Info("Hello New Relic!"), use

    ctx := newrelic.NewContext(context.Background(), txn)
    logger.WithContext(ctx).Info("Hello New Relic!")
    where txn is the current running transaction.

Optional configuration

Once you have enabled the Logrus plugin, you can configure how the agent sends data to New Relic. For more information on logging configuration, see the Logrus GoDocs.

Check for logging data

To verify that you have configured the extension correctly, run your application and verify that the logging you have configured contains the following:

  1. Includes trace.id and span.id fields
  2. Is properly-formatted JSON lines

If everything is configured correctly and your data is being reported, you should see data logs in the New Relic Logs UI using the query operator has: span.id/trace.id

.

What's next?

Once you've enabled logs-in-context, here are some potential next steps:

For more help

Recommendations for learning more: