Skip to content

Commit 21d76f4

Browse files
committed
feat: CR
1 parent 5f7736d commit 21d76f4

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

packages/hub/test/spancontext.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe('Span', () => {
1111
expect(from._parent._spanId).toEqual('bbbbbbbbbbbbbbbb');
1212
expect(from._traceId).toEqual('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
1313
expect(from._spanId).not.toEqual('bbbbbbbbbbbbbbbb');
14+
expect(from._recorded).toEqual(false);
1415
});
1516

1617
test('fromTraceparent - invalid', () => {
@@ -22,4 +23,10 @@ describe('Span', () => {
2223
`{"span_id":"bbbbbbbbbbbbbbbb","trace_id":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}`,
2324
);
2425
});
26+
27+
test('toJSON with parent', () => {
28+
const spanA = new Span('a', 'b');
29+
const spanB = new Span('c', 'd', false, spanA);
30+
expect(JSON.stringify(spanB)).toEqual(`{"parent":{"span_id":"b","trace_id":"a"},"span_id":"d","trace_id":"c"}`);
31+
});
2532
});

packages/integrations/src/tracing.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EventProcessor, Hub, Integration } from '@sentry/types';
2-
import { fill, getGlobalObject, isMatchingPattern, SentryError, supportsNativeFetch } from '@sentry/utils';
2+
import { consoleSandbox, fill, getGlobalObject, isMatchingPattern, supportsNativeFetch } from '@sentry/utils';
33

44
/** JSDoc */
55
interface TracingOptions {
@@ -36,8 +36,18 @@ export class Tracing implements Integration {
3636
* @param _options TracingOptions
3737
*/
3838
public constructor(private readonly _options: TracingOptions) {
39-
if (!_options.tracingOrigins || !Array.isArray(_options.tracingOrigins) || _options.tracingOrigins.length === 0) {
40-
throw new SentryError('You need to define `tracingOrigins` in the options. Set an array of urls to trace.');
39+
if (!Array.isArray(_options.tracingOrigins) || _options.tracingOrigins.length === 0) {
40+
consoleSandbox(() => {
41+
const defaultTracingOrigins = ['localhost', /^\//];
42+
// tslint:disable: no-unsafe-any
43+
// @ts-ignore
44+
console.warning(
45+
'Sentry: You need to define `tracingOrigins` in the options. Set an array of urls or patterns to trace.',
46+
);
47+
// @ts-ignore
48+
console.warning(`Sentry: We added a reasonable default for you: ${defaultTracingOrigins}`);
49+
// tslint:enable: no-unsafe-any
50+
});
4151
}
4252
}
4353

@@ -55,6 +65,11 @@ export class Tracing implements Integration {
5565
getGlobalObject<Window>().addEventListener('DOMContentLoaded', () => {
5666
Tracing.startTrace(getCurrentHub(), getGlobalObject<Window>().location.href);
5767
});
68+
getGlobalObject<Window>().document.onreadystatechange = () => {
69+
if (document.readyState === 'complete') {
70+
Tracing.startTrace(getCurrentHub(), getGlobalObject<Window>().location.href);
71+
}
72+
};
5873
}
5974
}
6075

@@ -104,17 +119,18 @@ export class Tracing implements Integration {
104119
const self = getCurrentHub().getIntegration(Tracing);
105120
if (self && self._xhrUrl) {
106121
const headers = getCurrentHub().traceHeaders();
107-
let whiteListed = false;
122+
let whiteList = false;
123+
108124
// tslint:disable-next-line: prefer-for-of
109125
for (let index = 0; index < self._options.tracingOrigins.length; index++) {
110126
const whiteListUrl = self._options.tracingOrigins[index];
111-
whiteListed = isMatchingPattern(self._xhrUrl, whiteListUrl);
112-
if (whiteListed) {
127+
whiteList = isMatchingPattern(self._xhrUrl, whiteListUrl);
128+
if (whiteList) {
113129
break;
114130
}
115131
}
116132

117-
if (whiteListed && this.setRequestHeader) {
133+
if (whiteList && this.setRequestHeader) {
118134
Object.keys(headers).forEach(key => {
119135
this.setRequestHeader(key, headers[key]);
120136
});

packages/node/src/handlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ export function errorHandler(): (
294294
return;
295295
}
296296
withScope(scope => {
297-
if (isString(_req.headers['sentry-trace'])) {
297+
if (_req.headers && isString(_req.headers['sentry-trace'])) {
298298
const span = Span.fromTraceparent(_req.headers['sentry-trace'] as string);
299299
scope.setSpan(span);
300300
}

packages/utils/src/syncpromise.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { isThenable } from './is';
33
/** SyncPromise internal states */
44
enum States {
55
/** Pending */
6-
P = 'P',
6+
PENDING = 'PENDING',
77
/** Resolved / OK */
8-
O = 'O',
8+
RESOLVED = 'RESOLVED',
99
/** Rejected / Error */
10-
E = 'E',
10+
REJECTED = 'REJECTED',
1111
}
1212

1313
/** JSDoc */
@@ -31,7 +31,7 @@ type Reject = (value?: any) => void;
3131
/** JSDoc */
3232
export class SyncPromise<T> implements PromiseLike<T> {
3333
/** JSDoc */
34-
private _state: States = States.P;
34+
private _state: States = States.PENDING;
3535
/** JSDoc */
3636
private _handlers: Array<Handler<T, any>> = [];
3737
/** JSDoc */
@@ -47,17 +47,17 @@ export class SyncPromise<T> implements PromiseLike<T> {
4747

4848
/** JSDoc */
4949
private readonly _resolve = (value: T) => {
50-
this._setResult(value, States.O);
50+
this._setResult(value, States.RESOLVED);
5151
};
5252

5353
/** JSDoc */
5454
private readonly _reject = (reason: any) => {
55-
this._setResult(reason, States.E);
55+
this._setResult(reason, States.REJECTED);
5656
};
5757

5858
/** JSDoc */
5959
private readonly _setResult = (value: T | any, state: States) => {
60-
if (this._state !== States.P) {
60+
if (this._state !== States.PENDING) {
6161
return;
6262
}
6363

@@ -74,11 +74,11 @@ export class SyncPromise<T> implements PromiseLike<T> {
7474

7575
/** JSDoc */
7676
private readonly _executeHandlers = () => {
77-
if (this._state === States.P) {
77+
if (this._state === States.PENDING) {
7878
return;
7979
}
8080

81-
if (this._state === States.E) {
81+
if (this._state === States.REJECTED) {
8282
// tslint:disable-next-line:no-unsafe-any
8383
this._handlers.forEach(h => h.onFail && h.onFail(this._value));
8484
} else {

0 commit comments

Comments
 (0)