Skip to content
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

GH1618 Simplify (internal) flushing logic #1628

Merged

Conversation

ncreated
Copy link
Member

What and why?

🧰 The internal Datadog.internalFlushAndDeinitialize() does not work without setting DD_SDK_COMPILED_FOR_TESTING compiler flag. To simplify the reasoning about internal "flush", I propose to remove this macro from downstream logic. It will be still used to hide the public API behind this feature, see:

#if DD_SDK_COMPILED_FOR_TESTING
public static func flushAndDeinitialize(instanceName: String = CoreRegistry.defaultInstanceName) {
internalFlushAndDeinitialize(instanceName: instanceName)
}
#endif

Solves #1618

How?

The "flush" logic was protected twice:

  • first, the public API was hidden behind DD_SDK_COMPILED_FOR_TESTING flag,
  • second, part of its implementation was conditionally enabled on DD_SDK_COMPILED_FOR_TESTING.

The second occurrence was redundant and it is removed.

Review checklist

  • Feature or bugfix MUST have appropriate tests (unit, integration)
  • Make sure each commit and the PR mention the Issue number or JIRA reference
  • Add CHANGELOG entry for user facing changes

Custom CI job configuration (optional)

  • Run unit tests for Core, RUM, Trace, Logs, CR and WVT
  • Run unit tests for Session Replay
  • Run integration tests
  • Run smoke tests
  • Run tests for tools/

@ncreated ncreated self-assigned this Jan 11, 2024
@datadog-datadog-prod-us1
Copy link

datadog-datadog-prod-us1 bot commented Jan 11, 2024

Datadog Report

Branch report: ncreated/GH1618/allow-flushing-SDK-using-internal-API
Commit report: c37be37
Test service: dd-sdk-ios

✅ 0 Failed, 2479 Passed, 0 Skipped, 5m 8.75s Wall Time
🔻 Test Sessions change in coverage: 9 decreased, 3 increased, 1 no change

🔻 Code Coverage Decreases vs Default Branch (9)

This report shows up to 5 code coverage decreases.

  • test DatadogLogsTests tvOS 44.78% (Δ-0.17%) - Details
  • test DatadogLogsTests iOS 44.74% (Δ-0.15%) - Details
  • test DatadogInternalTests tvOS 81.21% (Δ-0.06%) - Details
  • test DatadogInternalTests iOS 81.20% (Δ-0.03%) - Details
  • test DatadogRUMTests iOS 79.39% (Δ-0.02%) - Details

@ncreated ncreated marked this pull request as ready for review January 11, 2024 11:43
@ncreated ncreated requested review from a team as code owners January 11, 2024 11:43
@@ -157,13 +157,11 @@ internal class FilesOrchestrator: FilesOrchestratorType {
.compactMap { try deleteFileIfItsObsolete(file: $0.file, fileCreationDate: $0.creationDate) }
.sorted(by: { $0.creationDate < $1.creationDate })

#if DD_SDK_COMPILED_FOR_TESTING
Copy link
Member

@maciejburda maciejburda Jan 11, 2024

Choose a reason for hiding this comment

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

Are we sure about this? When we do this "open" batch is going to be included.

This function is called in the DataUploadWorker as well (it happens in line 77: fileReader.readFiles(limit: maxBatchesPerUpload)).

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 logic is conditioned by ignoreFilesAgeWhenReading flag enabled from core.flushAndTearDown() which is only available when DD_SDK_COMPILED_FOR_TESTING macro is set:

#if DD_SDK_COMPILED_FOR_TESTING
public static func flushAndDeinitialize(instanceName: String = CoreRegistry.defaultInstanceName) {
internalFlushAndDeinitialize(instanceName: instanceName)
}
#endif

so I don't see how this may interrupt DataUploadWorker in production apps. The entire concept of "ignoring files age" was introduced for testing purpose (we want to force-send all data in tests). Does it sound ok to you @maciejburda?

Ultimately, I don't like using macros in this way as they shred the codebase. Since it was introduced a long time ago however, we managed to keep the impact of DD_SDK_COMPILED_FOR_TESTING very minimal. A better approach would be to abstract certain components and inject different implementation in tests - it implies a much bigger effort, but I hope we do it one day.

Copy link
Member

@maciejburda maciejburda Jan 11, 2024

Choose a reason for hiding this comment

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

Not sure what am I missing, but the modified function getReadableFiles(excludingFilesNamed excludedFileNames: Set<String> = [], limit: Int = .max) is being called by the DataUploadWorker dependency: FileReader.

Removing this #if makes the file orchestrator ignore file age for producation code as well, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

The logic we enable here is conditioned by ignoreFilesAgeWhenReading flag 🙂:

if ignoreFilesAgeWhenReading {
   return filesFromOldest
      .prefix(limit)
      .map { $0.file }
}

And this is only set to true in our tests 💡.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I get it now! Thanks for clarifying :)

Copy link
Member

@maxep maxep left a comment

Choose a reason for hiding this comment

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

👍

@ncreated ncreated merged commit 5c5a6dd into develop Jan 12, 2024
6 checks passed
@ncreated ncreated deleted the ncreated/GH1618/allow-flushing-SDK-using-internal-API branch January 12, 2024 10:57
@ncreated ncreated mentioned this pull request Jan 25, 2024
8 tasks
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.

3 participants