go-agent icon indicating copy to clipboard operation
go-agent copied to clipboard

go agent , but not see span, please help me ,thanks

Open Jun661314 opened this issue 10 months ago • 3 comments

picture

Screenshot_1

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

Jun661314 avatar Apr 04 '24 13:04 Jun661314

win11 github.com/newrelic/go-agent/v3 v3.31.0

Jun661314 avatar Apr 04 '24 13:04 Jun661314

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.

nr-swilloughby avatar Apr 04 '24 16:04 nr-swilloughby

Also note that newrelic.StartSegment is deprecated. use txn.StartSegment instead.

nr-swilloughby avatar Apr 04 '24 16:04 nr-swilloughby