Skip to content

feat: Add span feature flag API#8158

Open
denrase wants to merge 4 commits into
feat/feature-flag-scope-apifrom
feat/feature-flag-span-api
Open

feat: Add span feature flag API#8158
denrase wants to merge 4 commits into
feat/feature-flag-scope-apifrom
feat/feature-flag-span-api

Conversation

@denrase

@denrase denrase commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

📜 Description

Add Swift APIs for recording feature flag evaluations on spans and transactions.

Feature flags recorded through the scope are also forwarded to the currently active span, while preserving independent span buffers.

Serialize span and transaction feature flags as trace data using the flag.evaluation.<name> convention.

💡 Motivation and Context

Closes #7990

💚 How did you test it?

Unit tests. Sample app.

https://sentry-sdks.sentry.io/explore/traces/trace/6e743a46c18d4e7091d6465b949f82f2/?node=span-205484086d134b0c&project=-1&query=trace%3A6e743a46c18d4e7091d6465b949f82f2&source=traces&sta
tsPeriod=14d&targetId=205484086d134b0c

Bildschirmfoto 2026-06-23 um 17 27 38

📝 Checklist

You have to check all boxes before merging:

  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.
  • If I added a new public API, I also added it to the SentryObjC wrapper.

Add Swift APIs for recording feature flag evaluations on spans and transactions.

Feature flags recorded through the scope are also forwarded to the currently active span, while preserving independent span buffers.

Serialize span and transaction feature flags as trace data using the `flag.evaluation.<name>` convention.
@github-actions

github-actions Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor
Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against eb3be8b

@denrase denrase self-assigned this Jun 23, 2026
@denrase denrase added the ready-to-merge Use this label to trigger all PR workflows label Jun 23, 2026
@denrase

denrase commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator Author

This is a stacked PR, so we should not enable auto-merge on it.

@denrase denrase marked this pull request as ready for review June 23, 2026 15:30

- (void)addFeatureFlagWithName:(NSString *)name result:(BOOL)result
{
id<SentrySpan> activeSpan = self.span;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: A race condition in addFeatureFlagWithName: can cause feature flags to be lost if they are added while a transaction is being finished and serialized.
Severity: MEDIUM

Suggested Fix

To prevent the race condition, ensure that the modification of the span's feature flags is atomic with respect to the span's serialization process. This could be achieved by moving the read of the activeSpan and its subsequent modification into a synchronized block that uses the same lock as the span's state management, or by adding a check to prevent modification if the span has already been serialized.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: Sources/Sentry/SentryScope.m#L773

Potential issue: A race condition can occur in `addFeatureFlagWithName:`. The active
span is read at the beginning of the method, but the feature flag is added to it later.
If another thread finishes and serializes the transaction in between these two actions,
the feature flag will be added to the span's buffer after the buffer has already been
serialized. This results in the feature flag data being lost and not included in the
final transaction event, leading to a silent data loss without causing a crash.

Also affects:

  • Sources/Sentry/SentryScope.m:787~787

Did we get this right? 👍 / 👎 to inform future reviews.

@sentry

sentry Bot commented Jun 23, 2026

Copy link
Copy Markdown

📲 Install Builds

iOS

🔗 App Name App ID Version Configuration
SDK-Size io.sentry.sample.SDK-Size 9.18.0 (1) Release

⚙️ sentry-cocoa Build Distribution Settings

@github-actions

Copy link
Copy Markdown
Contributor

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 1229.51 ms 1260.96 ms 31.45 ms
Size 24.14 KiB 1.20 MiB 1.17 MiB

Baseline results on branch: feat/feature-flag-scope-api

Startup times

Revision Plain With Sentry Diff
076d4c6 1206.52 ms 1251.42 ms 44.89 ms
b0c666c 1221.48 ms 1255.76 ms 34.28 ms

App size

Revision Plain With Sentry Diff
076d4c6 24.14 KiB 1.20 MiB 1.18 MiB
b0c666c 24.14 KiB 1.20 MiB 1.17 MiB


- (void)addFeatureFlagWithName:(NSString *)name result:(BOOL)result
{
id<SentrySpan> activeSpan = self.span;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

m: I also think the activeSpan must be synchronized


- (void)removeFeatureFlagWithName:(NSString *)name
{
id<SentrySpan> activeSpan = self.span;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

m: Same as above


- (void)addFeatureFlagWithName:(NSString *)name result:(BOOL)result
{
[self.featureFlagBuffer addWithName:name result:result];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

m: Do we need to synchronize the feature flag buffer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-merge Use this label to trigger all PR workflows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants