Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(flags): Use SDK util functions for tracking feature flags #81159

Open
wants to merge 1 commit into
base: feat-sdk-upgrade-to-8.41.0-beta.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions static/app/bootstrap/initializeSdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
useNavigationType,
} from 'react-router-dom';
import {useEffect} from 'react';
import FeatureObserver from 'sentry/utils/featureObserver';

const SPA_MODE_ALLOW_URLS = [
'localhost',
Expand Down Expand Up @@ -183,12 +182,7 @@ export function initializeSdk(config: Config) {

lastEventId = event.event_id || hint.event_id;

// attach feature flags to the event context
if (event.contexts) {
const flags = FeatureObserver.singleton({}).getFeatureFlags();
event.contexts.flags = flags;
}

Sentry.copyFlagsFromScopeToEvent(event);
return event;
},
});
Expand Down
32 changes: 3 additions & 29 deletions static/app/utils/featureObserver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {Flags} from 'sentry/types/event';
import {insertFlagToScope} from '@sentry/react';

import type {Organization} from 'sentry/types/organization';
import type {Project} from 'sentry/types/project';

Expand All @@ -22,46 +23,19 @@ export default class FeatureObserver {
}

private _bufferSize = 0;
private FEATURE_FLAGS: Flags = {values: []};

constructor({bufferSize}: {bufferSize: number}) {
this._bufferSize = bufferSize;
}

/**
* Return list of recently accessed feature flags.
*/
public getFeatureFlags() {
return this.FEATURE_FLAGS;
}

public updateFlagBuffer({
flagName,
flagResult,
}: {
flagName: string;
flagResult: boolean;
}) {
const flagBuffer = this.FEATURE_FLAGS;
// Check if the flag is already in the buffer
const index = flagBuffer.values.findIndex(f => f.flag === flagName);

// The flag is already in the buffer
if (index !== -1) {
flagBuffer.values.splice(index, 1);
}

// If at capacity, we need to remove the earliest flag
// This will only happen if not a duplicate flag
if (flagBuffer.values.length === this._bufferSize) {
flagBuffer.values.shift();
}

// Store the flag and its result in the buffer
flagBuffer.values.push({
flag: flagName,
result: flagResult,
});
insertFlagToScope(flagName, flagResult, this._bufferSize);
}

public observeOrganizationFlags({organization}: {organization: Organization}) {
Expand Down
Loading