Skip to content

Commit

Permalink
Merge pull request #102 from DataDog/ncreated/RUMM-409-update-service…
Browse files Browse the repository at this point in the history
…-env-and-version

RUMM-409 Unify `service`, `env` and `version` across all SDKs
  • Loading branch information
ncreated authored May 12, 2020
2 parents 9beeb56 + d289047 commit ca3cc71
Show file tree
Hide file tree
Showing 39 changed files with 778 additions and 620 deletions.
20 changes: 12 additions & 8 deletions Datadog/Datadog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
61133C4C2423990D00786299 /* LogsMocks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61133C1A2423990D00786299 /* LogsMocks.swift */; };
61133C4D2423990D00786299 /* CoreTelephonyMocks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61133C1B2423990D00786299 /* CoreTelephonyMocks.swift */; };
61133C4E2423990D00786299 /* UIKitMocks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61133C1C2423990D00786299 /* UIKitMocks.swift */; };
61133C4F2423990D00786299 /* DatadogObjcMocks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61133C1D2423990D00786299 /* DatadogObjcMocks.swift */; };
61133C512423990D00786299 /* DatadogMocks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61133C1F2423990D00786299 /* DatadogMocks.swift */; };
61133C522423990D00786299 /* FoundationMocks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61133C202423990D00786299 /* FoundationMocks.swift */; };
61133C532423990D00786299 /* MobileDeviceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61133C232423990D00786299 /* MobileDeviceTests.swift */; };
Expand Down Expand Up @@ -86,12 +85,14 @@
61133C712423993200786299 /* Datadog.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 61133B82242393DE00786299 /* Datadog.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
612983CD2449E62E00D4424B /* LoggingFeature.swift in Sources */ = {isa = PBXBuildFile; fileRef = 612983CC2449E62E00D4424B /* LoggingFeature.swift */; };
61345613244756E300E7DA6B /* PerformancePresetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61345612244756E300E7DA6B /* PerformancePresetTests.swift */; };
614E9EB3244719FA007EE3E1 /* Environment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 614E9EB2244719FA007EE3E1 /* Environment.swift */; };
614E9EB3244719FA007EE3E1 /* BundleType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 614E9EB2244719FA007EE3E1 /* BundleType.swift */; };
61B558CF2469561C001460D3 /* LoggerBuilderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61B558CE2469561C001460D3 /* LoggerBuilderTests.swift */; };
61BB2B1B244A185D009F3F56 /* PerformancePreset.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61BB2B1A244A185D009F3F56 /* PerformancePreset.swift */; };
61C363802436164B00C4D4E6 /* ObjcExceptionHandlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61C3637F2436164B00C4D4E6 /* ObjcExceptionHandlerTests.swift */; };
61C3638324361BE200C4D4E6 /* DatadogPrivateMocks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61C3638224361BE200C4D4E6 /* DatadogPrivateMocks.swift */; };
61C3638524361E9200C4D4E6 /* Globals.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61C3638424361E9200C4D4E6 /* Globals.swift */; };
61C36470243B5C8300C4D4E6 /* ServerMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61C3646F243B5C8300C4D4E6 /* ServerMock.swift */; };
61F8CC092469295500FE2908 /* DatadogConfigurationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61F8CC082469295500FE2908 /* DatadogConfigurationTests.swift */; };
61FB222D244A21ED00902D19 /* LoggingFeatureMocks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61FB222C244A21ED00902D19 /* LoggingFeatureMocks.swift */; };
61FB2230244E1BE900902D19 /* LoggingFeatureTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61FB222F244E1BE900902D19 /* LoggingFeatureTests.swift */; };
9E08587A242519FF001A3583 /* NetworkPathMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E085879242519FF001A3583 /* NetworkPathMonitor.swift */; };
Expand Down Expand Up @@ -208,7 +209,6 @@
61133C1A2423990D00786299 /* LogsMocks.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LogsMocks.swift; sourceTree = "<group>"; };
61133C1B2423990D00786299 /* CoreTelephonyMocks.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreTelephonyMocks.swift; sourceTree = "<group>"; };
61133C1C2423990D00786299 /* UIKitMocks.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIKitMocks.swift; sourceTree = "<group>"; };
61133C1D2423990D00786299 /* DatadogObjcMocks.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatadogObjcMocks.swift; sourceTree = "<group>"; };
61133C1F2423990D00786299 /* DatadogMocks.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatadogMocks.swift; sourceTree = "<group>"; };
61133C202423990D00786299 /* FoundationMocks.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationMocks.swift; sourceTree = "<group>"; };
61133C232423990D00786299 /* MobileDeviceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MobileDeviceTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -240,12 +240,14 @@
61133C472423990D00786299 /* DatadogExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatadogExtensions.swift; sourceTree = "<group>"; };
612983CC2449E62E00D4424B /* LoggingFeature.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggingFeature.swift; sourceTree = "<group>"; };
61345612244756E300E7DA6B /* PerformancePresetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PerformancePresetTests.swift; sourceTree = "<group>"; };
614E9EB2244719FA007EE3E1 /* Environment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Environment.swift; sourceTree = "<group>"; };
614E9EB2244719FA007EE3E1 /* BundleType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BundleType.swift; sourceTree = "<group>"; };
61B558CE2469561C001460D3 /* LoggerBuilderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggerBuilderTests.swift; sourceTree = "<group>"; };
61BB2B1A244A185D009F3F56 /* PerformancePreset.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PerformancePreset.swift; sourceTree = "<group>"; };
61C3637F2436164B00C4D4E6 /* ObjcExceptionHandlerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObjcExceptionHandlerTests.swift; sourceTree = "<group>"; };
61C3638224361BE200C4D4E6 /* DatadogPrivateMocks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatadogPrivateMocks.swift; sourceTree = "<group>"; };
61C3638424361E9200C4D4E6 /* Globals.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Globals.swift; sourceTree = "<group>"; };
61C3646F243B5C8300C4D4E6 /* ServerMock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerMock.swift; sourceTree = "<group>"; };
61F8CC082469295500FE2908 /* DatadogConfigurationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatadogConfigurationTests.swift; sourceTree = "<group>"; };
61FB222C244A21ED00902D19 /* LoggingFeatureMocks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggingFeatureMocks.swift; sourceTree = "<group>"; };
61FB222F244E1BE900902D19 /* LoggingFeatureTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggingFeatureTests.swift; sourceTree = "<group>"; };
9E085879242519FF001A3583 /* NetworkPathMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkPathMonitor.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -364,7 +366,7 @@
61133B9E2423979B00786299 /* Core */ = {
isa = PBXGroup;
children = (
614E9EB2244719FA007EE3E1 /* Environment.swift */,
614E9EB2244719FA007EE3E1 /* BundleType.swift */,
61BB2B1A244A185D009F3F56 /* PerformancePreset.swift */,
61133B9F2423979B00786299 /* Utils */,
61133BA12423979B00786299 /* System */,
Expand Down Expand Up @@ -551,7 +553,9 @@
children = (
61133C192423990D00786299 /* Mocks */,
61133C412423990D00786299 /* DatadogTests.swift */,
61F8CC082469295500FE2908 /* DatadogConfigurationTests.swift */,
61133C382423990D00786299 /* LoggerTests.swift */,
61B558CE2469561C001460D3 /* LoggerBuilderTests.swift */,
61133C212423990D00786299 /* Core */,
61133C392423990D00786299 /* Logs */,
61133C352423990D00786299 /* Utils */,
Expand All @@ -565,7 +569,6 @@
61133C1A2423990D00786299 /* LogsMocks.swift */,
61133C1B2423990D00786299 /* CoreTelephonyMocks.swift */,
61133C1C2423990D00786299 /* UIKitMocks.swift */,
61133C1D2423990D00786299 /* DatadogObjcMocks.swift */,
61133C1F2423990D00786299 /* DatadogMocks.swift */,
61C3638224361BE200C4D4E6 /* DatadogPrivateMocks.swift */,
61133C202423990D00786299 /* FoundationMocks.swift */,
Expand Down Expand Up @@ -1041,7 +1044,7 @@
61133BE42423979B00786299 /* LogEncoder.swift in Sources */,
61133BD82423979B00786299 /* HTTPClient.swift in Sources */,
61133BDB2423979B00786299 /* DatadogConfiguration.swift in Sources */,
614E9EB3244719FA007EE3E1 /* Environment.swift in Sources */,
614E9EB3244719FA007EE3E1 /* BundleType.swift in Sources */,
61133BCE2423979B00786299 /* BatteryStatusProvider.swift in Sources */,
61133BD52423979B00786299 /* DataUploadConditions.swift in Sources */,
612983CD2449E62E00D4424B /* LoggingFeature.swift in Sources */,
Expand Down Expand Up @@ -1069,6 +1072,7 @@
61C3638324361BE200C4D4E6 /* DatadogPrivateMocks.swift in Sources */,
61133C4C2423990D00786299 /* LogsMocks.swift in Sources */,
61133C542423990D00786299 /* NetworkConnectionInfoProviderTests.swift in Sources */,
61B558CF2469561C001460D3 /* LoggerBuilderTests.swift in Sources */,
61133C4A2423990D00786299 /* DDConfigurationTests.swift in Sources */,
61C363802436164B00C4D4E6 /* ObjcExceptionHandlerTests.swift in Sources */,
61133C602423990D00786299 /* HTTPHeadersTests.swift in Sources */,
Expand All @@ -1089,12 +1093,12 @@
61133C492423990D00786299 /* DDLoggerBuilderTests.swift in Sources */,
61133C4B2423990D00786299 /* DDLoggerTests.swift in Sources */,
61133C482423990D00786299 /* DDDatadogTests.swift in Sources */,
61133C4F2423990D00786299 /* DatadogObjcMocks.swift in Sources */,
61133C522423990D00786299 /* FoundationMocks.swift in Sources */,
61133C5B2423990D00786299 /* DirectoryTests.swift in Sources */,
61133C562423990D00786299 /* CarrierInfoProviderTests.swift in Sources */,
9E36D92224373EA700BFBDB7 /* SwiftExtensionsTests.swift in Sources */,
61133C652423990D00786299 /* LogBuilderTests.swift in Sources */,
61F8CC092469295500FE2908 /* DatadogConfigurationTests.swift in Sources */,
61133C642423990D00786299 /* LoggerTests.swift in Sources */,
61133C4E2423990D00786299 /* UIKitMocks.swift in Sources */,
61133C4D2423990D00786299 /* CoreTelephonyMocks.swift in Sources */,
Expand Down
5 changes: 4 additions & 1 deletion Shopist/Shopist/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
Datadog.initialize(
appContext: .init(),
configuration: Datadog.Configuration
.builderUsing(clientToken: config.clientToken) // use your own client token obtained on Datadog website)
.builderUsing(
clientToken: config.clientToken, // use your own client token obtained on Datadog website
environment: "tests"
)
.build()
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import Foundation

/// An environment running the SDK.
internal enum Environment: Equatable {
/// A type of the bundle running the SDK.
internal enum BundleType: String {
case iOSApp
case iOSAppExtension
}
4 changes: 2 additions & 2 deletions Sources/Datadog/Core/PerformancePreset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ internal struct PerformancePreset: Equatable {
logsUploadDelayDecreaseFactor: 0.5 // reduce significantly for more uploads in short-lived extensions
)

static func best(for environment: Environment) -> PerformancePreset {
switch environment {
static func best(for bundleType: BundleType) -> PerformancePreset {
switch bundleType {
case .iOSApp: return `default`
case .iOSAppExtension: return instantDataDelivery
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Datadog/Core/System/MobileDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ internal class MobileDevice {

/// Returns current mobile device if `UIDevice` is available on this platform.
/// On other platforms returns `nil`.
static var current: MobileDevice? {
static var current: MobileDevice {
#if !targetEnvironment(simulator)
// Real device
return MobileDevice(uiDevice: UIDevice.current, processInfo: ProcessInfo.processInfo)
Expand Down
24 changes: 7 additions & 17 deletions Sources/Datadog/Core/Upload/DataUploader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Foundation

/// Creates URL and adds query items before providing them
internal class UploadURLProvider {
private let rawURL: URL
private let urlWithClientToken: URL
private let dateProvider: DateProvider

private var queryItems: [URLQueryItem] {
Expand All @@ -22,29 +22,19 @@ internal class UploadURLProvider {
}

var url: URL {
var urlComponents = URLComponents(url: rawURL, resolvingAgainstBaseURL: false)
var urlComponents = URLComponents(url: urlWithClientToken, resolvingAgainstBaseURL: false)
urlComponents?.percentEncodedQueryItems = queryItems

guard let url = urlComponents?.url else {
userLogger.error("🔥 Failed to create URL from \(rawURL) with \(queryItems)")
developerLogger?.error("🔥 Failed to create URL from \(rawURL) with \(queryItems)")
return rawURL
userLogger.error("🔥 Failed to create URL from \(urlWithClientToken) with \(queryItems)")
developerLogger?.error("🔥 Failed to create URL from \(urlWithClientToken) with \(queryItems)")
return urlWithClientToken
}
return url
}

init(endpointURL: String, clientToken: String, dateProvider: DateProvider) throws {
guard !endpointURL.isEmpty, let endpointURL = URL(string: endpointURL) else {
throw ProgrammerError(description: "`endpointURL` cannot be empty.")
}
guard !clientToken.isEmpty else {
throw ProgrammerError(description: "`clientToken` cannot be empty.")
}
let endpointURLWithClientToken = endpointURL.appendingPathComponent(clientToken)
guard let url = URL(string: endpointURLWithClientToken.absoluteString) else {
throw ProgrammerError(description: "Cannot build logs upload URL.")
}
self.rawURL = url
init(urlWithClientToken: URL, dateProvider: DateProvider) {
self.urlWithClientToken = urlWithClientToken
self.dateProvider = dateProvider
}
}
Expand Down
22 changes: 5 additions & 17 deletions Sources/Datadog/Core/Upload/HTTPHeaders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,10 @@ internal struct HTTPHeaders {

let all: [String: String]

init(appContext: AppContext) {
// When running on mobile, `User-Agent` header is customized (e.x. `app-name/1 CFNetwork (iPhone; iOS/13.3)`).
// Other platforms will fall back to default UA header set by OS.
if let mobileDevice = appContext.mobileDevice {
let appName = appContext.executableName ?? "Datadog"
let appVersion = appContext.bundleVersion ?? sdkVersion
let device = mobileDevice

self.all = [
Constants.contentTypeField: Constants.contentTypeValue,
Constants.userAgentField: "\(appName)/\(appVersion) CFNetwork (\(device.model); \(device.osName)/\(device.osVersion))"
]
} else {
self.all = [
Constants.contentTypeField: Constants.contentTypeValue
]
}
init(appName: String, appVersion: String, device: MobileDevice) {
self.all = [
Constants.contentTypeField: Constants.contentTypeValue,
Constants.userAgentField: "\(appName)/\(appVersion) CFNetwork (\(device.model); \(device.osName)/\(device.osVersion))"
]
}
}
47 changes: 24 additions & 23 deletions Sources/Datadog/Datadog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,35 @@ internal let sdkVersion = "1.1.0"
public class Datadog {
/// Provides information about the app.
public struct AppContext {
internal let environment: Environment
internal let bundleType: BundleType
internal let bundleIdentifier: String?
/// Executable version (i.e. application version or app extension version)
internal let bundleVersion: String?
internal let bundleShortVersion: String?
internal let executableName: String?
/// Describes current mobile device if SDK runs on a platform that supports `UIKit`.
internal let mobileDevice: MobileDevice?
/// Executable name (i.e. application name or app extension name)
internal let bundleName: String?

public init(mainBundle: Bundle = Bundle.main) {
let bundleVersion = mainBundle.object(forInfoDictionaryKey: "CFBundleVersion") as? String
let bundleShortVersion = mainBundle.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String

self.init(
environment: mainBundle.bundlePath.hasSuffix(".appex") ? .iOSAppExtension : .iOSApp,
bundleType: mainBundle.bundlePath.hasSuffix(".appex") ? .iOSAppExtension : .iOSApp,
bundleIdentifier: mainBundle.bundleIdentifier,
bundleVersion: mainBundle.object(forInfoDictionaryKey: "CFBundleVersion") as? String,
bundleShortVersion: mainBundle.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String,
executableName: mainBundle.object(forInfoDictionaryKey: "CFBundleExecutable") as? String,
mobileDevice: MobileDevice.current
bundleVersion: bundleShortVersion ?? bundleVersion,
bundleName: mainBundle.object(forInfoDictionaryKey: "CFBundleExecutable") as? String
)
}

internal init(
environment: Environment,
bundleType: BundleType,
bundleIdentifier: String?,
bundleVersion: String?,
bundleShortVersion: String?,
executableName: String?,
mobileDevice: MobileDevice?
bundleName: String?
) {
self.environment = environment
self.bundleType = bundleType
self.bundleIdentifier = bundleIdentifier
self.bundleVersion = bundleVersion
self.bundleShortVersion = bundleShortVersion
self.executableName = executableName
self.mobileDevice = mobileDevice
self.bundleName = bundleName
}
}

Expand Down Expand Up @@ -85,13 +81,17 @@ public class Datadog {
guard Datadog.instance == nil else {
throw ProgrammerError(description: "SDK is already initialized.")
}
let logsUploadURLProvider = try UploadURLProvider(
endpointURL: configuration.logsEndpoint.url,
clientToken: configuration.clientToken,
let validConfiguration = try ValidConfiguration(
configuration: configuration,
appContext: appContext
)

let logsUploadURLProvider = UploadURLProvider(
urlWithClientToken: validConfiguration.logsUploadURLWithClientToken,
dateProvider: SystemDateProvider()
)

let performance = PerformancePreset.best(for: appContext.environment)
let performance = PerformancePreset.best(for: appContext.bundleType)
let dateProvider = SystemDateProvider()
let userInfoProvider = UserInfoProvider()
let networkConnectionInfoProvider = NetworkConnectionInfoProvider()
Expand All @@ -107,8 +107,9 @@ public class Datadog {

LoggingFeature.instance = LoggingFeature(
directory: try obtainLoggingFeatureDirectory(),
appContext: appContext,
configuration: validConfiguration,
performance: performance,
mobileDevice: MobileDevice.current,
httpClient: httpClient,
logsUploadURLProvider: logsUploadURLProvider,
dateProvider: dateProvider,
Expand Down
Loading

0 comments on commit ca3cc71

Please sign in to comment.