-
Notifications
You must be signed in to change notification settings - Fork 136
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-182 Sync logs time with server time #65
Conversation
5c695d1
to
3fad6df
Compare
3fad6df
to
a97af1c
Compare
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.
I like it 🏅. Let me know WDYT about my ideas 🙂.
Tests/DatadogTests/Datadog/Utils/FoundationExtensionTests.swift
Outdated
Show resolved
Hide resolved
a97af1c
to
171e5dc
Compare
} | ||
} | ||
|
||
/// Synchronously uploads data to server using `HTTPClient`. | ||
internal final class DataUploader { | ||
private let uploadURL: DataUploadURL | ||
private let uploadURL: UploadURLProvider |
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 is a bit confusing to keep naming it uploadUrl
, why not uploadUrlProvider
?
Sources/Datadog/Datadog.swift
Outdated
@@ -103,14 +103,14 @@ public class Datadog { | |||
|
|||
internal convenience init( | |||
appContext: AppContext, | |||
logsUploadURL: DataUploadURL, | |||
logsUploadURL: UploadURLProvider, |
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.
logsUploadUrlProvider
?
171e5dc
to
3c756b7
Compare
func testUSLogsEndpoint() { | ||
XCTAssertEqual( | ||
Configuration.builderUsing(clientToken: "abcd").set(logsEndpoint: .us).build().logsUploadURL?.url, | ||
URL(string: "https://mobile-http-intake.logs.datadoghq.com/v1/input/abcd?ddsource=mobile")! | ||
) | ||
} | ||
|
||
func testEULogsEndpoint() { | ||
XCTAssertEqual( | ||
Configuration.builderUsing(clientToken: "abcd").set(logsEndpoint: .eu).build().logsUploadURL?.url, | ||
URL(string: "https://mobile-http-intake.logs.datadoghq.eu/v1/input/abcd?ddsource=mobile")! | ||
) | ||
} | ||
|
||
func testCustomLogsEndpoint() { | ||
XCTAssertEqual( | ||
Configuration.builderUsing(clientToken: "abcd") | ||
.set(logsEndpoint: .custom(url: "https://api.example.com/v1/logs/")) | ||
.build().logsUploadURL?.url, | ||
URL(string: "https://api.example.com/v1/logs/abcd?ddsource=mobile")! | ||
) | ||
} |
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.
3c756b7
to
b68cef8
Compare
|
||
let dateAgo = date15Dec2019 - 0.001 | ||
XCTAssertEqual(dateAgo.currentTimeMillis, 1_576_403_999_999) | ||
} |
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.
Let's also include test for the code branch which captures overflow, this can be easily done by using "reference date" as relative base:
} | |
} | |
func testUInt64Overflow() { | |
let dateWithOverflow = Date(timeIntervalSinceReferenceDate: .greatestFiniteMagnitude) | |
XCTAssertEqual(dateWithOverflow.currentTimeMillis, UInt64.max) | |
} |
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.
good idea 💡
i just noticed i forgot to add tests for init(withReportingOverflow:)
too
EDIT done ✅
class DatadogConfigurationTests: XCTestCase { | ||
private typealias Configuration = Datadog.Configuration | ||
|
||
func testDefaultConfiguration() { |
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.
We no longer test Configuration.Builder
behaviours. In fact, when we switch default endpoint from .us
to .eu
, none of Swift unit tests do fail.
This will test builder logic:
import XCTest
@testable import Datadog
class DatadogConfigurationTests: XCTestCase {
private typealias Configuration = Datadog.Configuration
func testDefaultConfiguration() {
let defaultConfiguration = Configuration.builderUsing(clientToken: "abcd").build()
XCTAssertEqual(defaultConfiguration.clientToken, "abcd")
XCTAssertEqual(defaultConfiguration.logsEndpoint.url, "https://mobile-http-intake.logs.datadoghq.com/v1/input/")
}
// MARK: - Logs endpoint
func testUSLogsEndpoint() {
let configuration = Configuration.builderUsing(clientToken: .mockAny()).set(logsEndpoint: .us).build()
XCTAssertEqual(configuration.logsEndpoint.url, "https://mobile-http-intake.logs.datadoghq.com/v1/input/")
}
func testEULogsEndpoint() {
let configuration = Configuration.builderUsing(clientToken: .mockAny()).set(logsEndpoint: .eu).build()
XCTAssertEqual(configuration.logsEndpoint.url, "https://mobile-http-intake.logs.datadoghq.eu/v1/input/")
}
func testCustomLogsEndpoint() {
let configuration = Configuration.builderUsing(clientToken: .mockAny())
.set(logsEndpoint: .custom(url: "https://api.example.com/v1/logs/"))
.build()
XCTAssertEqual(configuration.logsEndpoint.url, "https://api.example.com/v1/logs/")
}
}
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.
copied&pasted ✅
|
||
// MARK: - Logs endpoint | ||
|
||
func testUSLogsEndpoint() { | ||
let urlUS = Datadog.Configuration.LogsEndpoint.us.url | ||
let urlProvider = try! UploadURLProvider( | ||
endpointURL: urlUS, | ||
clientToken: "abc", | ||
dateProvider: dateProvider | ||
) | ||
XCTAssertEqual(urlProvider.url, URL(string: "https://mobile-http-intake.logs.datadoghq.com/v1/input/abc?ddsource=mobile&batch_time=1576404000000")) | ||
} | ||
|
||
func testEULogsEndpoint() { | ||
let urlEU = Datadog.Configuration.LogsEndpoint.eu.url | ||
let urlProvider = try! UploadURLProvider( | ||
endpointURL: urlEU, | ||
clientToken: "abc", | ||
dateProvider: dateProvider | ||
) | ||
XCTAssertEqual(urlProvider.url, URL(string: "https://mobile-http-intake.logs.datadoghq.eu/v1/input/abc?ddsource=mobile&batch_time=1576404000000")) | ||
} | ||
|
||
func testCustomLogsEndpoint() { | ||
let urlCustom = Datadog.Configuration.LogsEndpoint.custom(url: "foo.bar").url | ||
let urlProvider = try! UploadURLProvider( | ||
endpointURL: urlCustom, | ||
clientToken: "abc", | ||
dateProvider: dateProvider | ||
) | ||
XCTAssertEqual(urlProvider.url, URL(string: "foo.bar/abc?ddsource=mobile&batch_time=1576404000000")) | ||
} |
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.
Those tests do exactly the same assertion as func testItBuildsValidURL()
- "When a given URL string is provided, UploadURLProvider
decorates it with expected URL queries". Asserting values of the URL for different endpoints (.us
, .eu
, .custom(...)
) is a better fit for configuration builder tests (DatadogConfigurationTests
) as we also need to assert the defaults it produces (when no endpoint is set explicitly).
In other words:
- in
DataUploaderTests
we can test:.us
endpoint string.eu
endpoint string.custom
endpoint string
- in
DatadogConfigurationTests
we could test:.us
endpoint string.eu
endpoint string.custom
endpoint string- default endpoint if not explicitly set by the user 👈
// MARK: - Logs endpoint | |
func testUSLogsEndpoint() { | |
let urlUS = Datadog.Configuration.LogsEndpoint.us.url | |
let urlProvider = try! UploadURLProvider( | |
endpointURL: urlUS, | |
clientToken: "abc", | |
dateProvider: dateProvider | |
) | |
XCTAssertEqual(urlProvider.url, URL(string: "https://mobile-http-intake.logs.datadoghq.com/v1/input/abc?ddsource=mobile&batch_time=1576404000000")) | |
} | |
func testEULogsEndpoint() { | |
let urlEU = Datadog.Configuration.LogsEndpoint.eu.url | |
let urlProvider = try! UploadURLProvider( | |
endpointURL: urlEU, | |
clientToken: "abc", | |
dateProvider: dateProvider | |
) | |
XCTAssertEqual(urlProvider.url, URL(string: "https://mobile-http-intake.logs.datadoghq.eu/v1/input/abc?ddsource=mobile&batch_time=1576404000000")) | |
} | |
func testCustomLogsEndpoint() { | |
let urlCustom = Datadog.Configuration.LogsEndpoint.custom(url: "foo.bar").url | |
let urlProvider = try! UploadURLProvider( | |
endpointURL: urlCustom, | |
clientToken: "abc", | |
dateProvider: dateProvider | |
) | |
XCTAssertEqual(urlProvider.url, URL(string: "foo.bar/abc?ddsource=mobile&batch_time=1576404000000")) | |
} |
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.
done ✅
UploadURLProvider dynamically injects current date as query to URLs
b68cef8
to
717f567
Compare
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.
💪
What and why?
• SDK needs to send current date in miliseconds in order for the backend to be able to tell time difference between the device and actual time.
• This is especially essential for tracing among different systems.
How?
DataUploadURL
can change query of its URL dynamicallyURLQueryProvider
is madeenum
so that we limit the number of cases to testReview checklist