Skip to content

Commit 53e6de3

Browse files
committed
fix(core): Only attach flags context to error events
1 parent 8804c4e commit 53e6de3

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

packages/core/src/utils/featureFlags.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ const SPAN_FLAG_ATTRIBUTE_PREFIX = 'flag.evaluation.';
2828
* Copies feature flags that are in current scope context to the event context
2929
*/
3030
export function _INTERNAL_copyFlagsFromScopeToEvent(event: Event): Event {
31+
if (event.type) {
32+
// No need to add the flags context to transaction events.
33+
// Spans already get the flag.evaluation attributes.
34+
return event;
35+
}
36+
3137
const scope = getCurrentScope();
3238
const flagContext = scope.getScopeData().contexts.flags;
3339
const flagBuffer = flagContext ? flagContext.values : [];

packages/core/test/lib/utils/featureFlags.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import { afterEach, describe, expect, it, vi } from 'vitest';
22
import { getCurrentScope } from '../../../src/currentScopes';
33
import { debug } from '../../../src/utils/debug-logger';
44
import {
5+
_INTERNAL_copyFlagsFromScopeToEvent,
56
_INTERNAL_insertFlagToScope,
67
_INTERNAL_insertToFlagBuffer,
78
type FeatureFlag,
89
} from '../../../src/utils/featureFlags';
910

11+
import * as currentScopeModule from '../../../src/currentScopes';
12+
import type { Event } from '../../../src/types-hoist/event';
13+
1014
describe('flags', () => {
1115
describe('insertFlagToScope()', () => {
1216
it('adds flags to the current scope context', () => {
@@ -109,4 +113,59 @@ describe('flags', () => {
109113
]);
110114
});
111115
});
116+
117+
describe('copyFlagsFromScopeToEvent()', () => {
118+
it.each(['transaction', 'replay_event', 'feedback', 'profile'])('does not add flags context to %s events', type => {
119+
vi.spyOn(currentScopeModule, 'getCurrentScope').mockReturnValue({
120+
// @ts-expect-error - only returning partial scope data
121+
getScopeData: () => ({
122+
contexts: {
123+
flags: { values: [{ flag: 'feat1', result: true }] },
124+
},
125+
}),
126+
});
127+
128+
const event = {
129+
type: type,
130+
spans: [],
131+
} as Event;
132+
133+
const result = _INTERNAL_copyFlagsFromScopeToEvent(event);
134+
135+
expect(result).toEqual(event);
136+
expect(getCurrentScope).not.toHaveBeenCalled();
137+
});
138+
139+
it('adds add flags context to error events', () => {
140+
vi.spyOn(currentScopeModule, 'getCurrentScope').mockReturnValue({
141+
// @ts-expect-error - only returning partial scope data
142+
getScopeData: () => ({
143+
contexts: {
144+
flags: {
145+
values: [
146+
{ flag: 'feat1', result: true },
147+
{ flag: 'feat2', result: false },
148+
],
149+
},
150+
},
151+
}),
152+
});
153+
154+
const event: Event = {
155+
exception: {
156+
values: [
157+
{
158+
type: 'Error',
159+
value: 'error message',
160+
},
161+
],
162+
},
163+
};
164+
165+
const result = _INTERNAL_copyFlagsFromScopeToEvent(event);
166+
167+
expect(result).toEqual(event);
168+
expect(getCurrentScope).toHaveBeenCalled();
169+
});
170+
});
112171
});

0 commit comments

Comments
 (0)