Skip to content

Commit 33a6232

Browse files
authored
feat(core): Lookup client on current scope, not hub (#10635)
One more step to avoid the hub... Note: This showed a potential footgun (?), where if you pass a client-less scope to `startSpan()`, no span will be created (because no client = no tracing, and the given scope will be made the current scope in the callback). this is correct behavior here, but _could_ be a bit unexpected ("why is no span being created here??"). We could think about warning if a client-less scope is passed in there, but not sure if that's worth it...
1 parent 0e7ab1e commit 33a6232

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

packages/core/src/exports.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,7 @@ export async function close(timeout?: number): Promise<boolean> {
349349
* Get the currently active client.
350350
*/
351351
export function getClient<C extends Client>(): C | undefined {
352-
// eslint-disable-next-line deprecation/deprecation
353-
return getCurrentHub().getClient<C>();
352+
return getCurrentScope().getClient<C>();
354353
}
355354

356355
/**

packages/core/src/scope.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ export class Scope implements ScopeInterface {
163163
*
164164
* It is generally recommended to use the global function `Sentry.getClient()` instead, unless you know what you are doing.
165165
*/
166-
public getClient(): Client | undefined {
167-
return this._client;
166+
public getClient<C extends Client>(): C | undefined {
167+
return this._client as C | undefined;
168168
}
169169

170170
/**

packages/core/test/lib/tracing/trace.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
spanToJSON,
1010
withScope,
1111
} from '../../../src';
12-
import { Scope } from '../../../src/scope';
1312
import {
1413
Span,
1514
continueTrace,
@@ -304,7 +303,7 @@ describe('startSpan', () => {
304303
it('allows to pass a scope', () => {
305304
const initialScope = getCurrentScope();
306305

307-
const manualScope = new Scope();
306+
const manualScope = initialScope.clone();
308307
const parentSpan = new Span({ spanId: 'parent-span-id' });
309308
// eslint-disable-next-line deprecation/deprecation
310309
manualScope.setSpan(parentSpan);
@@ -463,7 +462,7 @@ describe('startSpanManual', () => {
463462
it('allows to pass a scope', () => {
464463
const initialScope = getCurrentScope();
465464

466-
const manualScope = new Scope();
465+
const manualScope = initialScope.clone();
467466
const parentSpan = new Span({ spanId: 'parent-span-id' });
468467
// eslint-disable-next-line deprecation/deprecation
469468
manualScope.setSpan(parentSpan);
@@ -568,7 +567,9 @@ describe('startInactiveSpan', () => {
568567
});
569568

570569
it('allows to pass a scope', () => {
571-
const manualScope = new Scope();
570+
const initialScope = getCurrentScope();
571+
572+
const manualScope = initialScope.clone();
572573
const parentSpan = new Span({ spanId: 'parent-span-id' });
573574
// eslint-disable-next-line deprecation/deprecation
574575
manualScope.setSpan(parentSpan);

0 commit comments

Comments
 (0)