sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

feat(flags): capture feature flag evaluations on spans

Open aliu39 opened this issue 7 months ago • 1 comments

Based off https://github.com/getsentry/sentry-python/pull/4280/files Reopened from https://github.com/getsentry/sentry-javascript/pull/16475

This PR updates our 5 browser FF integrations to capture flag evaluations on spans. This implementation avoids changes to the Span class, saving bundle size.

  • on eval, flags are added to span attributes. A global WeakMap is used to track the unique flags for a span. Updates to a flag's value is allowed.
  • staying consistent with the python PR:
    • we only capture values for the first 10 unique flags per span. Subsequent flags are dropped.
    • attribute keys have the format flag.evaluation.{flagKey}

From @AbhiPrasad :

A method on the span break our compat with OTEL spans, which we use for duck typing in some scenarios.

From @cmanallen :

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 avatar Jun 04 '25 18:06 aliu39

Useful refs:

  • https://develop.sentry.dev/sdk/platform-specifics/javascript-sdks/browser-tracing/
  • https://develop.sentry.dev/sdk/data-model/event-payloads/span/

aliu39 avatar Jun 05 '25 21:06 aliu39

Looks good! Just one question: You need the buffer approach for determining the evaluation order right?

Yes, I originally used buffers/arrays to track the eval order as we do in the scope/errors. However I don't believe this is a requirement as we're not enforcing order in Python spans, only recording the first 10 unique flags. IMO it's not as valuable for the span usecase, and for only 10 flags.

Otherwise we could also just conditionally add the flag based on how many attributes are already present on the attributes.

Yes that would also work as an alternative to the global weak map. Is there a way to do this without extending the Span interface though?

aliu39 avatar Jun 16 '25 16:06 aliu39

@aliu39 something like

if (shouldAddFeatureFlagToSpan(Sentry.spanToJSON(span).data, someFlag)) {
  span.setAttribute(
    // ...
  )
}

And in shouldAddFeatureFlagToSpan you can count the attributes starting with your prefix for example.

chargome avatar Jun 17 '25 08:06 chargome