Skip to content

Commit d1b364e

Browse files
authored
feat: Vendor TraceKit (#729)
This PR vendors tracekit to alleviate some issues with ESM compatibility. Currently the typescript conversion is minimal. It adds the types which are largely compatible and leaves most of it reasonably unchanged. This PR does not port the tests from tracekit to this repository. A subsequent PR will do this. BEGIN_COMMIT_OVERRIDE feat: Vendor TraceKit feat: Export browser-telemetry initialization method. END_COMMIT_OVERRIDE Jira: EMSR-14 Jira: EMSR-15
1 parent 20cccbf commit d1b364e

File tree

5 files changed

+1129
-9
lines changed

5 files changed

+1129
-9
lines changed

packages/telemetry/browser-telemetry/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@
4343
"bugs": {
4444
"url": "https://github.com/launchdarkly/js-core/issues"
4545
},
46-
"dependencies": {
47-
"tracekit": "^0.4.6"
48-
},
4946
"devDependencies": {
5047
"@jest/globals": "^29.7.0",
5148
"@launchdarkly/js-client-sdk": "0.3.2",

packages/telemetry/browser-telemetry/src/BrowserTelemetryImpl.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as TraceKit from 'tracekit';
2-
31
/**
42
* A limited selection of type information is provided by the browser client SDK.
53
* This is only a type dependency and these types should be compatible between
@@ -23,6 +21,7 @@ import makeInspectors from './inspectors';
2321
import { ParsedOptions, ParsedStackOptions } from './options';
2422
import randomUuidV4 from './randomUuidV4';
2523
import parse from './stack/StackParser';
24+
import { getTraceKit } from './vendor/TraceKit';
2625

2726
// TODO: Use a ring buffer for the breadcrumbs/pending events instead of shifting. (SDK-914)
2827

@@ -54,13 +53,18 @@ function safeValue(u: unknown): string | boolean | number | undefined {
5453
}
5554

5655
function configureTraceKit(options: ParsedStackOptions) {
56+
const TraceKit = getTraceKit();
5757
// Include before + after + source line.
5858
// TraceKit only takes a total context size, so we have to over capture and then reduce the lines.
5959
// So, for instance if before is 3 and after is 4 we need to capture 4 and 4 and then drop a line
6060
// from the before context.
6161
// The typing for this is a bool, but it accepts a number.
6262
const beforeAfterMax = Math.max(options.source.afterLines, options.source.beforeLines);
63-
(TraceKit as any).linesOfContext = beforeAfterMax * 2 + 1;
63+
// The assignment here has bene split to prevent esbuild from complaining about an assigment to
64+
// an import. TraceKit exports a single object and the interface requires modifying an exported
65+
// var.
66+
const anyObj = TraceKit as any;
67+
anyObj.linesOfContext = beforeAfterMax * 2 + 1;
6468
}
6569

6670
export default class BrowserTelemetryImpl implements BrowserTelemetry {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1+
import { BrowserTelemetry } from './api/BrowserTelemetry';
2+
import { Options } from './api/Options';
3+
import BrowserTelemetryImpl from './BrowserTelemetryImpl';
4+
import parse from './options';
5+
16
export * from './api';
7+
8+
export function initializeTelemetry(options?: Options): BrowserTelemetry {
9+
const parsedOptions = parse(options || {});
10+
return new BrowserTelemetryImpl(parsedOptions);
11+
}

packages/telemetry/browser-telemetry/src/stack/StackParser.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { computeStackTrace } from 'tracekit';
2-
31
import { StackFrame } from '../api/stack/StackFrame';
42
import { StackTrace } from '../api/stack/StackTrace';
53
import { ParsedStackOptions } from '../options';
4+
import { getTraceKit } from '../vendor/TraceKit';
65

76
/**
87
* In the browser we will not always be able to determine the source file that code originates
@@ -195,7 +194,7 @@ export function getSrcLines(
195194
* @returns The stack trace for the given error.
196195
*/
197196
export default function parse(error: Error, options: ParsedStackOptions): StackTrace {
198-
const parsed = computeStackTrace(error);
197+
const parsed = getTraceKit().computeStackTrace(error);
199198
const frames: StackFrame[] = parsed.stack.reverse().map((inFrame) => ({
200199
fileName: processUrlToFileName(inFrame.url, window.location.origin),
201200
function: inFrame.func,

0 commit comments

Comments
 (0)