Skip to content

Commit

Permalink
fix: apm tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HazAT committed Jun 2, 2020
1 parent 6dec1a8 commit 83a9a96
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/apm/src/hubextensions.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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();
Expand Down
10 changes: 8 additions & 2 deletions packages/apm/test/hub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -59,24 +63,26 @@ 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);
});

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);
});
Expand Down
63 changes: 53 additions & 10 deletions packages/apm/test/span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -205,16 +205,15 @@ 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();
});

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

Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();

Expand All @@ -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();
Expand All @@ -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', () => {
Expand Down

0 comments on commit 83a9a96

Please sign in to comment.