Skip to content

Commit b96c17f

Browse files
committed
Rewrite as a util fx instead of class. Use in LD integration
1 parent f172683 commit b96c17f

File tree

15 files changed

+115
-245
lines changed

15 files changed

+115
-245
lines changed

packages/core/src/scope.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,10 @@ class ScopeClass implements ScopeInterface {
129129
newScope._extra = { ...this._extra };
130130
newScope._contexts = { ...this._contexts };
131131
if (this._contexts.flags) {
132-
// The flags context requires a deep copy.
133-
newScope._contexts.flags = {flag_buffer: this._contexts.flags.flag_buffer.clone()};
132+
// The flags context needs a deep copy.
133+
newScope._contexts.flags = {
134+
values: [...this._contexts.flags.values]
135+
}
134136
}
135137

136138
newScope._user = this._user;

packages/launchdarkly/.yalc/@sentry/types/LICENSE

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

packages/launchdarkly/.yalc/@sentry/types/README.md

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

packages/launchdarkly/.yalc/@sentry/types/package.json

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

packages/launchdarkly/.yalc/@sentry/types/yalc.sig

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/launchdarkly/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@sentry-internal/launchdarkly",
2+
"name": "@sentry/launchdarkly",
33
"version": "8.35.0",
44
"description": "Sentry SDK integration for Launch Darkly feature flagging",
55
"repository": "git://github.com/getsentry/sentry-javascript.git",
@@ -41,7 +41,7 @@
4141
"dependencies": {
4242
"@sentry/browser": "^8.35.0",
4343
"@sentry/core": "8.35.0",
44-
"@sentry/types": "file:.yalc/@sentry/types",
44+
"@sentry/types": "8.35.0",
4545
"@sentry/utils": "8.35.0",
4646
"launchdarkly-js-client-sdk": "^3.5.0"
4747
},

packages/launchdarkly/rollup.bundle.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { makeBaseBundleConfig, makeBundleConfigVariants } from '@sentry-internal
33
const baseBundleConfig = makeBaseBundleConfig({
44
bundleType: 'addon',
55
entrypoints: ['src/index.ts'],
6-
licenseTitle: '@sentry-internal/launchdarkly',
6+
licenseTitle: '@sentry/launchdarkly',
77
outputFileBase: () => 'bundles/launchdarkly',
88
});
99

packages/launchdarkly/src/core/integration.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,45 @@ import * as Sentry from '@sentry/browser';
44
import type { Client as SentryClient, Event, EventHint, IntegrationFn } from '@sentry/types';
55
import type { LDContext, LDEvaluationDetail, LDInspectionFlagUsedHandler } from 'launchdarkly-js-client-sdk';
66
import type { LaunchDarklyOptions } from '../types';
7+
import { insertToFlagBuffer } from '@sentry/utils';
78

89
/**
910
* Sentry integration for capturing feature flags from LaunchDarkly.
1011
*
1112
* See the [feature flag documentation](TODO:) for more information.
1213
*
1314
* @example
14-
*
1515
* ```
16-
* TODO:
17-
* Sentry.init({
18-
* dsn: '__DSN__',
19-
* integrations: [Sentry.replayIntegration()],
20-
* });
16+
* import {SentryInspector, launchDarklyIntegration} from '@sentry/launchdarkly';
17+
* import {LDClient} from 'launchdarkly-js-client-sdk';
18+
*
19+
* Sentry.init(..., integrations: [launchDarklyIntegration()])
20+
* const ldClient = LDClient.initialize(..., inspectors: [SentryInspector]);
2121
* ```
2222
*/
2323
export const launchDarklyIntegration = ((_options?: LaunchDarklyOptions) => {
2424
return {
2525
name: 'launchdarkly',
2626

2727
processEvent(event: Event, _hint: EventHint, _client: SentryClient): Event {
28-
const scope = Sentry.getCurrentScope(); // client doesn't have getCurrentScope
29-
const flagContext = { values: scope.getFlags() };
30-
if (event.contexts) {
31-
event.contexts.flags = flagContext;
32-
} else {
33-
event.contexts = { flags: flagContext };
28+
const scope = Sentry.getCurrentScope();
29+
const flagContext = scope.getScopeData().contexts.flags;
30+
31+
if (event.contexts === undefined) {
32+
event.contexts = {};
3433
}
34+
event.contexts.flags = flagContext;
3535
return event;
3636
},
3737
};
3838
}) satisfies IntegrationFn;
3939

4040
/**
4141
* LaunchDarkly hook that listens for flag evaluations and updates the
42-
* flagBuffer in our current scope
43-
* TODO: finalize docstring
42+
* flagBuffer in our current scope.
43+
*
44+
* This needs to be registered separately in the LDClient, after initializing
45+
* Sentry.
4446
*/
4547
export class SentryInspector implements LDInspectionFlagUsedHandler {
4648
public name = 'sentry-flag-auditor';
@@ -51,11 +53,16 @@ export class SentryInspector implements LDInspectionFlagUsedHandler {
5153
public synchronous = false;
5254

5355
/**
54-
* TODO: docstring
56+
* Handle a flag evaluation by storing its name and value on the current scope.
5557
*/
5658
public method(flagKey: string, flagDetail: LDEvaluationDetail, _context: LDContext): void {
5759
if (typeof flagDetail.value === 'boolean') {
58-
Sentry.getCurrentScope().insertFlag(flagKey, flagDetail.value);
60+
const scopeContexts = Sentry.getCurrentScope().getScopeData().contexts;
61+
if (!scopeContexts.flags) {
62+
scopeContexts.flags = {values: []}
63+
}
64+
const flagBuffer = scopeContexts.flags.values;
65+
insertToFlagBuffer(flagBuffer, flagKey, flagDetail.value);
5966
}
6067
return;
6168
}

packages/launchdarkly/yalc.lock

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

packages/types/src/context.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Primitive } from './misc';
22
import type { SpanOrigin } from './span';
3-
import type { FlagBufferInterface } from './flags'
3+
import type { FeatureFlag } from './flags'
44

55
export type Context = Record<string, unknown>;
66

@@ -128,5 +128,6 @@ export interface MissingInstrumentationContext extends Record<string, unknown> {
128128
}
129129

130130
export interface FeatureFlagContext extends Record<string, unknown> {
131-
flag_buffer: FlagBufferInterface;
131+
// This should only be modified by @sentry/util methods (insertToFlagBuffer).
132+
readonly values: FeatureFlag[];
132133
}

0 commit comments

Comments
 (0)