-
Notifications
You must be signed in to change notification settings - Fork 135
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
RUM-3569 Store and filter resource ids between sessions #1747
Merged
maciejburda
merged 13 commits into
develop
from
maciey/RUM-3569-store-and-filter-resource-ids
Apr 15, 2024
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d0a731c
RUM-3569 Use data store to remove resource duplicates between sessions
maciejburda 2aa2584
RUM-3569 Add tests
maciejburda 178cefd
RUM-3569 Register resources before session replay
maciejburda 40756d6
RUM-3569 Fix after rebase
maciejburda 342386b
RUM-3569 Refactor
maciejburda 7effbe1
RUM-3569 PR fixes
maciejburda c01730a
RUM-3569 Refactor to scope and add telemetry on errors
maciejburda 4a419bc
RUM-3569 Fix snapshot tests
maciejburda d743d18
RUM-3569 Update CHANGELOG.md
maciejburda 7468984
RUM-3569 Fix after rebase
maciejburda e2b5005
RUM-3569 PR fixes
maciejburda c920d8b
RUM-3569 Fix CHANGELOG
maciejburda 85647cb
RUM-3569 Revert after rebase
maciejburda 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 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 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 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 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 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 |
---|---|---|
|
@@ -15,26 +15,111 @@ internal protocol ResourcesWriting { | |
} | ||
|
||
internal class ResourcesWriter: ResourcesWriting { | ||
/// An instance of SDK core the SR feature is registered to. | ||
private weak var core: DatadogCoreProtocol? | ||
private let scope: FeatureScope | ||
private let encoder: JSONEncoder | ||
private let decoder: JSONDecoder | ||
|
||
@ReadWriteLock | ||
private var knownIdentifiers = Set<String>() { | ||
didSet { | ||
if let knownIdentifiers = knownIdentifiers.asData(encoder) { | ||
scope.dataStore.setValue( | ||
knownIdentifiers, | ||
forKey: Constants.knownResourcesKey | ||
) | ||
} | ||
} | ||
} | ||
|
||
init( | ||
core: DatadogCoreProtocol | ||
scope: FeatureScope, | ||
dataStoreResetTime: TimeInterval = TimeInterval(30).days, | ||
encoder: JSONEncoder = JSONEncoder(), | ||
decoder: JSONDecoder = JSONDecoder() | ||
) { | ||
self.core = core | ||
self.scope = scope | ||
self.encoder = encoder | ||
self.decoder = decoder | ||
self.scope.dataStore.value(forKey: Constants.storeCreationKey) { [weak self] result in | ||
do { | ||
if let storeCreation = try result.data()?.asTimeInterval(), Date().timeIntervalSince1970 - storeCreation < dataStoreResetTime { | ||
self?.scope.dataStore.value(forKey: Constants.knownResourcesKey) { result in | ||
switch result { | ||
case .value(let data, _): | ||
do { | ||
if let knownIdentifiers = try data.asKnownIdentifiers(decoder) { | ||
self?.knownIdentifiers.formUnion(knownIdentifiers) | ||
} | ||
} catch let error { | ||
self?.scope.telemetry.error("Failed to decode known identifiers", error: error) | ||
} | ||
default: | ||
break | ||
} | ||
} | ||
} else { // Reset if store was created more than 30 days ago | ||
self?.scope.dataStore.setValue( | ||
Date().timeIntervalSince1970.asData(), | ||
forKey: Constants.storeCreationKey | ||
) | ||
self?.scope.dataStore.removeValue(forKey: Constants.knownResourcesKey) | ||
} | ||
} catch let error { | ||
self?.scope.telemetry.error("Failed to decode store creation", error: error) | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Writing | ||
|
||
func write(resources: [EnrichedResource]) { | ||
guard let scope = core?.scope(for: ResourcesFeature.self) else { | ||
return | ||
} | ||
scope.eventWriteContext { _, recordWriter in | ||
resources.forEach { | ||
recordWriter.write(value: $0) | ||
scope.eventWriteContext { [weak self] _, recordWriter in | ||
let unknownResources = resources.filter { self?.knownIdentifiers.contains($0.identifier) == false } | ||
for resource in unknownResources { | ||
recordWriter.write(value: resource) | ||
} | ||
self?.knownIdentifiers.formUnion(Set(unknownResources.map { $0.identifier })) | ||
} | ||
} | ||
|
||
enum Constants { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this and below are |
||
static let knownResourcesKey = "known-resources" | ||
static let storeCreationKey = "store-creation" | ||
} | ||
} | ||
|
||
extension Data { | ||
enum SerializationError: Error { | ||
case invalidData | ||
} | ||
|
||
func asTimeInterval() throws -> TimeInterval { | ||
var value: TimeInterval = 0 | ||
guard count >= MemoryLayout.size(ofValue: value) else { | ||
throw SerializationError.invalidData | ||
} | ||
_ = Swift.withUnsafeMutableBytes(of: &value) { | ||
copyBytes(to: $0) | ||
} | ||
return value | ||
} | ||
|
||
func asKnownIdentifiers(_ decoder: JSONDecoder) throws -> Set<String>? { | ||
return try decoder.decode(Set<String>.self, from: self) | ||
} | ||
} | ||
|
||
extension TimeInterval { | ||
func asData() -> Data { | ||
return Swift.withUnsafeBytes(of: self) { | ||
Data($0) | ||
} | ||
} | ||
} | ||
|
||
extension Set<String> { | ||
func asData(_ encoder: JSONEncoder) -> Data? { | ||
return try? encoder.encode(self) // Never fails | ||
} | ||
} | ||
#endif |
This file contains 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 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interestingly enough - this order matters. Since we use resources data store in session replay recorder it requires resources feature to be registered before.
Raises a question if it would make sense to add new function that registers array of features.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be no longer the case after we switched from
core
to dependency onFeatureScope
. Starting from #1744 Make FeatureScope always available, theFeatureScope
is always available, even before certain feature is registered 🧹.