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

No way to check if a transaction has ended #995

Open
frknikiz opened this issue Jan 30, 2025 · 0 comments · May be fixed by #998
Open

No way to check if a transaction has ended #995

frknikiz opened this issue Jan 30, 2025 · 0 comments · May be fixed by #998

Comments

@frknikiz
Copy link

Summary

This feature request proposes adding a new method to the New Relic Go agent's Transaction object that allows developers to check if a transaction has been ended.

Desired Behaviour

Currently, there is no way to directly determine if a transaction has been ended, especially when the End() call is made in a separate goroutine. This can lead to uncertainty and potential issues in tracking the status of transactions.

The desired behavior is to have a method, such as IsEnded(), that returns a boolean value indicating whether the End() method has been called on the transaction. This would allow developers to reliably check the transaction's status and handle it accordingly.

For example:

txn:= newrelic.FromContext(r.Context())

//... some code...

go func() {
  //... some code...
  txn.End()
}()

//... some code...

if txn.IsEnded() {
  // Transaction has ended
} else {
  // Transaction is still active
}

Possible Solution

Examining the current Transaction struct reveals a finished boolean field within the nested txn struct, which appears to track the transaction's end state.

type Transaction struct {
    Private any
    thread  *thread
}

type thread struct {
    *txn
    // ... other fields
}

type txn struct {
    // ... other fields
    finished           bool
    // ... other fields
}

Leveraging this existing field, the IsEnded() method could be implemented as follows:

func (t *Transaction) IsEnded() bool {
  if t == nil || t.thread == nil || t.thread.txn == nil {
    return true
  }
  return t.thread.txn.finished
}

This method first handles potential nil cases to prevent panics and then directly accesses the finished field to provide the desired functionality.

Additional context

This feature is important for applications that heavily rely on asynchronous processing and goroutines. In such scenarios, it can be difficult to keep track of the status of transactions, and the proposed method would provide a clear and reliable way to do so. This would help in debugging, ensuring proper transaction completion, and managing resources effectively.

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

Successfully merging a pull request may close this issue.

1 participant