Skip to content

feat: Add scope.getTransaction, rename scope.setTransaction -> setTransactionName - #2668

Merged
HazAT merged 16 commits into
masterfrom
hazat/getTransaction
Jun 22, 2020
Merged

feat: Add scope.getTransaction, rename scope.setTransaction -> setTransactionName#2668
HazAT merged 16 commits into
masterfrom
hazat/getTransaction

Conversation

@HazAT

@HazATHazAT commented Jun 11, 2020

Copy link
Copy Markdown
Member

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:

constspan=Sentry.getCurrentHub().getScope().getSpan();

This introduces scope.getTransaction.

consttransaction=Sentry.getCurrentHub().getScope().getTransaction();

which could return undefined if there is no Transaction going.

Also (in the title)
rename scope.setTransaction -> setTransactionName to make it clear that getTransaction doesn't return a string.

@HazAT
HazAT requested a review from kamilogorek as a code ownerJune 11, 2020 09:49
@HazATHazAT self-assigned this Jun 11, 2020
@getsentry-bot

getsentry-bot commented Jun 11, 2020

Copy link
Copy Markdown
Contributor
Messages
📖

@sentry/browser bundle gzip'ed minified size: (ES5: 17.1387 kB) (ES6: 16.2529 kB)

📖✅ TSLint passed

Generated by 🚫 dangerJS against fd23147

Comment threadpackages/apm/src/hubextensions.ts Outdated
@HazATHazAT changed the title feat: Add Sentry.getTransactionfeat: Add Sentry.getSpanJun 15, 2020

@rhcarvalhorhcarvalho left a comment

Copy link
Copy Markdown
Contributor

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

Comment threadpackages/apm/src/span.ts Outdated
Comment threadpackages/apm/src/span.ts Outdated
Comment threadpackages/minimal/src/index.ts Outdated
Comment threadpackages/apm/src/span.ts Outdated
@HazATHazAT changed the title feat: Add Sentry.getSpanfeat: Add scope.getTransactionJun 16, 2020
@HazATHazAT changed the title feat: Add scope.getTransactionfeat: Add scope.getTransaction, rename scope.setTransaction -> setTransactionNameJun 16, 2020
Comment threadCHANGELOG.md Outdated
Comment threadpackages/hub/src/scope.ts

@rhcarvalhorhcarvalho left a comment

Copy link
Copy Markdown
Contributor

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.

withTransaction(name="...") ast:
child=t.startChild(...)
# ...child.finish()

Comment threadCHANGELOG.md Outdated
Comment threadpackages/apm/src/span.ts Outdated
Comment threadpackages/apm/test/hub.test.ts Outdated
Comment on lines +92 to +95
/**
* Returns the current set span if there is one
*/
getSpan(): Span | undefined;

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
MemberAuthor

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.

Copy link
Copy Markdown
Contributor

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 <AbhiPrasad@users.noreply.github.com>
Comment threadCHANGELOG.md Outdated
Comment threadpackages/apm/src/span.ts Outdated
Comment threadpackages/apm/src/span.ts Outdated
Comment threadpackages/apm/test/hub.test.ts Outdated
@untitaker

Copy link
Copy Markdown
Member

Sorry I wrote that review in the morning and forgot to submit

@dcramer

Copy link
Copy Markdown
Member

@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.

@untitaker

Copy link
Copy Markdown
Member

@dcramer I think rodolfo wrote the same thing

@rhcarvalhorhcarvalho left a comment

Copy link
Copy Markdown
Contributor

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).

Comment threadpackages/hub/src/scope.ts
Comment threadpackages/types/src/scope.ts
Comment threadpackages/types/src/span.ts Outdated
jest.useRealTimers();
});

describe('getTransaction', () => {

Copy link
Copy Markdown
Contributor

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().

Copy link
Copy Markdown
MemberAuthor

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

*/
public setTransaction(transaction?: string): this {
this._transaction = transaction;
public setTransactionName(name?: string): this {

Copy link
Copy Markdown
Contributor

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kilobytes, kilobytes 😶

Comment threadpackages/hub/src/scope.ts Outdated
*/
public getTransaction(): Transaction | undefined {
const span = this.getSpan() as Span & { spanRecorder: { spans: Span[] } };
if (span) {

Copy link
Copy Markdown
Contributor

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

@kamilogorek
kamilogorek self-requested a review June 22, 2020 08:58

@kamilogorekkamilogorek left a comment

Copy link
Copy Markdown
Contributor

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.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@HazAT@getsentry-bot@untitaker@dcramer@rhcarvalho@kamilogorek@AbhiPrasad