Skip to content

Commit

Permalink
Event Logging Override (#231)
Browse files Browse the repository at this point in the history
* Event Logging Override

* Update StatsigOptions.swift
  • Loading branch information
daniel-statsig authored Dec 16, 2023
1 parent 49b44b9 commit e8a36b8
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 26 deletions.
18 changes: 14 additions & 4 deletions Sources/Statsig/NetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,12 @@ class NetworkService {
urlComponents.host = ApiHost
urlComponents.path = endpoint.rawValue

if let override = self.statsigOptions.overrideURL {
urlComponents.scheme = override.scheme
urlComponents.host = override.host
urlComponents.port = override.port
if let override = self.statsigOptions.mainApiUrl {
urlComponents.applyOverride(override)
}

if endpoint == .logEvent, let loggingApiOverride = self.statsigOptions.logEventApiUrl {
urlComponents.applyOverride(loggingApiOverride)
}

guard let requestURL = urlComponents.url else {
Expand Down Expand Up @@ -287,3 +289,11 @@ class NetworkService {
}
}
}

extension URLComponents {
mutating func applyOverride(_ url: URL) {
scheme = url.scheme
host = url.host
port = url.port
}
}
36 changes: 28 additions & 8 deletions Sources/Statsig/StatsigOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,25 @@ public class StatsigOptions {
public var shutdownOnBackground = true;

/**
The endpoint to use for all SDK network requests. You should not need to override this (unless you have another API that implements the Statsig API endpoints)
The API to use for all SDK network requests. You should not need to override this (unless you have another API that implements the Statsig API endpoints)
*/
public var api = "https://\(ApiHost)";
public var api = "https://\(ApiHost)" {
didSet {
mainApiUrl = URL(string: api) ?? mainApiUrl
}
}

/**
The API to use for log_event network requests. You should not need to override this (unless you have another API that implements the Statsig /v1/log_event endpoint)
*/
public var eventLoggingApi = "https://\(ApiHost)" {
didSet {
logEventApiUrl = URL(string: eventLoggingApi) ?? logEventApiUrl
}
}

internal var overrideURL: URL?
internal var mainApiUrl: URL?
internal var logEventApiUrl: URL?
var environment: [String: String] = [:]

public init(initTimeout: Double? = 3.0,
Expand All @@ -79,7 +93,8 @@ public class StatsigOptions {
disableDiagnostics: Bool? = false,
disableHashing: Bool? = false,
shutdownOnBackground: Bool? = true,
api: String? = nil
api: String? = nil,
eventLoggingApi: String? = nil
)
{
if let initTimeout = initTimeout, initTimeout >= 0 {
Expand Down Expand Up @@ -122,10 +137,15 @@ public class StatsigOptions {
self.autoValueUpdateIntervalSec = internval
}

if let api = api, let url = URL(string: api) {
self.overrideURL = url
} else {
self.overrideURL = nil

if let api = api {
self.api = api
self.mainApiUrl = URL(string: api)
}

if let eventLoggingApi = eventLoggingApi {
self.eventLoggingApi = eventLoggingApi
self.logEventApiUrl = URL(string: eventLoggingApi)
}

self.overrideStableID = overrideStableID
Expand Down
56 changes: 52 additions & 4 deletions Tests/StatsigTests/ApiOverrideSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class ApiOverrideSpec: BaseSpec {
var request: URLRequest?


describe("When Overriden") {
describe("When Main API Overridden") {
func start() {
let opts = StatsigOptions(api: "http://api.override.com")
request = TestUtils.startWithStatusAndWait(options: opts)
Expand All @@ -28,7 +28,7 @@ final class ApiOverrideSpec: BaseSpec {
expect(request?.url?.absoluteString).to(equal("http://api.override.com/v1/initialize"))
}

it("calls initialize on the overridden api") {
it("calls log_event on the overridden api") {
start()
var hitLog = false
TestUtils.captureLogs(host: "api.override.com") { logs in
Expand All @@ -41,7 +41,7 @@ final class ApiOverrideSpec: BaseSpec {
}
}

describe("When Not Overriden") {
describe("When Not Overridden") {
func start() {
request = TestUtils.startWithStatusAndWait()
}
Expand All @@ -51,7 +51,7 @@ final class ApiOverrideSpec: BaseSpec {
expect(request?.url?.absoluteString).to(equal("https://api.statsig.com/v1/initialize"))
}

it("calls initialize on the statsig api") {
it("calls log_event on the statsig api") {
start()
var hitLog = false
TestUtils.captureLogs(host: "api.statsig.com") { logs in
Expand All @@ -64,5 +64,53 @@ final class ApiOverrideSpec: BaseSpec {
}
}

describe("When Logging API Overridden") {
func start() {
let opts = StatsigOptions(eventLoggingApi: "http://api.log.co.nz/")
request = TestUtils.startWithStatusAndWait(options: opts)
}

it("calls initialize on the statsig api") {
start()
expect(request?.url?.absoluteString).to(equal("https://api.statsig.com/v1/initialize"))
}

it("calls log_event on the overridden api.log.co.nz api") {
start()
var hitLog = false
TestUtils.captureLogs(host: "api.log.co.nz") { logs in
hitLog = true
}

Statsig.logEvent("test_event")
Statsig.shutdown()
expect(hitLog).toEventually(beTrue())
}
}

describe("When Main and Logging API Overridden") {
func start() {
let opts = StatsigOptions(api: "http://main.api", eventLoggingApi: "http://api.log.co.nz")
request = TestUtils.startWithStatusAndWait(options: opts)
}

it("calls initialize on the overridden api") {
start()
expect(request?.url?.absoluteString).to(equal("http://main.api/v1/initialize"))
}

it("calls log_event on the overridden api.log.co.nz api") {
start()
var hitLog = false
TestUtils.captureLogs(host: "api.log.co.nz") { logs in
hitLog = true
}

Statsig.logEvent("test_event")
Statsig.shutdown()
expect(hitLog).toEventually(beTrue())
}
}

}
}
2 changes: 1 addition & 1 deletion Tests/StatsigTests/AppLifecycleSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class AppLifecycleSpec: BaseSpec {

func startAndLog(shutdownOnBackground: Bool, tag: String) {
let opts = StatsigOptions(shutdownOnBackground: shutdownOnBackground)
opts.overrideURL = URL(string: "http://AppLifecycleSpec::\(tag)")
opts.mainApiUrl = URL(string: "http://AppLifecycleSpec::\(tag)")

_ = TestUtils.startWithStatusAndWait(options: opts)

Expand Down
2 changes: 1 addition & 1 deletion Tests/StatsigTests/CodableSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CodableSpec: BaseSpec {

describe("Codable") {
let opts = StatsigOptions(disableDiagnostics: true)
opts.overrideURL = URL(string: "http://CodableSpec")
opts.mainApiUrl = URL(string: "http://CodableSpec")

beforeEach {
_ = TestUtils.startWithResponseAndWait([
Expand Down
2 changes: 1 addition & 1 deletion Tests/StatsigTests/ExposureLoggingSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ExposureLoggingSpec: BaseSpec {

describe("ExposureLogging") {
let opts = StatsigOptions(disableDiagnostics: true)
opts.overrideURL = URL(string: "http://ExposureLoggingSpec")
opts.mainApiUrl = URL(string: "http://ExposureLoggingSpec")

var logs: [[String: Any]] = []
beforeEach {
Expand Down
2 changes: 1 addition & 1 deletion Tests/StatsigTests/ManualFlushSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class ManualFlushSpec: BaseSpec {

it("flushes the logger") {
let opts = StatsigOptions()
opts.overrideURL = URL(string: "http://ManualFlushSpec")
opts.mainApiUrl = URL(string: "http://ManualFlushSpec")

_ = TestUtils.startWithResponseAndWait([:], options: opts)
Statsig.logEvent("my_event")
Expand Down
2 changes: 1 addition & 1 deletion Tests/StatsigTests/NotAwaitingInitCallsSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class NotAwaitingInitCallsSpec: BaseSpec {
describe("Not Awaiting Init Calls") {

beforeEach {
options.overrideURL = URL(string: "http://NotAwaitingInitCallsSpec")
options.mainApiUrl = URL(string: "http://NotAwaitingInitCallsSpec")

TestUtils.clearStorage()

Expand Down
2 changes: 1 addition & 1 deletion Tests/StatsigTests/PerformanceSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class PerformanceSpec: XCTestCase {

override func setUpWithError() throws {
let opts = StatsigOptions(disableDiagnostics: true)
opts.overrideURL = URL(string: "http://PerformanceSpec")
opts.mainApiUrl = URL(string: "http://PerformanceSpec")

_ = TestUtils.startWithResponseAndWait([
"feature_gates": [
Expand Down
4 changes: 2 additions & 2 deletions Tests/StatsigTests/StatsigSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class StatsigSpec: BaseSpec {
autoValueUpdateIntervalSec: 0.1,
disableDiagnostics: true
)
opts.overrideURL = URL(string: "http://api.statsig.enableAutoValueUpdateTest")
opts.mainApiUrl = URL(string: "http://api.statsig.enableAutoValueUpdateTest")
Statsig.start(sdkKey: "client-api-key", options: opts)

// first request, "lastSyncTimeForUser" field should not be present in the request body
Expand All @@ -114,7 +114,7 @@ class StatsigSpec: BaseSpec {
autoValueUpdateIntervalSec: 0.1,
disableDiagnostics: true
)
opts.overrideURL = URL(string: "http://StatsigSpec.enableAutoValueUpdateEQtrue")
opts.mainApiUrl = URL(string: "http://StatsigSpec.enableAutoValueUpdateEQtrue")

var requestExpectation = self.expectation(description: "Request Made Once")
stub(condition: isHost("StatsigSpec.enableAutoValueUpdateEQtrue")) { request in
Expand Down
4 changes: 2 additions & 2 deletions Tests/StatsigTests/TestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TestUtils {

static func startWithResponseAndWait(_ response: [String: Any], _ key: String = "client-api-key", _ user: StatsigUser? = nil, _ statusCode: Int32 = 200, options: StatsigOptions? = nil) -> URLRequest? {
var result: URLRequest? = nil
let host = options?.overrideURL?.host ?? "api.statsig.com"
let host = options?.mainApiUrl?.host ?? "api.statsig.com"
let handle = stub(condition: isHost(host)) { req in
result = req
return HTTPStubsResponse(jsonObject: response, statusCode: statusCode, headers: nil)
Expand All @@ -68,7 +68,7 @@ class TestUtils {

static func startWithStatusAndWait(_ statusCode: Int32 = 200, _ key: String = "client-api-key", _ user: StatsigUser? = nil, options: StatsigOptions? = nil) -> URLRequest? {
var result: URLRequest? = nil
stub(condition: isHost(options?.overrideURL?.host ?? "api.statsig.com")) { req in
stub(condition: isHost(options?.mainApiUrl?.host ?? "api.statsig.com")) { req in
result = req
return HTTPStubsResponse(data: Data(), statusCode: statusCode, headers: nil)
}
Expand Down

0 comments on commit e8a36b8

Please sign in to comment.