Skip to content

feat(flags): track feature flag evaluations in span attributes #16475

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

Closed
wants to merge 3 commits into from

Conversation

aliu39
Copy link
Member

@aliu39 aliu39 commented Jun 3, 2025

Based off https://github.com/getsentry/sentry-python/pull/4280/files


Before submitting a pull request, please take a look at our
Contributing guidelines and verify:

  • If you've added code that should be tested, please add tests.
  • Ensure your code lints and the test suite passes (yarn lint) & (yarn test).

@aliu39 aliu39 requested a review from cmanallen June 3, 2025 15:55
@@ -1,3 +1,5 @@
/* eslint-disable max-lines */
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

304 lines after changes

@@ -45,6 +47,8 @@ import { timedEventsToMeasurements } from './measurement';
import { getCapturedScopesOnSpan } from './utils';

const MAX_SPAN_COUNT = 1000;
const MAX_FEATURE_FLAGS_PER_SPAN = 10; // The maximum number of feature flag evaluations that can be recorded in span attributes.
const FEATURE_FLAG_ATTRIBUTE_PREFIX = 'sentry.flag.evaluation.';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since users can call setAttribute, can be user defined I thought it best to prefix with sentry. See SpanAttributes defn

Comment on lines +144 to +146
if (this._numFeatureFlags >= MAX_FEATURE_FLAGS_PER_SPAN) {
return this;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a buffer so that we can evict the least recently added flags (similar to what we do w/ breadcrumbs)? Silently ignoring the flag here feels not great.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's what we do on in python atm, cc @cmanallen. We could have a buffer but need to make sure the attribute is serializable - Doesn't look like SpanAttributeValue allows nested objects. Maybe we could store a buffer of keys internally

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For spans the oldest flag evaluations are the most important because they're likely to have the largest performance impact. We drop late arrivals due to request size concerns.

@aliu39 aliu39 marked this pull request as ready for review June 3, 2025 17:32
@aliu39 aliu39 marked this pull request as draft June 3, 2025 17:32
Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it expected that this will be used by users? Like they will call span.addFeatureFlag?

A method on the span break our compat with OTEL spans, which we use for duck typing in some scenarios. I would prefer if we made this a helper method we export from @sentry/core instead. You can see an example of this with setHttpStatus:

export function setHttpStatus(span: Span, httpStatus: number): void {

We're basically just extracting addFlagToActiveSpan into core, with a implementation that looks something like so:

export function addFlagToSpan(span: Span, name: string, value: unknown): void {
  if (!span || typeof value !== 'boolean') {
    return;
  }
  span.setAttribute(`${FEATURE_FLAG_ATTRIBUTE_PREFIX}${name}`, value);
}

@@ -79,6 +79,11 @@ export class SentryNonRecordingSpan implements Span {
return this;
}

/** @inheritDoc */
public addFeatureFlag(_name: string, _value: boolean): this {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a new method onto a span breaks our compatibility with OTEL, which we leverage for duck typing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This strategy makes sense, but how can we track the num current flags? (currently a protected field on the span)

Copy link
Member Author

@aliu39 aliu39 Jun 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it expected that this will be used by users? Like they will call span.addFeatureFlag?

I'm thinking we can expose this as an API for users, following the pattern we have for adding flags to scope in featureFlagIntegration. It'd be the same as the addFeatureFlag method, without the need for an integration

@aliu39
Copy link
Member Author

aliu39 commented Jun 4, 2025

Opening a new PR w/revised implementation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants