diff --git a/packages/apm/src/hubextensions.ts b/packages/apm/src/hubextensions.ts index b558d1f3cd56..9398f1814100 100644 --- a/packages/apm/src/hubextensions.ts +++ b/packages/apm/src/hubextensions.ts @@ -1,4 +1,4 @@ -import { getCurrentHub, getMainCarrier, Hub } from '@sentry/hub'; +import { getMainCarrier, Hub } from '@sentry/hub'; import { SpanContext, TransactionContext } from '@sentry/types'; import { logger } from '@sentry/utils'; @@ -62,7 +62,7 @@ function startSpan(this: Hub, context: SpanContext): Transaction | Span { // We have the check of not undefined since we defined it's ok to start a transaction with an empty name // tslint:disable-next-line: strict-type-predicates if ((context as TransactionContext).name !== undefined) { - return getCurrentHub().startTransaction(context as TransactionContext); + return this.startTransaction(context as TransactionContext); } const scope = this.getScope(); diff --git a/packages/apm/test/hub.test.ts b/packages/apm/test/hub.test.ts index b067ea77d46e..9104beb3bbc2 100644 --- a/packages/apm/test/hub.test.ts +++ b/packages/apm/test/hub.test.ts @@ -21,16 +21,19 @@ describe('Hub', () => { }); test('set tracesSampleRate 0 on transaction', () => { const hub = new Hub(new BrowserClient({ tracesSampleRate: 0 })); + // @ts-ignore const transaction = hub.startSpan({ name: 'foo' }) as any; expect(transaction.sampled).toBe(false); }); test('set tracesSampleRate 1 on transaction', () => { const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 })); + // @ts-ignore const transaction = hub.startSpan({ name: 'foo' }) as any; expect(transaction.sampled).toBeTruthy(); }); test('set tracesSampleRate should be propergated to children', () => { const hub = new Hub(new BrowserClient({ tracesSampleRate: 0 })); + // @ts-ignore const transaction = hub.startSpan({ name: 'foo' }) as any; const child = transaction.startChild({ op: 1 }); expect(child.sampled).toBeFalsy(); @@ -46,6 +49,7 @@ describe('Hub', () => { test('simple standalone Transaction', () => { const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 })); + // @ts-ignore const transaction = hub.startSpan({ name: 'transaction' }) as Transaction; expect(transaction.spanId).toBeTruthy(); // tslint:disable-next-line: no-unbound-method @@ -59,6 +63,7 @@ describe('Hub', () => { hub.configureScope(scope => { scope.setSpan(parentSpan); }); + // @ts-ignore const span = hub.startSpan({ name: 'test' }) as any; expect(span.trace_id).toEqual(parentSpan.trace_id); }); @@ -66,17 +71,18 @@ describe('Hub', () => { test('create a child if there is a Span already on the scope', () => { const myScope = new Scope(); const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }), myScope); + // @ts-ignore const transaction = hub.startSpan({ name: 'transaction' }) as Transaction; hub.configureScope(scope => { scope.setSpan(transaction); }); - const span = hub.startSpan({}) as Span; + const span = hub.startSpan({}); expect(span.traceId).toEqual(transaction.traceId); expect(span.parentSpanId).toEqual(transaction.spanId); hub.configureScope(scope => { scope.setSpan(span); }); - const span2 = hub.startSpan({}) as Span; + const span2 = hub.startSpan({}); expect(span2.traceId).toEqual(span.traceId); expect(span2.parentSpanId).toEqual(span.spanId); }); diff --git a/packages/apm/test/span.test.ts b/packages/apm/test/span.test.ts index 06cfaa702d3c..0fb373174259 100644 --- a/packages/apm/test/span.test.ts +++ b/packages/apm/test/span.test.ts @@ -180,10 +180,10 @@ describe('Span', () => { expect(span.endTimestamp).toBeGreaterThan(1); }); - describe('hub.startSpan', () => { + describe('hub.startTransaction', () => { test('finish a transaction', () => { const spy = jest.spyOn(hub as any, 'captureEvent') as any; - const transaction = hub.startSpan({ name: 'test' }); + const transaction = hub.startTransaction({ name: 'test' }); transaction.finish(); expect(spy).toHaveBeenCalled(); expect(spy.mock.calls[0][0].spans).toHaveLength(0); @@ -194,7 +194,7 @@ describe('Span', () => { test('finish a transaction + child span', () => { const spy = jest.spyOn(hub as any, 'captureEvent') as any; - const transaction = hub.startSpan({ name: 'test' }); + const transaction = hub.startTransaction({ name: 'test' }); const childSpan = transaction.startChild(); childSpan.finish(); transaction.finish(); @@ -205,7 +205,7 @@ describe('Span', () => { test("finish a child span shouldn't trigger captureEvent", () => { const spy = jest.spyOn(hub as any, 'captureEvent') as any; - const transaction = hub.startSpan({ name: 'test' }); + const transaction = hub.startTransaction({ name: 'test' }); const childSpan = transaction.startChild(); childSpan.finish(); expect(spy).not.toHaveBeenCalled(); @@ -213,8 +213,7 @@ describe('Span', () => { test("finish a span with another one on the scope shouldn't override contexts.trace", () => { const spy = jest.spyOn(hub as any, 'captureEvent') as any; - - const transaction = hub.startSpan({ name: 'test' }); + const transaction = hub.startTransaction({ name: 'test' }); const childSpanOne = transaction.startChild(); childSpanOne.finish(); @@ -239,7 +238,7 @@ describe('Span', () => { }), ); const spy = jest.spyOn(_hub as any, 'captureEvent') as any; - const transaction = _hub.startSpan({ name: 'test' }); + const transaction = _hub.startTransaction({ name: 'test' }); for (let i = 0; i < 10; i++) { const child = transaction.startChild(); child.finish(); @@ -255,7 +254,7 @@ describe('Span', () => { }), ); const spy = jest.spyOn(_hub as any, 'captureEvent') as any; - const transaction = _hub.startSpan({ name: 'test' }); + const transaction = _hub.startTransaction({ name: 'test' }); for (let i = 0; i < 10; i++) { const child = transaction.startChild(); child.finish(); @@ -274,7 +273,7 @@ describe('Span', () => { ); const spy = jest.spyOn(_hub as any, 'captureEvent') as any; - const transaction = _hub.startSpan({ name: 'test' }); + const transaction = _hub.startTransaction({ name: 'test' }); const childSpanOne = transaction.startChild({ op: '1' }); childSpanOne.finish(); @@ -297,7 +296,7 @@ describe('Span', () => { test('tree structure of spans should be correct when mixing it with span on scope', () => { const spy = jest.spyOn(hub as any, 'captureEvent') as any; - const transaction = hub.startSpan({ name: 'test' }); + const transaction = hub.startTransaction({ name: 'test' }); const childSpanOne = transaction.startChild(); const childSpanTwo = childSpanOne.startChild(); @@ -321,6 +320,50 @@ describe('Span', () => { expect(spanTwo.toJSON().parent_span_id).toEqual(transaction.toJSON().span_id); }); }); + + describe('hub.startSpan', () => { + test('finish a transaction', () => { + const spy = jest.spyOn(hub as any, 'captureEvent') as any; + // @ts-ignore + const transaction = hub.startSpan({ name: 'test' }); + transaction.finish(); + expect(spy).toHaveBeenCalled(); + expect(spy.mock.calls[0][0].spans).toHaveLength(0); + expect(spy.mock.calls[0][0].timestamp).toBeTruthy(); + expect(spy.mock.calls[0][0].start_timestamp).toBeTruthy(); + expect(spy.mock.calls[0][0].contexts.trace).toEqual(transaction.getTraceContext()); + }); + + test('finish a transaction (deprecated way)', () => { + const spy = jest.spyOn(hub as any, 'captureEvent') as any; + // @ts-ignore + const transaction = hub.startSpan({ transaction: 'test' }); + transaction.finish(); + expect(spy).toHaveBeenCalled(); + expect(spy.mock.calls[0][0].spans).toHaveLength(0); + expect(spy.mock.calls[0][0].timestamp).toBeTruthy(); + expect(spy.mock.calls[0][0].start_timestamp).toBeTruthy(); + expect(spy.mock.calls[0][0].contexts.trace).toEqual(transaction.getTraceContext()); + }); + + test('startSpan with Span on the Scope should be a child', () => { + const spy = jest.spyOn(hub as any, 'captureEvent') as any; + const transaction = hub.startTransaction({ name: 'test' }); + const child1 = transaction.startChild(); + hub.configureScope(scope => { + scope.setSpan(child1); + }); + + const child2 = hub.startSpan({}); + child1.finish(); + child2.finish(); + transaction.finish(); + + expect(spy).toHaveBeenCalled(); + expect(spy.mock.calls[0][0].spans).toHaveLength(2); + expect(child2.parentSpanId).toEqual(child1.spanId); + }); + }); }); describe('getTraceContext', () => {