Skip to content

Commit 14ecdb3

Browse files
authored
feat: Make browser-telemetry specific inspector type. (#741)
This is a compatibility improvement PR intended to make the telemetry package less dependent on a specific SDK package. The inspector interface between 3.x and 4.x has some minor differences and this provides a more broad interface that is compatible with both.
1 parent 68a3b87 commit 14ecdb3

File tree

6 files changed

+46
-18
lines changed

6 files changed

+46
-18
lines changed

packages/telemetry/browser-telemetry/__tests__/singleton/singletonMethods.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { LDInspection } from '@launchdarkly/js-client-sdk';
2-
31
import { Breadcrumb, LDClientTracking } from '../../src/api';
42
import { BrowserTelemetry } from '../../src/api/BrowserTelemetry';
3+
import { BrowserTelemetryInspector } from '../../src/api/client/BrowserTelemetryInspector';
54
import { getTelemetryInstance } from '../../src/singleton/singletonInstance';
65
import {
76
addBreadcrumb,
@@ -36,8 +35,8 @@ it('returns empty array when telemetry is not initialized for inspectors', () =>
3635
});
3736

3837
it('returns inspectors when telemetry is initialized', () => {
39-
const mockInspectors: LDInspection[] = [
40-
{ name: 'test-inspector', type: 'flag-used', method: () => {} },
38+
const mockInspectors: BrowserTelemetryInspector[] = [
39+
{ name: 'test-inspector', type: 'flag-used', synchronous: true, method: () => {} },
4140
];
4241
mockGetTelemetryInstance.mockReturnValue(mockTelemetry);
4342
mockTelemetry.inspectors.mockReturnValue(mockInspectors);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
* This is only a type dependency and these types should be compatible between
44
* SDKs.
55
*/
6-
import type { LDContext, LDEvaluationDetail, LDInspection } from '@launchdarkly/js-client-sdk';
6+
import type { LDContext, LDEvaluationDetail } from '@launchdarkly/js-client-sdk';
77

88
import { BreadcrumbFilter, LDClientLogging, LDClientTracking, MinLogger } from './api';
99
import { Breadcrumb, FeatureManagementBreadcrumb } from './api/Breadcrumb';
1010
import { BrowserTelemetry } from './api/BrowserTelemetry';
11+
import { BrowserTelemetryInspector } from './api/client/BrowserTelemetryInspector';
1112
import { Collector } from './api/Collector';
1213
import { ErrorData } from './api/ErrorData';
1314
import { EventData } from './api/EventData';
@@ -94,7 +95,7 @@ export default class BrowserTelemetryImpl implements BrowserTelemetry {
9495

9596
private _breadcrumbs: Breadcrumb[] = [];
9697

97-
private _inspectorInstances: LDInspection[] = [];
98+
private _inspectorInstances: BrowserTelemetryInspector[] = [];
9899
private _collectors: Collector[] = [];
99100
private _sessionId: string = randomUuidV4();
100101

@@ -149,7 +150,7 @@ export default class BrowserTelemetryImpl implements BrowserTelemetry {
149150
);
150151

151152
const impl = this;
152-
const inspectors: LDInspection[] = [];
153+
const inspectors: BrowserTelemetryInspector[] = [];
153154
makeInspectors(_options, inspectors, impl);
154155
this._inspectorInstances.push(...inspectors);
155156

@@ -184,7 +185,7 @@ export default class BrowserTelemetryImpl implements BrowserTelemetry {
184185
}
185186
}
186187

187-
inspectors(): LDInspection[] {
188+
inspectors(): BrowserTelemetryInspector[] {
188189
return this._inspectorInstances;
189190
}
190191

packages/telemetry/browser-telemetry/src/api/BrowserTelemetry.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { LDInspection } from '@launchdarkly/js-client-sdk';
2-
31
import { Breadcrumb } from './Breadcrumb';
2+
import { BrowserTelemetryInspector } from './client/BrowserTelemetryInspector';
43
import { LDClientTracking } from './client/LDClientTracking';
54

65
/**
@@ -15,9 +14,9 @@ export interface BrowserTelemetry {
1514
* Returns an array of active SDK inspectors to use with SDK versions that do
1615
* not support hooks.
1716
*
18-
* @returns An array of {@link LDInspection} objects.
17+
* @returns An array of {@link BrowserTelemetryInspector} objects.
1918
*/
20-
inspectors(): LDInspection[];
19+
inspectors(): BrowserTelemetryInspector[];
2120

2221
/**
2322
* Captures an Error object for telemetry purposes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* A less constrained version of the LDInspection interface in order to allow for greater compatibility between
3+
* SDK versions.
4+
*
5+
* This interface is not intended for use by application developers and is instead intended as a compatibility bridge
6+
* to support multiple SDK versions.
7+
*/
8+
export interface BrowserTelemetryInspector {
9+
/**
10+
* The telemetry package only requires flag-detail-changed inspectors and flag-used inspectors.
11+
*/
12+
type: 'flag-used' | 'flag-detail-changed';
13+
14+
/**
15+
* The name of the inspector, used for debugging purposes.
16+
*/
17+
name: string;
18+
/**
19+
* Whether the inspector is synchronous.
20+
*/
21+
synchronous: boolean;
22+
/**
23+
* The method to call when the inspector is triggered.
24+
*
25+
* The typing here is intentionally loose to allow for greater compatibility between SDK versions.
26+
* This function should ONLY be called by an SDK instance and not by an application developer.
27+
*/
28+
method: (...args: any[]) => void;
29+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import type { LDContext, LDEvaluationDetail, LDInspection } from '@launchdarkly/js-client-sdk';
1+
import type { LDContext, LDEvaluationDetail } from '@launchdarkly/js-client-sdk';
22

3+
import { BrowserTelemetryInspector } from './api/client/BrowserTelemetryInspector.js';
34
import BrowserTelemetryImpl from './BrowserTelemetryImpl.js';
45
import { ParsedOptions } from './options.js';
56

@@ -12,7 +13,7 @@ import { ParsedOptions } from './options.js';
1213
*/
1314
export default function makeInspectors(
1415
options: ParsedOptions,
15-
inspectors: LDInspection[],
16+
inspectors: BrowserTelemetryInspector[],
1617
telemetry: BrowserTelemetryImpl,
1718
) {
1819
if (options.breadcrumbs.evaluations) {

packages/telemetry/browser-telemetry/src/singleton/singletonMethods.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type { LDInspection } from '@launchdarkly/js-client-sdk';
2-
31
import { LDClientTracking } from '../api';
42
import { Breadcrumb } from '../api/Breadcrumb';
3+
import { BrowserTelemetryInspector } from '../api/client/BrowserTelemetryInspector';
54
import { getTelemetryInstance } from './singletonInstance';
65

76
/**
@@ -11,9 +10,9 @@ import { getTelemetryInstance } from './singletonInstance';
1110
* Telemetry must be initialized, using {@link initializeTelemetry} before calling this method.
1211
* If telemetry is not initialized, this method will return an empty array.
1312
*
14-
* @returns An array of {@link LDInspection} objects.
13+
* @returns An array of {@link BrowserTelemetryInspector} objects.
1514
*/
16-
export function inspectors(): LDInspection[] {
15+
export function inspectors(): BrowserTelemetryInspector[] {
1716
return getTelemetryInstance()?.inspectors() || [];
1817
}
1918

0 commit comments

Comments
 (0)