Skip to content

Commit

Permalink
Updated Auth0 Telemetry Format (#256)
Browse files Browse the repository at this point in the history
* Updated Auth0 Telemetry Format
* Updated Tests
* Added Platform OS, Version
* Add Telemetry
  • Loading branch information
cocojoe authored Jan 28, 2019
1 parent 1a087ff commit 95b00a3
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 18 deletions.
61 changes: 51 additions & 10 deletions Auth0/Telemetry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public struct Telemetry {

static let NameKey = "name"
static let VersionKey = "version"
static let WrappedVersion = "lib_version"
static let WrappedVersion = "core"
static let EnvironmentKey = "env"

static let NoVersion = "0.0.0"
static let LibraryName = "Auth0.swift"
Expand All @@ -45,10 +46,16 @@ public struct Telemetry {

mutating func wrapped(inLibrary name: String, version: String) {
let info = Telemetry.versionInformation()
let wrapped = [
var env = Telemetry.generateEnviroment()
if let libVersion = info[Telemetry.VersionKey] as? String {
env[Telemetry.WrappedVersion] = libVersion
} else {
env[Telemetry.WrappedVersion] = Telemetry.NoVersion
}
let wrapped: [String: Any] = [
Telemetry.NameKey: name,
Telemetry.VersionKey: version,
Telemetry.WrappedVersion: info[Telemetry.VersionKey] ?? Telemetry.NoVersion
Telemetry.EnvironmentKey: env
]
self.info = Telemetry.generateValue(fromInfo: wrapped)
}
Expand All @@ -69,20 +76,54 @@ public struct Telemetry {
return items
}

static func versionInformation(bundle: Bundle = Bundle(for: Credentials.classForCoder())) -> [String: String] {
static func versionInformation(bundle: Bundle = Bundle(for: Credentials.classForCoder())) -> [String: Any] {
let version = bundle.infoDictionary?["CFBundleShortVersionString"] as? String ?? Telemetry.NoVersion
let dict = [
let dict: [String: Any] = [
Telemetry.NameKey: Telemetry.LibraryName,
Telemetry.VersionKey: version,
"swift-version": "3.0"
]
Telemetry.EnvironmentKey: Telemetry.generateEnviroment()
]
return dict
}

static func generateValue(fromInfo info: [String: String] = Telemetry.versionInformation()) -> String? {
static func generateEnviroment() -> [String: String] {
let platform = Telemetry.osInfo()
let env = [ "swift": Telemetry.swiftVersion(),
platform.0: platform.1
]
return env
}

static func generateValue(fromInfo info: [String: Any] = Telemetry.versionInformation()) -> String? {
let data = try? JSONSerialization.data(withJSONObject: info, options: [])
return data?.a0_encodeBase64URLSafe()
}

static func swiftVersion() -> String {
#if swift(>=4.0)
return "4.x"
#elseif swift(>=3.0)
return "3.x"
#endif
}

static func osPlatform() -> String {
#if os(iOS)
return "iOS"
#elseif os(OSX)
return "macOS"
#elseif os(tvOS)
return "tvOS"
#elseif os(watchOS)
return "watchOS"
#else
return "unknown"
#endif
}

static func osInfo() -> (String, String) {
return (self.osPlatform(), "\(ProcessInfo().operatingSystemVersion.majorVersion).\(ProcessInfo().operatingSystemVersion.minorVersion)")
}
}

public protocol Trackable {
Expand All @@ -95,7 +136,7 @@ extension Trackable {
/**
Avoid Auth0.swift sending its version on every request to Auth0 API.
By default we collect our libraries and SDKs versions to help us during support and evaluate usage.

- parameter enabled: if Auth0.swift should send it's version on every request.
*/
public mutating func tracking(enabled: Bool) {
Expand All @@ -104,7 +145,7 @@ extension Trackable {

/**
Send the library/framework, that has Auth0.swift as dependency, when sending telemetry information

- parameter name: name of library or framework that uses Auth0.swift
- parameter version: version of library or framework
*/
Expand Down
43 changes: 35 additions & 8 deletions Auth0Tests/TelemetrySpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,36 @@ class TelemetrySpec: QuickSpec {
bundle.version = nil
}

var subject: [String: String] {
var subject: [String: Any] {
return Telemetry.versionInformation(bundle: bundle)
}

pending("should return bundle default version if nil") {
expect(subject["version"]) == "0.0.0"
expect(subject["version"] as? String) == "0.0.0"
}

pending("should return bundle version") {
bundle.version = "1.0.0"
expect(subject["version"]) == "1.0.0"
expect(subject["version"] as? String) == "1.0.0"
}

it("should return lib name") {
expect(subject["name"]) == "Auth0.swift"
expect(subject["name"] as? String) == "Auth0.swift"
}

it("should have env info") {
let env = subject["env"] as! [String : String]
#if os(iOS)
expect(env["iOS"]).toNot(beNil())
#elseif os(OSX)
expect(env["macOS"]).toNot(beNil())
#elseif os(tvOS)
expect(env["tvOS"]).toNot(beNil())
#elseif os(watchOS)
expect(env["watchOS"]).toNot(beNil())
#else
expect(env["unknown"]).toNot(beNil())
#endif
}

}
Expand Down Expand Up @@ -112,10 +127,22 @@ class TelemetrySpec: QuickSpec {
let padded = value.padding(toLength: paddedLength, withPad: "=", startingAt: 0)

let data = Data(base64Encoded: padded, options: [NSData.Base64DecodingOptions.ignoreUnknownCharacters])
let info = try! JSONSerialization.jsonObject(with: data!, options: []) as! [String: String]
expect(info["version"]) == "1.0.0-alpha.4"
expect(info["lib_version"]) == version
expect(info["name"]) == "Lock.iOS"
let info = try! JSONSerialization.jsonObject(with: data!, options: []) as! [String: Any]
expect(info["version"] as? String) == "1.0.0-alpha.4"
expect(info["name"] as? String) == "Lock.iOS"
let env = info["env"] as! [String : String]
expect(env["core"]) == version as? String
#if os(iOS)
expect(env["iOS"]).toNot(beNil())
#elseif os(OSX)
expect(env["macOS"]).toNot(beNil())
#elseif os(tvOS)
expect(env["tvOS"]).toNot(beNil())
#elseif os(watchOS)
expect(env["watchOS"]).toNot(beNil())
#else
expect(env["unknown"]).toNot(beNil())
#endif
}
}

Expand Down

0 comments on commit 95b00a3

Please sign in to comment.