Skip to content

Commit 3993134

Browse files
authored
refactor observability plugins to no longer require an observability project id (#59)
## Summary Sets up the SDK to work with launchdarkly/observability#66 where the client-side ID is sent over instead of a highlight project ID. ## How did you test this change? https://www.loom.com/share/43bcbbc625eb40a6b00a6b81fdce1e7e ## Are there any deployment considerations? changeset ## Does this work require review from our design team? no
1 parent 0716fe5 commit 3993134

File tree

12 files changed

+105
-77
lines changed

12 files changed

+105
-77
lines changed

.changeset/curvy-games-shop.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@launchdarkly/observability': minor
3+
'@launchdarkly/session-replay': minor
4+
'highlight.run': patch
5+
---
6+
7+
refactor observability plugins to no longer require an observability project id

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const client = init3(
4040
{
4141
// Not including plugins at all would be equivalent to the current LaunchDarkly SDK.
4242
plugins: [
43-
new Observability('<OBSERVABILITY_PROJECT_ID>', {
43+
new Observability({
4444
networkRecording: {
4545
enabled: true,
4646
recordHeadersAndBody: true,
@@ -83,7 +83,7 @@ const client = init3(
8383
{
8484
// Not including plugins at all would be equivalent to the current LaunchDarkly SDK.
8585
plugins: [
86-
new SessionReplay('<OBSERVABILITY_PROJECT_ID>', {
86+
new SessionReplay({
8787
serviceName: 'example-svc',
8888
}), // Could be omitted for customers who cannot use session replay.
8989
],

e2e/react-router/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"@launchdarkly/js-client-sdk": "^0.6.0",
1515
"@launchdarkly/observability": "workspace:*",
1616
"@launchdarkly/session-replay": "workspace:*",
17-
"launchdarkly-js-client-sdk": "^3.7.0",
18-
"launchdarkly-react-client-sdk": "^3.7.0",
17+
"launchdarkly-js-client-sdk": "3.8.1",
18+
"launchdarkly-react-client-sdk": "3.8.1",
1919
"localforage": "^1.10.0",
2020
"match-sorter": "^6.3.1",
2121
"react": "^19.0.0",

e2e/react-router/src/ldclient.tsx

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,45 @@ import { initialize as init } from 'launchdarkly-js-client-sdk'
33
import Observability from '@launchdarkly/observability'
44
import SessionReplay from '@launchdarkly/session-replay'
55
// import { LD } from '@launchdarkly/browser'
6+
// import { withLDProvider } from 'launchdarkly-react-client-sdk'
7+
8+
const observabilitySettings: ConstructorParameters<typeof Observability>[0] = {
9+
networkRecording: {
10+
enabled: true,
11+
recordHeadersAndBody: true,
12+
},
13+
serviceName: 'ryan-test',
14+
backendUrl: 'https://pub.observability.ld-stg.launchdarkly.com',
15+
otel: {
16+
otlpEndpoint: 'https://otel.observability.ld-stg.launchdarkly.com',
17+
},
18+
}
19+
const sessionReplaySettings: ConstructorParameters<typeof SessionReplay>[0] = {
20+
debug: { clientInteractions: true, domRecording: true },
21+
privacySetting: 'none',
22+
serviceName: 'ryan-test',
23+
backendUrl: 'https://pub.observability.ld-stg.launchdarkly.com',
24+
}
625

726
export const client = init(
827
'66d9d3c255856f0fa8fd62d0',
928
{ key: 'unknown' },
1029
{
1130
// Not including plugins at all would be equivalent to the current LaunchDarkly SDK.
1231
plugins: [
13-
new Observability('1', {
14-
networkRecording: {
15-
enabled: true,
16-
recordHeadersAndBody: true,
17-
},
18-
serviceName: 'ryan-test',
19-
backendUrl: 'https://pub.observability.ld-stg.launchdarkly.com',
20-
otel: {
21-
otlpEndpoint:
22-
'https://otel.observability.ld-stg.launchdarkly.com',
23-
},
24-
}),
25-
new SessionReplay('1', {
26-
debug: { clientInteractions: true, domRecording: true },
27-
privacySetting: 'none',
28-
serviceName: 'ryan-test',
29-
backendUrl: 'https://pub.observability.ld-stg.launchdarkly.com',
30-
}), // Could be omitted for customers who cannot use session replay.
32+
new Observability(observabilitySettings),
33+
new SessionReplay(sessionReplaySettings),
3134
],
3235
},
3336
)
37+
38+
/*export const LDProvider = withLDProvider({
39+
clientSideID: '66d9d3c255856f0fa8fd62d0',
40+
context: { key: 'unknown' },
41+
options: {
42+
plugins: [
43+
new Observability(observabilitySettings),
44+
new SessionReplay(sessionReplaySettings),
45+
],
46+
},
47+
})*/

e2e/react-router/src/routes/welcome.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ function Welcome() {
99

1010
useEffect(() => {
1111
if (observabilityEnabled && client) {
12-
const plugins = [
13-
new Observability('<observability-id>'),
14-
new SessionReplay('<observability-id>'),
15-
]
12+
const plugins = [new Observability(), new SessionReplay()]
1613
plugins.forEach((plugin) =>
1714
plugin.register(client, {
1815
sdk: { name: '', version: '' },

sdk/@launchdarkly/observability/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const client = init3(
3232
{
3333
// Not including plugins at all would be equivalent to the current LaunchDarkly SDK.
3434
plugins: [
35-
new Observability('<OBSERVABILITY_PROJECT_ID>', {
35+
new Observability({
3636
networkRecording: {
3737
enabled: true,
3838
recordHeadersAndBody: true,

sdk/@launchdarkly/session-replay/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const client = init3(
3232
{
3333
// Not including plugins at all would be equivalent to the current LaunchDarkly SDK.
3434
plugins: [
35-
new SessionReplay('<OBSERVABILITY_PROJECT_ID>', {
35+
new SessionReplay({
3636
serviceName: 'example-svc',
3737
}), // Could be omitted for customers who cannot use session replay.
3838
],

sdk/highlight-run/src/__tests__/sdk.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ describe('SDK', () => {
7474
throw new Error('set error')
7575
}),
7676
})
77-
const sdk = new Record('1')
77+
const sdk = new Record()
78+
// trigger sdk to initialize
79+
sdk.getHooks?.({ sdkKey: 'abc123', sdk: { name: '', version: '' } })
7880
expect(sdk.record).toBeDefined()
7981
vi.unstubAllGlobals()
8082
})
@@ -91,7 +93,7 @@ describe('SDK', () => {
9193
throw new Error('set error')
9294
}),
9395
})
94-
const sdk = new Record('1')
96+
const sdk = new Record()
9597
expect(sdk.record).toBeUndefined()
9698
vi.unstubAllGlobals()
9799
})

sdk/highlight-run/src/plugins/observe.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@ import { LDEvaluationReason } from '@launchdarkly/js-sdk-common/dist/cjs/api/dat
3434

3535
export class Observe extends Plugin<ObserveOptions> implements LDPlugin {
3636
observe: ObserveAPI | undefined
37+
options: ObserveOptions | undefined
3738

38-
constructor(projectID?: string | number, options?: ObserveOptions) {
39+
constructor(options?: ObserveOptions) {
40+
super(options)
41+
this.options = options
42+
}
43+
44+
private initialize(
45+
ldCredential: string | undefined,
46+
options?: ObserveOptions,
47+
) {
3948
try {
40-
super(options)
4149
// Don't run init when called outside of the browser.
4250
if (
4351
typeof window === 'undefined' ||
@@ -49,9 +57,9 @@ export class Observe extends Plugin<ObserveOptions> implements LDPlugin {
4957
return
5058
}
5159
// Don't initialize if an projectID is not set.
52-
if (!projectID) {
60+
if (!ldCredential) {
5361
console.warn(
54-
'@launchdarkly/observability is not initializing because projectID was passed undefined.',
62+
'@launchdarkly/observability is not initializing because the SDK credential is undefined.',
5563
)
5664
return
5765
}
@@ -62,7 +70,7 @@ export class Observe extends Plugin<ObserveOptions> implements LDPlugin {
6270
otlpEndpoint:
6371
options?.otel?.otlpEndpoint ??
6472
'https://otel.observability.app.launchdarkly.com',
65-
projectId: projectID,
73+
projectId: ldCredential,
6674
sessionSecureId: this.sessionSecureID,
6775
environment: options?.environment ?? 'production',
6876
networkRecordingOptions:
@@ -111,6 +119,10 @@ export class Observe extends Plugin<ObserveOptions> implements LDPlugin {
111119
}
112120
: {}),
113121
}
122+
this.initialize(
123+
metadata.sdkKey ?? metadata.mobileKey ?? metadata.clientSideId,
124+
this.options,
125+
)
114126
return [
115127
{
116128
getMetadata: () => {
Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
import type {
2+
LDPluginEnvironmentMetadata,
3+
LDPluginMetadata,
4+
} from '@launchdarkly/js-client-sdk'
15
import type { Hook, LDClient } from '../integrations/launchdarkly'
26

7+
export type { Hook, LDClient, LDPluginEnvironmentMetadata, LDPluginMetadata }
8+
39
export interface LDPlugin {
410
getMetadata(): LDPluginMetadata
511
register(
@@ -8,25 +14,3 @@ export interface LDPlugin {
814
): void
915
getHooks?(metadata: LDPluginEnvironmentMetadata): Hook[]
1016
}
11-
12-
export interface LDPluginMetadata {
13-
readonly name: string
14-
}
15-
16-
export interface LDPluginSdkMetadata {
17-
name: string
18-
version: string
19-
wrapperName?: string
20-
wrapperVersion?: string
21-
}
22-
23-
export interface LDPluginApplicationMetadata {
24-
id?: string
25-
version?: string
26-
}
27-
28-
export interface LDPluginEnvironmentMetadata {
29-
sdk: LDPluginSdkMetadata
30-
application?: LDPluginApplicationMetadata
31-
clientSideId: string
32-
}

0 commit comments

Comments
 (0)