Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Jun661314 opened this issue Apr 4, 2024 · 3 comments
Open

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

Jun661314 opened this issue Apr 4, 2024 · 3 comments
Labels

Comments

@Jun661314
Copy link

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 Jun661314 added the bug label Apr 4, 2024
@Jun661314
Copy link
Author

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

@nr-swilloughby
Copy link
Contributor

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants