Skip to content

Commit 5007b5c

Browse files
committed
feat: Tracing Integration
1 parent 4d60369 commit 5007b5c

File tree

8 files changed

+135
-8
lines changed

8 files changed

+135
-8
lines changed

packages/hub/src/spancontext.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { SpanContext as SpanContextInterface } from '@sentry/types';
2+
import { uuid4 } from '@sentry/utils';
3+
4+
export class SpanContext implements SpanContextInterface {
5+
public constructor(
6+
public traceId: string = uuid4(),
7+
public spanId: string = uuid4().substring(16),
8+
public recorded: boolean = false,
9+
public parent?: SpanContext,
10+
) {}
11+
12+
/**
13+
* @inheritDoc
14+
*/
15+
public toTraceparent(): string {
16+
return `00-${this.traceId}-${this.spanId}-${this.recorded ? '01' : '00'}`;
17+
}
18+
}

packages/integrations/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ export { Ember } from './ember';
66
export { ExtraErrorData } from './extraerrordata';
77
export { ReportingObserver } from './reportingobserver';
88
export { RewriteFrames } from './rewriteframes';
9+
export { Tracing } from './tracing';
910
export { Transaction } from './transaction';
1011
export { Vue } from './vue';

packages/integrations/src/tracing.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { Integration } from '@sentry/types';
2+
import { fill, getGlobalObject, supportsNativeFetch, uuid4 } from '@sentry/utils';
3+
4+
/**
5+
* Session Tracking Integration
6+
*/
7+
export class Tracing implements Integration {
8+
/**
9+
* @inheritDoc
10+
*/
11+
public name: string = Tracing.id;
12+
13+
/**
14+
* @inheritDoc
15+
*/
16+
public static id: string = 'Tracing';
17+
18+
/**
19+
* Constructor for OpenTracingIntegration
20+
*
21+
* @param traceId Optional TraceId that should be set into the integration.
22+
*/
23+
public constructor(private readonly _traceId: string = uuid4()) {}
24+
25+
/**
26+
* @inheritDoc
27+
*/
28+
public setupOnce(): void {
29+
this._traceXHR();
30+
this._traceFetch();
31+
}
32+
33+
/**
34+
* JSDoc
35+
*/
36+
private _traceXHR(): void {
37+
if (!('XMLHttpRequest' in global)) {
38+
return;
39+
}
40+
41+
const traceId = this._traceId;
42+
43+
const xhrproto = XMLHttpRequest.prototype;
44+
fill(
45+
xhrproto,
46+
'send',
47+
originalSend =>
48+
function(this: XMLHttpRequest, ...args: any[]): void {
49+
this.setRequestHeader('sentry-trace', traceId);
50+
// tslint:disable-next-line: no-unsafe-any
51+
return originalSend.apply(this, args);
52+
},
53+
);
54+
}
55+
56+
/**
57+
* JSDoc
58+
*/
59+
private _traceFetch(): void {
60+
if (!supportsNativeFetch()) {
61+
return;
62+
}
63+
64+
const traceId = this._traceId;
65+
66+
// tslint:disable: only-arrow-functions
67+
fill(getGlobalObject<Window>(), 'fetch', function(originalFetch: () => void): () => void {
68+
return function(...args: any[]): void {
69+
const options = args[1] as { [key: string]: any };
70+
if (options) {
71+
if (options.headers) {
72+
options.headers = {
73+
...options.headers,
74+
'sentry-trace': traceId,
75+
};
76+
} else {
77+
options.headers = {
78+
'sentry-trace': traceId,
79+
};
80+
}
81+
}
82+
// tslint:disable-next-line: no-unsafe-any
83+
return originalFetch.apply(global, args);
84+
};
85+
});
86+
// tslint:enable: only-arrow-functions
87+
88+
// fill(
89+
// getGlobalObject<Window>(),
90+
// 'fetch',
91+
// originalFetch =>
92+
// function(...args: any[]): void {
93+
// console.log(args);
94+
// // tslint:disable-next-line: no-unsafe-any
95+
// return originalFetch.apply(this, args);
96+
// },
97+
// );
98+
}
99+
}

packages/types/src/event.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Exception } from './exception';
33
import { Request } from './request';
44
import { SdkInfo } from './sdkinfo';
55
import { Severity } from './severity';
6-
import { Span } from './span';
76
import { Stacktrace } from './stacktrace';
87
import { User } from './user';
98

@@ -33,7 +32,6 @@ export interface Event {
3332
tags?: { [key: string]: string };
3433
extra?: { [key: string]: any };
3534
user?: User;
36-
spans?: Span[];
3735
type?: EventType;
3836
}
3937

packages/types/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export { Response } from './response';
1616
export { Scope } from './scope';
1717
export { SdkInfo } from './sdkinfo';
1818
export { Severity } from './severity';
19-
export { Span } from './span';
19+
export { SpanContext } from './spancontext';
2020
export { StackFrame } from './stackframe';
2121
export { Stacktrace } from './stacktrace';
2222
export { Status } from './status';

packages/types/src/scope.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Breadcrumb } from './breadcrumb';
22
import { EventProcessor } from './eventprocessor';
33
import { Severity } from './severity';
4+
import { SpanContext } from './spancontext';
45
import { User } from './user';
56

67
/**
@@ -63,6 +64,12 @@ export interface Scope {
6364
*/
6465
setContext(name: string, context: { [key: string]: any } | null): this;
6566

67+
/**
68+
* Sets the SpanContext on the scope.
69+
* @param spanContext SpanContext
70+
*/
71+
setSpanContext(spanContext: SpanContext | null): this;
72+
6673
/** Clears the current scope and resets its properties. */
6774
clear(): this;
6875

packages/types/src/span.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/types/src/spancontext.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/** JSDoc */
2+
export interface SpanContext {
3+
traceId: string;
4+
spanId: string;
5+
recorded: boolean;
6+
parent?: SpanContext;
7+
/** JSDoc */
8+
toTraceparent(): string;
9+
}

0 commit comments

Comments
 (0)