Skip to content

Commit

Permalink
RUMM-409 Update tracing with new rules for service, env and `vers…
Browse files Browse the repository at this point in the history
…ion`
  • Loading branch information
ncreated committed May 13, 2020
1 parent 38f769a commit 4c2a1c1
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 62 deletions.
4 changes: 2 additions & 2 deletions Sources/Datadog/DDTracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

public class DDTracer: Tracer {
/// Writes `Span` objects to output.
private let spanOutput: SpanOutput
internal let spanOutput: SpanOutput
/// Queue ensuring thread-safety of the `DDTracer` and `DDSpan` operations.
internal let queue: DispatchQueue

Expand Down Expand Up @@ -43,7 +43,7 @@ public class DDTracer: Tracer {
spanBuilder: SpanBuilder(
applicationVersion: tracingFeature.configuration.applicationVersion,
environment: tracingFeature.configuration.environment,
serviceName: tracerConfiguration.serviceName ?? "ios", // TODO: RUMM-409 use default service name
serviceName: tracerConfiguration.serviceName ?? tracingFeature.configuration.serviceName,
userInfoProvider: tracingFeature.userInfoProvider,
networkConnectionInfoProvider: tracingFeature.networkConnectionInfoProvider,
carrierInfoProvider: tracingFeature.carrierInfoProvider
Expand Down
2 changes: 1 addition & 1 deletion Sources/Datadog/DatadogConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ extension Datadog {
internal let environment: String

internal let logsUploadURLWithClientToken: URL
internal let tracesUploadURLWithClientToken: URL // TODO: RUMM-409 add tests
internal let tracesUploadURLWithClientToken: URL
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Datadog/Tracing/Span/SpanEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internal struct SpanEncoder {
// MARK: - Meta

case source = "meta._dd.source"
case applicationVersion = "meta.application.version" // TODO: RUMM-409 Change to `meta.version`? - check with Android
case applicationVersion = "meta.version"
case tracerVersion = "meta.tracer.version"

case userId = "meta.usr.id"
Expand Down
3 changes: 1 addition & 2 deletions Sources/Datadog/Tracing/SpanOutputs/SpanFileOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import Foundation
internal struct SpanFileOutput: SpanOutput {
let spanBuilder: SpanBuilder
let fileWriter: FileWriter
let environment: String = "staging" // TODO: RUMM-409 Receive `env` value from the user

func write(ddspan: DDSpan, finishTime: Date) {
do {
let span = try spanBuilder.createSpan(from: ddspan, finishTime: finishTime)
let envelope = SpanEnvelope(span: span, environment: environment)
let envelope = SpanEnvelope(span: span, environment: spanBuilder.environment)
fileWriter.write(value: envelope)
} catch {
userLogger.error("🔥 Failed to build span: \(error)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class TracingIntegrationTests: IntegrationTests {
XCTAssertEqual(try matcher.resource(), try matcher.operationName())
XCTAssertEqual(try matcher.type(), "custom")
XCTAssertEqual(try matcher.isError(), 0)
XCTAssertEqual(try matcher.environment(), "staging")
XCTAssertEqual(try matcher.environment(), "integration")

XCTAssertEqual(try matcher.meta.source(), "mobile")
XCTAssertEqual(try matcher.meta.tracerVersion().split(separator: ".").count, 3)
Expand Down
59 changes: 56 additions & 3 deletions Tests/DatadogTests/Datadog/DDTracerConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,67 @@ import XCTest
@testable import Datadog

class DDTracerConfigurationTests: XCTestCase {
private typealias Configuration = DDTracer.Configuration
private let networkConnectionInfoProvider: NetworkConnectionInfoProviderMock = .mockAny()
private let carrierInfoProvider: CarrierInfoProviderMock = .mockAny()
private var mockServer: ServerMock! // swiftlint:disable:this implicitly_unwrapped_optional

override func setUp() {
super.setUp()
temporaryDirectory.create()

mockServer = ServerMock(delivery: .success(response: .mockResponseWith(statusCode: 200)))
TracingFeature.instance = .mockWorkingFeatureWith(
server: mockServer,
directory: temporaryDirectory,
configuration: .mockWith(
applicationVersion: "1.2.3",
serviceName: "service-name",
environment: "tests"
),
networkConnectionInfoProvider: networkConnectionInfoProvider,
carrierInfoProvider: carrierInfoProvider
)
}

override func tearDown() {
mockServer.waitAndAssertNoRequestsSent()
TracingFeature.instance = nil
mockServer = nil

temporaryDirectory.delete()
super.tearDown()
}

func testDefaultTracer() {
// TODO: RUMM-409 write test
let tracer = DDTracer.initialize(configuration: .init()).dd

guard let spanBuilder = (tracer.spanOutput as? SpanFileOutput)?.spanBuilder else {
XCTFail()
return
}

XCTAssertEqual(spanBuilder.applicationVersion, "1.2.3")
XCTAssertEqual(spanBuilder.serviceName, "service-name")
XCTAssertEqual(spanBuilder.environment, "tests")
XCTAssertNotNil(spanBuilder.networkConnectionInfoProvider) // TODO: RUMM-422 Assert it's `nil` by default
XCTAssertNotNil(spanBuilder.carrierInfoProvider) // TODO: RUMM-422 Assert it's `nil` by default
}

func testCustomizedTracer() {
// TODO: RUMM-409 write test
let tracer = DDTracer.initialize(
configuration: .init(serviceName: "custom-service-name")
).dd

guard let spanBuilder = (tracer.spanOutput as? SpanFileOutput)?.spanBuilder else {
XCTFail()
return
}

XCTAssertEqual(spanBuilder.applicationVersion, "1.2.3")
XCTAssertEqual(spanBuilder.serviceName, "custom-service-name")
XCTAssertEqual(spanBuilder.environment, "tests")
XCTAssertNotNil(spanBuilder.networkConnectionInfoProvider) // TODO: RUMM-422 Disable network info and assert it's `nil`
XCTAssertNotNil(spanBuilder.carrierInfoProvider) // TODO: RUMM-422 Disable network info and assert it's `nil`
}
}

Expand Down
8 changes: 4 additions & 4 deletions Tests/DatadogTests/Datadog/DDTracerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class DDTracerTests: XCTestCase {
applicationVersion: "1.0.0",
applicationBundleIdentifier: "com.datadoghq.ios-sdk",
serviceName: "default-service-name",
environment: "tests"
environment: "custom"
),
dateProvider: RelativeDateProvider(using: .mockDecember15th2019At10AMUTC()),
tracingUUIDGenerator: RelativeTracingUUIDGenerator(startingFrom: 1)
Expand All @@ -57,14 +57,14 @@ class DDTracerTests: XCTestCase {
"span_id": "2",
"parent_id": "0",
"name": "operation",
"service": "ios",
"service": "default-service-name",
"resource": "operation",
"start": 1576404000000000000,
"duration": 500000000,
"error": 0,
"type": "custom",
"meta.tracer.version": "\(sdkVersion)",
"meta.application.version": "1.0.0",
"meta.version": "1.0.0",
"meta._dd.source": "mobile",
"meta.network.client.available_interfaces": "wifi",
"meta.network.client.is_constrained": "0",
Expand All @@ -80,7 +80,7 @@ class DDTracerTests: XCTestCase {
"metrics._sampling_priority_v1": 1
}
],
"env": "staging"
"env": "custom"
}
""") // TOOD: RUMM-422 Network info is not send by default with spans
}
Expand Down
111 changes: 64 additions & 47 deletions Tests/DatadogTests/Datadog/DatadogConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,20 @@ class DatadogConfigurationTests: XCTestCase {
}

func testTracingEndpoints() {
// TODO: RUMM-409 write test
var configuration = Configuration.builderUsing(clientToken: .mockAny(), environment: .mockAny())
.set(tracesEndpoint: .us)
.build()
XCTAssertEqual(configuration.tracesEndpoint.url, "https://public-trace-http-intake.logs.datadoghq.com/v1/input/")

configuration = Configuration.builderUsing(clientToken: .mockAny(), environment: .mockAny())
.set(tracesEndpoint: .eu)
.build()
XCTAssertEqual(configuration.tracesEndpoint.url, "https://public-trace-http-intake.logs.datadoghq.eu/v1/input/")

configuration = Configuration.builderUsing(clientToken: .mockAny(), environment: .mockAny())
.set(tracesEndpoint: .custom(url: "https://api.example.com/v1/traces/"))
.build()
XCTAssertEqual(configuration.tracesEndpoint.url, "https://api.example.com/v1/traces/")
}
}

Expand Down Expand Up @@ -164,56 +177,60 @@ class DatadogValidConfigurationTests: XCTestCase {
}

func testLogsUploadURLValidation() throws {
func verify(clientToken: String, logsEndpoint: Datadog.Configuration.LogsEndpoint, expectedLogsUploadURL: URL) throws {
// it equals `Datadog.Configuration.environment`
let configuration = try Configuration(
configuration: .mockWith(clientToken: clientToken, logsEndpoint: logsEndpoint),
func configurationWith(
clientToken: String = "abc",
logsEndpoint: Datadog.Configuration.LogsEndpoint = .us,
tracesEndpoint: Datadog.Configuration.TracesEndpoint = .us
) throws -> Configuration {
return try Configuration(
configuration: .mockWith(clientToken: clientToken, logsEndpoint: logsEndpoint, tracesEndpoint: tracesEndpoint),
appContext: .mockAny()
)
XCTAssertEqual(configuration.logsUploadURLWithClientToken, expectedLogsUploadURL)
}
func verify(clientToken: String, logsEndpoint: Datadog.Configuration.LogsEndpoint, expectedError: String) {
XCTAssertThrowsError(
try Configuration(
configuration: .mockWith(clientToken: clientToken, logsEndpoint: logsEndpoint),
appContext: .mockAny()
)
) { error in
XCTAssertEqual((error as? ProgrammerError)?.description, expectedError)
}
}

try verify(
clientToken: "abc",
logsEndpoint: .us,
expectedLogsUploadURL: URL(string: "https://mobile-http-intake.logs.datadoghq.com/v1/input/abc")!
)
try verify(
clientToken: "abc",
logsEndpoint: .eu,
expectedLogsUploadURL: URL(string: "https://mobile-http-intake.logs.datadoghq.eu/v1/input/abc")!
)
try verify(
clientToken: "abc",
logsEndpoint: .custom(url: "http://example.com/api"),
expectedLogsUploadURL: URL(string: "http://example.com/api/abc")!
)
verify(clientToken: "", logsEndpoint: .us, expectedError: "🔥 Datadog SDK usage error: `clientToken` cannot be empty.")
verify(clientToken: "", logsEndpoint: .eu, expectedError: "🔥 Datadog SDK usage error: `clientToken` cannot be empty.")
verify(
clientToken: "",
logsEndpoint: .custom(url: URL.mockAny().absoluteString),
expectedError: "🔥 Datadog SDK usage error: `clientToken` cannot be empty."
)
verify(
clientToken: "abc",
logsEndpoint: .custom(url: ""),
expectedError: "🔥 Datadog SDK usage error: The `url` in `.custom(url:)` must be a valid URL string."
)
verify(
clientToken: "abc",
logsEndpoint: .custom(url: "not a valid url string"),
expectedError: "🔥 Datadog SDK usage error: The `url` in `.custom(url:)` must be a valid URL string."
// Valid fixtures:

XCTAssertEqual(
try configurationWith(clientToken: "abc", logsEndpoint: .us).logsUploadURLWithClientToken,
URL(string: "https://mobile-http-intake.logs.datadoghq.com/v1/input/abc")!
)
XCTAssertEqual(
try configurationWith(clientToken: "abc", logsEndpoint: .eu).logsUploadURLWithClientToken,
URL(string: "https://mobile-http-intake.logs.datadoghq.eu/v1/input/abc")!
)
XCTAssertEqual(
try configurationWith(clientToken: "abc", logsEndpoint: .custom(url: "http://example.com/api")).logsUploadURLWithClientToken,
URL(string: "http://example.com/api/abc")!
)
XCTAssertEqual(
try configurationWith(clientToken: "abc", tracesEndpoint: .us).tracesUploadURLWithClientToken,
URL(string: "https://public-trace-http-intake.logs.datadoghq.com/v1/input/abc")!
)
XCTAssertEqual(
try configurationWith(clientToken: "abc", tracesEndpoint: .eu).tracesUploadURLWithClientToken,
URL(string: "https://public-trace-http-intake.logs.datadoghq.eu/v1/input/abc")!
)
XCTAssertEqual(
try configurationWith(clientToken: "abc", tracesEndpoint: .custom(url: "http://example.com/api")).tracesUploadURLWithClientToken,
URL(string: "http://example.com/api/abc")!
)

// Invalid fixtures:

XCTAssertThrowsError(try configurationWith(clientToken: "")) { error in
XCTAssertEqual((error as? ProgrammerError)?.description, "🔥 Datadog SDK usage error: `clientToken` cannot be empty.")
}
XCTAssertThrowsError(try configurationWith(logsEndpoint: .custom(url: "not a valid url string"))) { error in
XCTAssertEqual(
(error as? ProgrammerError)?.description,
"🔥 Datadog SDK usage error: The `url` in `.custom(url:)` must be a valid URL string."
)
}
XCTAssertThrowsError(try configurationWith(tracesEndpoint: .custom(url: "not a valid url string"))) { error in
XCTAssertEqual(
(error as? ProgrammerError)?.description,
"🔥 Datadog SDK usage error: The `url` in `.custom(url:)` must be a valid URL string."
)
}
}
}
2 changes: 1 addition & 1 deletion Tests/DatadogTests/Matchers/SpanMatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ internal class SpanMatcher {
fileprivate let matcher: SpanMatcher

func source() throws -> String { try matcher.meta(forKeyPath: "meta._dd.source") }
func applicationVersion() throws -> String { try matcher.meta(forKeyPath: "meta.application.version") }
func applicationVersion() throws -> String { try matcher.meta(forKeyPath: "meta.version") }
func tracerVersion() throws -> String { try matcher.meta(forKeyPath: "meta.tracer.version") }

func userID() throws -> String { try matcher.meta(forKeyPath: "meta.usr.id") }
Expand Down

0 comments on commit 4c2a1c1

Please sign in to comment.