nrb3

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 7, 2021 License: Apache-2.0 Imports: 4 Imported by: 1

README

v3/integrations/nrb3 GoDoc

Package nrb3 supports adding B3 headers to outgoing requests.

import "github.com/newrelic/go-agent/v3/integrations/nrb3"

For more information, see godocs.

Documentation

Overview

Package nrb3 supports adding B3 headers to outgoing requests.

When using the New Relic Go Agent, use this package if you want to add B3 headers ("X-B3-TraceId", etc., see https://github.com/openzipkin/b3-propagation) to outgoing requests.

Distributed tracing must be enabled (https://docs.newrelic.com/docs/understand-dependencies/distributed-tracing/enable-configure/enable-distributed-tracing) for B3 headers to be added properly.

This example demonstrates how to create a Zipkin reporter using the standard Zipkin http reporter (https://godoc.org/github.com/openzipkin/zipkin-go/reporter/http) to send Span data to New Relic. Follow this example when your application uses Zipkin for tracing (instead of the New Relic Go Agent) and you wish to send span data to the New Relic backend. The example assumes you have the environment variable NEW_RELIC_API_KEY set to your New Relic Insights Insert Key.

import (
	zipkin "github.com/openzipkin/zipkin-go"
	reporterhttp "github.com/openzipkin/zipkin-go/reporter/http"
)

func main() {
	reporter := reporterhttp.NewReporter(
		"https://trace-api.newrelic.com/trace/v1",
		reporterhttp.RequestCallback(func(req *http.Request) {
			req.Header.Add("X-Insert-Key", os.Getenv("NEW_RELIC_API_KEY"))
			req.Header.Add("Data-Format", "zipkin")
			req.Header.Add("Data-Format-Version", "2")
		}),
	)
	defer reporter.Close()

	// use the reporter to create a new tracer
	zipkin.NewTracer(reporter)
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRoundTripper

func NewRoundTripper(original http.RoundTripper) http.RoundTripper

NewRoundTripper creates an `http.RoundTripper` to instrument external requests. The RoundTripper returned creates an external segment and adds B3 tracing headers to each request if and only if a `newrelic.Transaction` (https://godoc.org/github.com/newrelic/go-agent#Transaction) is found in the `http.Request`'s context. It then delegates to the original RoundTripper provided (or http.DefaultTransport if none is provided).

Example
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/newrelic/go-agent/v3/integrations/nrb3"
	"github.com/newrelic/go-agent/v3/newrelic"
)

func currentTxn() *newrelic.Transaction {
	return nil
}

func main() {
	// When defining the client, set the Transport to the NewRoundTripper. This
	// will create ExternalSegments and add B3 headers for each request.
	client := &http.Client{
		Transport: nrb3.NewRoundTripper(nil),
	}

	// Distributed Tracing must be enabled for this application.
	txn := currentTxn()

	req, err := http.NewRequest("GET", "http://example.com", nil)
	if nil != err {
		log.Fatalln(err)
	}

	// Be sure to add the transaction to the request context.  This step is
	// required.
	req = newrelic.RequestWithTransactionContext(req, txn)
	resp, err := client.Do(req)
	if nil != err {
		log.Fatalln(err)
	}

	defer resp.Body.Close()
	fmt.Println(resp.StatusCode)
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL