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

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

Merged
merged 16 commits into from Jun 22, 2020

Conversation

HazAT
Copy link
Member

@HazAT HazAT commented Jun 11, 2020

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:

const span = Sentry.getCurrentHub().getScope().getSpan();

This introduces scope.getTransaction.

const transaction = 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 owner June 11, 2020 09:49
@HazAT HazAT self-assigned this Jun 11, 2020
@getsentry-bot
Copy link
Contributor

getsentry-bot commented Jun 11, 2020

Messages
📖

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

📖 ✅ TSLint passed

Generated by 🚫 dangerJS against fd23147

@HazAT HazAT changed the title feat: Add Sentry.getTransaction feat: Add Sentry.getSpan Jun 15, 2020
Copy link
Contributor

@rhcarvalho rhcarvalho left a 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

packages/apm/src/span.ts Outdated Show resolved Hide resolved
packages/apm/src/span.ts Outdated Show resolved Hide resolved
packages/minimal/src/index.ts Outdated Show resolved Hide resolved
@HazAT HazAT changed the title feat: Add Sentry.getSpan feat: Add scope.getTransaction Jun 16, 2020
@HazAT HazAT changed the title feat: Add scope.getTransaction feat: Add scope.getTransaction, rename scope.setTransaction -> setTransactionName Jun 16, 2020
CHANGELOG.md Outdated Show resolved Hide resolved
packages/hub/src/scope.ts Show resolved Hide resolved
Copy link
Contributor

@rhcarvalho rhcarvalho left a 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()

CHANGELOG.md Outdated Show resolved Hide resolved
packages/apm/src/span.ts Outdated Show resolved Hide resolved
packages/apm/test/hub.test.ts Outdated Show resolved Hide resolved
Comment on lines 92 to 95
/**
* Returns the current set span if there is one
*/
getSpan(): Span | undefined;
Copy link
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
Member Author

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
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>
CHANGELOG.md Outdated Show resolved Hide resolved
packages/apm/src/span.ts Outdated Show resolved Hide resolved
packages/apm/src/span.ts Outdated Show resolved Hide resolved
packages/apm/test/hub.test.ts Outdated Show resolved Hide resolved
@untitaker
Copy link
Member

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

@dcramer
Copy link
Member

dcramer commented Jun 16, 2020

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

@dcramer I think rodolfo wrote the same thing

Copy link
Contributor

@rhcarvalho rhcarvalho left a 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).

packages/hub/src/scope.ts Show resolved Hide resolved
packages/types/src/scope.ts Show resolved Hide resolved
packages/types/src/span.ts Outdated Show resolved Hide resolved
@@ -11,6 +11,28 @@ describe('Hub', () => {
jest.useRealTimers();
});

describe('getTransaction', () => {
Copy link
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
Member Author

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

Choose a reason for hiding this comment

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

kilobytes, kilobytes 😶

*/
public getTransaction(): Transaction | undefined {
const span = this.getSpan() as Span & { spanRecorder: { spans: Span[] } };
if (span) {
Copy link
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
Copy link
Contributor

@kamilogorek kamilogorek left a 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.

Sign up for free to 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.

None yet

7 participants