go-agent
go-agent copied to clipboard
go agent , but not see span, please help me ,thanks
picture
Description
use /five api , but not see span
my golang code
http.HandleFunc(newrelic.WrapHandleFunc(app, "/five", five))
func five(w http.ResponseWriter, r *http.Request) { // txn := app.StartTransaction("tThree") txn := newrelic.FromContext(r.Context()) fmt.Println("use five")
// add span
s1 := newrelic.StartSegment(txn, "my_span_five")
defer s1.End()
fmt.Println("ok five 1")
// time.Sleep(3 * time.Second)
client := &http.Client{}
client.Transport = newrelic.NewRoundTripper(client.Transport)
request, _ := http.NewRequest("GET", "http://127.0.0.1:9091/ohterhappytwo", nil)
// request, _ := http.NewRequest("GET", "http://127.0.0.1:8080/two", nil)
request = newrelic.RequestWithTransactionContext(request, txn)
response, _ := client.Do(request)
fmt.Println("resp--", response)
// add span
s2 := newrelic.StartSegment(txn, "my_span_four_five")
defer s2.End()
fmt.Println("ok three")
// time.Sleep(12 * time.Second)
defer txn.End()
}
Environment
win11
win11 github.com/newrelic/go-agent/v3 v3.31.0
Remember that in Go, deferred functions are executed in reverse order (the last one deferred is the first one executed), as they are stored on a stack. This means that the txn.End()
happens first when your function exits, so by the time it executes s2.End()
, there isn't a transaction anymore for it to be reported in. You need to end the segment before you end the transaction.
Also note that newrelic.StartSegment
is deprecated. use txn.StartSegment
instead.