github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrb3/example_test.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package nrb3
     5  
     6  import (
     7  	"fmt"
     8  	"log"
     9  	"net/http"
    10  	"os"
    11  
    12  	newrelic "github.com/newrelic/go-agent"
    13  	"github.com/openzipkin/zipkin-go"
    14  	reporterhttp "github.com/openzipkin/zipkin-go/reporter/http"
    15  )
    16  
    17  func currentTxn() newrelic.Transaction {
    18  	return nil
    19  }
    20  
    21  func ExampleNewRoundTripper() {
    22  	// When defining the client, set the Transport to the NewRoundTripper. This
    23  	// will create ExternalSegments and add B3 headers for each request.
    24  	client := &http.Client{
    25  		Transport: NewRoundTripper(nil),
    26  	}
    27  
    28  	// Distributed Tracing must be enabled for this application.
    29  	txn := currentTxn()
    30  
    31  	req, err := http.NewRequest("GET", "http://example.com", nil)
    32  	if nil != err {
    33  		log.Fatalln(err)
    34  	}
    35  
    36  	// Be sure to add the transaction to the request context.  This step is
    37  	// required.
    38  	req = newrelic.RequestWithTransactionContext(req, txn)
    39  	resp, err := client.Do(req)
    40  	if nil != err {
    41  		log.Fatalln(err)
    42  	}
    43  
    44  	defer resp.Body.Close()
    45  	fmt.Println(resp.StatusCode)
    46  }
    47  
    48  // This example demonstrates how to create a Zipkin reporter using the standard
    49  // Zipkin http reporter
    50  // (https://godoc.org/github.com/openzipkin/zipkin-go/reporter/http) to send
    51  // Span data to New Relic.  Follow this example when your application uses
    52  // Zipkin for tracing (instead of the New Relic Go Agent) and you wish to send
    53  // span data to the New Relic backend.  The example assumes you have the
    54  // environment variable NEW_RELIC_API_KEY set to your New Relic Insights Insert
    55  // Key.
    56  func Example_zipkinReporter() {
    57  	// import (
    58  	//    reporterhttp "github.com/openzipkin/zipkin-go/reporter/http"
    59  	// )
    60  	reporter := reporterhttp.NewReporter(
    61  		"https://trace-api.newrelic.com/trace/v1",
    62  		reporterhttp.RequestCallback(func(req *http.Request) {
    63  			req.Header.Add("X-Insert-Key", os.Getenv("NEW_RELIC_API_KEY"))
    64  			req.Header.Add("Data-Format", "zipkin")
    65  			req.Header.Add("Data-Format-Version", "2")
    66  		}),
    67  	)
    68  	defer reporter.Close()
    69  
    70  	// use the reporter to create a new tracer
    71  	zipkin.NewTracer(reporter)
    72  }