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

RUMM-2169 Isolate Logging feature from Core's configuration and dependencies #877

Conversation

ncreated
Copy link
Member

@ncreated ncreated commented Jun 2, 2022

What and why?

🔨🧰 This PR makes LoggingFeature no longer depend on CoreConfiguration and CoreDependencies.

This refactoring is required to better isolate Feature modules from Core implementation. It prepares the codebase for further introduction of SDK context in V2.

How?

There are two main changes and everything else is mainly their consequence:

  • Common configuration is now removed from FeaturesConfiguration.Logging
struct Logging {
-   let common: Common
   let uploadURL: URL
   let logEventMapper: LogEventMapper?
}
  • Common dependencies are now removed from LoggingFeature interface
internal final class LoggingFeature: V1FeatureInitializable {
   // ...

-   // MARK: - Dependencies
- 
-   let dateProvider: DateProvider
-   let dateCorrector: DateCorrectorType
-   let userInfoProvider: UserInfoProvider
-   let networkConnectionInfoProvider: NetworkConnectionInfoProviderType
-   let carrierInfoProvider: CarrierInfoProviderType

   // ...
}

In V2 both, "common configuration" and "common dependencies" will belong to the Core module and their values will be passed through SDK context.

🎁 I made a bonus change, which should help us in transitioning to V2-like Features testing. The way we now create LoggingFeature in tests does no longer depend on dummy DatadogCore instance. Instead, the feature is instantiated with mocked FeatureStorage and FeatureUpload:

  • FeatureUpload is now 100% no-op as we won't need any uploads in V2 Features tests;
  • FeatureStorage uses new InMemoryWriter, which records events in-memory and returns them back for assertions.

This bonus change is only scoped to configuration update in LoggingFeatureMocks and addition of InMemoryWriter. It's transparent for everything else.

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
  • Run integration tests
  • Run smoke tests

ncreated added 4 commits June 2, 2022 09:15
which means we're no longer testing Core as part of `LoggingFeature` tests.
Instead, the mock is installed early on the writting layer, and no-ops are used
for batches orchestration and upload.
because these behaviours should be tested as part of Core tests.

Whats more:
- the tracking consent logic is already covered in `FeatureStorage` tests,
- the upload condition logic is for now covered in `RUMMonitor` and `Tracer` tests,
but will be covered in `FeatureUpload` once all features are migrated in RUMM-2169.
@ncreated ncreated self-assigned this Jun 2, 2022
Comment on lines -727 to -730
// MARK: - Tracking Consent

func testWhenChangingConsentValues_itUploadsOnlyAuthorizedLogs() throws {
let consentProvider = ConsentProvider(initialConsent: .pending)
Copy link
Member Author

@ncreated ncreated Jun 2, 2022

Choose a reason for hiding this comment

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

I'm removing this test, because in V2 tracking consent will be fully managed by the Core module and transparent to Feature modules. We already have it covered in FeatureStorage tests.

I'm doing it now, because since this PR we're no longer running full Core backend in part of Logger tests. Both FeatureStorage and FeatureUpload are mocked, so we don't have their consent logic available.

Comment on lines -469 to -471
// MARK: - Sending logs with different network and battery conditions

func testGivenBadBatteryConditions_itDoesNotTryToSendLogs() throws {
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm removing these 2 tests, because we're no longer running full Core backend in Logger tests and the behaviour of conditional uploads is part of the Core. This needs to be tested separately as FeatureUpload tests. I plan to add such tests after migrating both Tracing and RUM in similar fashion.

Comment on lines +639 to +641
/// Waits until given number of events is written and returns data for these events.
/// Passing no `timeout` will result with picking the recommended timeout for unit tests.
func waitAndReturnEventsData(count: UInt, timeout: TimeInterval? = nil, file: StaticString = #file, line: UInt = #line) -> [Data] {
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 is copied from DataUploadWorkerMock as InMemoryWriter is aimed to replace it entirely after Tracing and RUM are migrated in next PRs.

@ncreated ncreated marked this pull request as ready for review June 2, 2022 08:50
@ncreated ncreated requested a review from a team as a code owner June 2, 2022 08:50
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.

Awesome!! The benefits in tests are already significant and this interceptedStorage with InMemoryWriter is perfect 👌

@@ -9,6 +9,11 @@ import Foundation

internal final class DatadogCoreMock: Flushable {
private var v1Features: [String: Any] = [:]
private var v1Context: DatadogV1Context?
Copy link
Member

Choose a reason for hiding this comment

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

Can this be internal so we can mutate the context during a test case? it could then be reset to nil or .mockAny() on flush, WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea 👌, it helps a lot in some repetitive tests like LoggerTests where we need to dance with the core each time.

Changed ✅, however, I left the init(v1Context:) too, as in some other tests the inline initialisation comes handy.

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.

2 participants