-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: Add scope.getTransaction, rename scope.setTransaction -> setTransactionName #2668
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel getSpan
that does not return a Span
is an awkward API addition.
For a parallel, see getScope(): Scope | undefined
and withScope(callback: (scope: Scope) => void): void
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please let's not do getXXX
that does not return XXX
. This is confusing naming right in the face of API users. We already have withXXX
for APIs that take a callback and return nothing.
This potential withTransaction
on the Scope only solve the problem of setting the transaction name half-way.
Instead of Sentry.getCurrentHub().getScope().getSpan()
, we still need to reach for the Scope
with Sentry.getCurrentHub().withScope
or ...configureScope
which makes using it quite verbose. I noted we moved away from adding something to the static API as we had in a previous iteration of this PR?
I have a proposal:
Sentry.withTransaction((t) => {
t.name = "...";
})
In fact, this pattern would be similar to doing context managers in Python -- Sentry.withTransaction
could start a new transaction and finish it after the callback returns -- users will never forget to call finish and have access to change the name directly.
with Transaction(name="...") as t:
child = t.startChild(...)
# ...
child.finish()
packages/types/src/scope.ts
Outdated
/** | ||
* Returns the current set span if there is one | ||
*/ | ||
getSpan(): Span | undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still want getSpan
or is this a left over?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still want it, right now we don't have a case in JS where we need it but I think as soon as we start thinking about adding ORM (database) support to node we'll have the same need as python has right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's interesting. Last time I looked at the Python SDK, it only had one slot to store a "span" in the scope and it is either a span or transaction.
If we want to have a slot for both and distinguish what getSpan
and getTransaction
do, we need to change the scope impl.
cc @untitaker
Co-authored-by: Abhijeet Prasad <[email protected]>
Sorry I wrote that review in the morning and forgot to submit |
@rhcarvalho the main need is to not create a new transaction, but rather capture the active transaction so that we can mutate it. Given that case the context manager style approach wouldn't be helpful here. |
@dcramer I think rodolfo wrote the same thing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR description needs an update callback => getter.
The current motivation seems a bit off, since we don't really solve for shortening Sentry.getCurrentHub().getScope().getSpan()
(it can already be shorter today with Sentry.withScope
).
Seems that the major change being proposed here is making getSpan
and getTransaction
"blessed for public use" (getSpan
was considered internal and is still documented as such).
@@ -11,6 +11,28 @@ describe('Hub', () => { | |||
jest.useRealTimers(); | |||
}); | |||
|
|||
describe('getTransaction', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests are not technically "correct". defined/undefined test should be moved to scope.test.ts
, as this functionality is owned by the Scope
, and tests below should only assert that a given method was called on the scope. expect(s.getTransaction).toBeCalled()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to scope.test.ts
@@ -185,12 +186,20 @@ export class Scope implements ScopeInterface { | |||
/** | |||
* @inheritDoc | |||
*/ | |||
public setTransaction(transaction?: string): this { | |||
this._transaction = transaction; | |||
public setTransactionName(name?: string): this { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
offtopic: This whole transaction thing will be very confused (and possibly broken) if someone uses Sentry.Integrations.Transaction
in node env.
* Can be removed in major version. | ||
* @deprecated in favor of {@link this.setTransactionName} | ||
*/ | ||
public setTransaction(name?: string): this { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kilobytes, kilobytes 😶
packages/hub/src/scope.ts
Outdated
*/ | ||
public getTransaction(): Transaction | undefined { | ||
const span = this.getSpan() as Span & { spanRecorder: { spans: Span[] } }; | ||
if (span) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be inline in one check, as there're no other if
branches
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved code wise. I didn't take part in API design, so don't sue me please.
Motivation
We want to have an easy way for users to retrieve a Transaction that is set on the Scope.
Right now we only have an API for retrieving the Span:
This introduces
scope.getTransaction
.which could return
undefined
if there is no Transaction going.Also (in the title)
rename
scope.setTransaction
->setTransactionName
to make it clear thatgetTransaction
doesn't return astring
.