-
-
Notifications
You must be signed in to change notification settings - Fork 410
feat: Add feature flag models and buffers #8078
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
95f42f8
feat: Add feature flag models + buffers
denrase 5d4b29b
Merge branch 'main' into feat/feature-flag-models-and-buffer
denrase 237a412
reduce obj-c api footprint
denrase 1f97aa6
use internal minimal objs object instead of using accociated objects
denrase 9030176
update test repsonsibilites
denrase 994eb21
Merge branch 'main' into feat/feature-flag-models-and-buffer
denrase 8c1d090
add remove api
denrase e1bc725
refactorings, remove out of scope functionality
denrase 9725f33
Merge branch 'main' into feat/feature-flag-models-and-buffer
denrase 51b20de
create eval after early return
denrase 5614a54
cleanup
denrase 0f80c20
Merge branch 'main' into feat/feature-flag-models-and-buffer
denrase 585341c
link spec
denrase 2a2a95a
ref: objc wrapper so intent is clear
denrase 501b534
add index map
denrase 52ffdac
reserve capacity
denrase 5fb5c40
feeback changes
denrase 11ee533
Merge branch 'main' into feat/feature-flag-models-and-buffer
denrase 309a245
Merge branch 'main' into feat/feature-flag-models-and-buffer
denrase d851ecf
bump diffMax
denrase File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
Sources/Swift/FeatureFlags/SentryFeatureFlagBuffer.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| // swiftlint:disable missing_docs | ||
| import Foundation | ||
|
|
||
| private let maxScopeFeatureFlags = 100 | ||
| private let maxSpanFeatureFlags = 10 | ||
|
|
||
| final class SentryFeatureFlagBuffer { | ||
|
denrase marked this conversation as resolved.
|
||
| private let maxSize: Int | ||
| private let overflowBehavior: SentryFeatureFlagBufferOverflowBehavior | ||
| private let lock = NSLock() | ||
| private var evaluations: [SentryFeatureFlagEvaluation] | ||
| private var indexesByFlag: [String: Int] | ||
|
|
||
| init(maxSize: Int, | ||
| overflowBehavior: SentryFeatureFlagBufferOverflowBehavior, | ||
| evaluations: [SentryFeatureFlagEvaluation] = []) { | ||
| self.maxSize = maxSize | ||
| self.overflowBehavior = overflowBehavior | ||
| self.evaluations = evaluations | ||
|
denrase marked this conversation as resolved.
|
||
| self.indexesByFlag = [:] | ||
| rebuildIndexes() | ||
| } | ||
|
|
||
| static func scopeBuffer() -> SentryFeatureFlagBuffer { | ||
| SentryFeatureFlagBuffer( | ||
| // Error events record the 100 most recent, unique feature flag evaluations. | ||
| // https://develop.sentry.dev/sdk/foundations/client/integrations/feature-flags/#tracking-feature-flag-evaluations | ||
| maxSize: maxScopeFeatureFlags, | ||
| overflowBehavior: .dropOldest | ||
| ) | ||
| } | ||
|
|
||
| static func spanBuffer() -> SentryFeatureFlagBuffer { | ||
| SentryFeatureFlagBuffer( | ||
| // Spans track the first 10 feature flags evaluated within the span's scope. | ||
| // https://develop.sentry.dev/sdk/foundations/client/integrations/feature-flags/#tracking-feature-flag-evaluations | ||
| maxSize: maxSpanFeatureFlags, | ||
| overflowBehavior: .rejectNew | ||
| ) | ||
| } | ||
|
|
||
| var allEvaluations: [SentryFeatureFlagEvaluation] { | ||
| return lock.synchronized { | ||
| evaluations | ||
| } | ||
| } | ||
|
|
||
| func add<Value: SentryFeatureFlagValue>(name: String, value: Value) { | ||
| lock.synchronized { | ||
| guard maxSize > 0 else { | ||
| return | ||
| } | ||
| let evaluation = SentryFeatureFlagEvaluation(flag: name, result: value.asSentryFeatureFlagValue) | ||
|
denrase marked this conversation as resolved.
|
||
| if let existingIndex = indexesByFlag[evaluation.flag] { | ||
| switch overflowBehavior { | ||
| case .dropOldest: | ||
| evaluations.remove(at: existingIndex) | ||
| evaluations.append(evaluation) | ||
| rebuildIndexes() | ||
| case .rejectNew: | ||
| evaluations[existingIndex] = evaluation | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if evaluations.count >= maxSize { | ||
| switch overflowBehavior { | ||
| case .dropOldest: | ||
| evaluations.removeFirst() | ||
| rebuildIndexes() | ||
| case .rejectNew: | ||
| return | ||
| } | ||
| } | ||
|
|
||
| indexesByFlag[evaluation.flag] = evaluations.count | ||
| evaluations.append(evaluation) | ||
| } | ||
| } | ||
|
|
||
| func remove(name: String) { | ||
| lock.synchronized { | ||
| guard let index = indexesByFlag[name] else { | ||
| return | ||
| } | ||
| evaluations.remove(at: index) | ||
| rebuildIndexes() | ||
| } | ||
| } | ||
|
|
||
| func removeAll() { | ||
| lock.synchronized { | ||
| evaluations.removeAll() | ||
| indexesByFlag.removeAll() | ||
| } | ||
| } | ||
|
|
||
| func serializeForContext() -> [String: Any]? { | ||
| let values = allEvaluations.map { $0.serializeForContext() } | ||
| guard !values.isEmpty else { | ||
| return nil | ||
| } | ||
| return ["values": values] | ||
| } | ||
|
|
||
| func serializeForSpanData() -> [String: Any] { | ||
| return allEvaluations.reduce(into: [String: Any]()) { result, evaluation in | ||
| result[evaluation.spanDataKey] = evaluation.result.serializedValue | ||
| } | ||
| } | ||
|
|
||
| func copy() -> SentryFeatureFlagBuffer { | ||
| return SentryFeatureFlagBuffer( | ||
| maxSize: maxSize, | ||
| overflowBehavior: overflowBehavior, | ||
| evaluations: allEvaluations | ||
| ) | ||
| } | ||
|
|
||
| private func rebuildIndexes() { | ||
| indexesByFlag.removeAll(keepingCapacity: true) | ||
| for (index, evaluation) in evaluations.enumerated() { | ||
| indexesByFlag[evaluation.flag] = index | ||
| } | ||
| } | ||
| } | ||
| // swiftlint:enable missing_docs | ||
6 changes: 6 additions & 0 deletions
6
Sources/Swift/FeatureFlags/SentryFeatureFlagBufferOverflowBehavior.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // swiftlint:disable missing_docs | ||
| enum SentryFeatureFlagBufferOverflowBehavior { | ||
| case dropOldest | ||
| case rejectNew | ||
| } | ||
| // swiftlint:enable missing_docs |
48 changes: 48 additions & 0 deletions
48
Sources/Swift/FeatureFlags/SentryFeatureFlagBufferWrapper.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // swiftlint:disable missing_docs | ||
| @_implementationOnly import _SentryPrivate | ||
| import Foundation | ||
|
|
||
| // Objective-C scope/span internals need to own and serialize feature flag buffers, but | ||
| // SentryFeatureFlagBuffer is a pure Swift type. Keep this wrapper thin: ObjC gets only the | ||
| // serialization/copying methods it needs, while Swift can access the wrapped buffer directly. | ||
| @_spi(Private) | ||
| @objc(SentryFeatureFlagBufferWrapper) | ||
| public final class SentryFeatureFlagBufferWrapper: NSObject { | ||
| let buffer: SentryFeatureFlagBuffer | ||
|
|
||
| private init(buffer: SentryFeatureFlagBuffer) { | ||
| self.buffer = buffer | ||
| super.init() | ||
| } | ||
|
|
||
| @objc | ||
| public static func scopeBuffer() -> SentryFeatureFlagBufferWrapper { | ||
| SentryFeatureFlagBufferWrapper(buffer: SentryFeatureFlagBuffer.scopeBuffer()) | ||
| } | ||
|
|
||
| @objc | ||
| public static func spanBuffer() -> SentryFeatureFlagBufferWrapper { | ||
| SentryFeatureFlagBufferWrapper(buffer: SentryFeatureFlagBuffer.spanBuffer()) | ||
| } | ||
|
|
||
| @objc | ||
| public func removeAll() { | ||
| buffer.removeAll() | ||
| } | ||
|
|
||
| @objc | ||
| public func copyBuffer() -> SentryFeatureFlagBufferWrapper { | ||
| SentryFeatureFlagBufferWrapper(buffer: buffer.copy()) | ||
| } | ||
|
|
||
| @objc | ||
| public func serializeForContext() -> [String: Any]? { | ||
| buffer.serializeForContext() | ||
| } | ||
|
|
||
| @objc | ||
| public func serializeForSpanData() -> [String: Any] { | ||
| buffer.serializeForSpanData() | ||
| } | ||
| } | ||
| // swiftlint:enable missing_docs |
27 changes: 27 additions & 0 deletions
27
Sources/Swift/FeatureFlags/SentryFeatureFlagEvaluation.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // swiftlint:disable missing_docs | ||
| struct SentryFeatureFlagEvaluation: Equatable { | ||
| static let spanDataKeyPrefix = "flag.evaluation." | ||
|
denrase marked this conversation as resolved.
Outdated
|
||
|
|
||
| let flag: String | ||
| let result: SentryFeatureFlagValueContent | ||
|
|
||
| static func spanDataKey(for flag: String) -> String { | ||
| return "\(spanDataKeyPrefix)\(flag)" | ||
| } | ||
|
|
||
| var spanDataKey: String { | ||
| return Self.spanDataKey(for: flag) | ||
| } | ||
|
denrase marked this conversation as resolved.
Outdated
|
||
|
|
||
| func serializeForContext() -> [String: Any] { | ||
| return [ | ||
| "flag": flag, | ||
| "result": result.serializedValue | ||
| ] | ||
| } | ||
|
|
||
| func serializeForSpanData() -> [String: Any] { | ||
| return [spanDataKey: result.serializedValue] | ||
| } | ||
| } | ||
| // swiftlint:enable missing_docs | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // swiftlint:disable missing_docs | ||
| protocol SentryFeatureFlagValue { | ||
| var asSentryFeatureFlagValue: SentryFeatureFlagValueContent { get } | ||
| } | ||
|
|
||
| extension Bool: SentryFeatureFlagValue { | ||
| var asSentryFeatureFlagValue: SentryFeatureFlagValueContent { | ||
| return .boolean(self) | ||
| } | ||
| } | ||
| // swiftlint:enable missing_docs |
12 changes: 12 additions & 0 deletions
12
Sources/Swift/FeatureFlags/SentryFeatureFlagValueContent.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // swiftlint:disable missing_docs | ||
| enum SentryFeatureFlagValueContent: Equatable { | ||
| case boolean(Bool) | ||
|
|
||
| var serializedValue: Any { | ||
| switch self { | ||
| case .boolean(let value): | ||
| return value | ||
| } | ||
| } | ||
| } | ||
| // swiftlint:enable missing_docs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // swiftlint:disable missing_docs | ||
| @_implementationOnly import _SentryPrivate | ||
|
|
||
| // Feature flag APIs live in this file so the eventual public Scope API has a clear home. | ||
| // These methods stay SPI while the public surface is being finalized. | ||
| extension Scope { | ||
| @_spi(Private) public func addFeatureFlag(name: String, result: Bool) { | ||
| guard let wrapper = featureFlagBuffer as? SentryFeatureFlagBufferWrapper else { | ||
| return | ||
| } | ||
| wrapper.buffer.add(name: name, value: result) | ||
| } | ||
|
|
||
| @_spi(Private) public func removeFeatureFlag(name: String) { | ||
| guard let wrapper = featureFlagBuffer as? SentryFeatureFlagBufferWrapper else { | ||
| return | ||
| } | ||
| wrapper.buffer.remove(name: name) | ||
| } | ||
| } | ||
| // swiftlint:enable missing_docs |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.