Skip to content

Commit

Permalink
public api: rename configure method to start (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
Augustyniak authored Oct 2, 2024
1 parent 556142b commit 61e94c4
Show file tree
Hide file tree
Showing 28 changed files with 130 additions and 128 deletions.
2 changes: 1 addition & 1 deletion examples/android/HelloWorldApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class HelloWorldApp : Application() {

val userID = UUID.randomUUID().toString();

Logger.configure(
Logger.start(
apiKey = bitdriftAPIKey,
apiUrl = BITDRIFT_URL,
sessionStrategy = SessionStrategy.Fixed { UUID.randomUUID().toString() },
Expand Down
2 changes: 1 addition & 1 deletion examples/objective-c/CAPViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ - (void)setSelectedLogLevel:(LogLevel)selectedLogLevel {

- (void)setUpLogger {
[CAPLogger
configureWithAPIKey:kCaptureAPIKey
startWithAPIKey:kCaptureAPIKey
sessionStrategy:[CAPSessionStrategy fixed]
apiURL:[NSURL URLWithString:kCaptureURLString]
];
Expand Down
2 changes: 1 addition & 1 deletion examples/swift/hello_world/LoggerCustomer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ final class LoggerCustomer: NSObject, URLSessionDelegate {
self.appStartTime = Date()

Logger
.configure(
.start(
withAPIKey: kBitdriftAPIKey,
sessionStrategy: .fixed(),
configuration: .init(),
Expand Down
68 changes: 35 additions & 33 deletions platform/jvm/capture/src/main/kotlin/io/bitdrift/capture/Capture.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,43 @@ import kotlin.time.Duration

internal sealed class LoggerState {
/**
* The logger has not yet been configured.
* The logger has not yet been started.
*/
data object NotConfigured : LoggerState()
data object NotStarted : LoggerState()

/**
* The logger has been successfully configured and is ready for use. Subsequent attempts to configure the logger will be ignored.
* The logger is in the process of being started. Subsequent attempts to start the logger will be ignored.
*/
class Configured(val logger: LoggerImpl) : LoggerState()
data object Starting : LoggerState()

/**
* The configuration has started but is not yet complete. Subsequent attempts to configure the logger will be ignored.
* The logger has been successfully started and is ready for use. Subsequent attempts to start the logger will be ignored.
*/
data object ConfigurationStarted : LoggerState()
class Started(val logger: LoggerImpl) : LoggerState()

/**
* The configuration was attempted but failed. Subsequent attempts to configure the logger will be ignored.
* An attempt to start the logger was made but failed. Subsequent attempts to start the logger will be ignored.
*/
data object ConfigurationFailure : LoggerState()
data object StartFailure : LoggerState()
}

/**
* Top level namespace Capture SDK.
*/
object Capture {
private val default: AtomicReference<LoggerState> = AtomicReference(LoggerState.NotConfigured)
private val default: AtomicReference<LoggerState> = AtomicReference(LoggerState.NotStarted)

/**
* Returns a handle to the underlying logger instance, if Capture has been configured.
* Returns a handle to the underlying logger instance, if Capture has been started.
*
* @return ILogger a logger handle
*/
fun logger(): ILogger? {
return when (val state = default.get()) {
is LoggerState.NotConfigured -> null
is LoggerState.Configured -> state.logger
is LoggerState.ConfigurationStarted -> null
is LoggerState.ConfigurationFailure -> null
is LoggerState.NotStarted -> null
is LoggerState.Starting -> null
is LoggerState.Started -> state.logger
is LoggerState.StartFailure -> null
}
}

Expand All @@ -69,7 +69,7 @@ object Capture {
*
* ## Logger Initialization
*
* To initialize the logger, first call Logger.configure() with the desired configuration. Once this
* To initialize the logger, first call Logger.start() with the desired configuration. Once this
* call completes, logging can be done either via the top level logging functions or via a logger
* handle acquired via Capture.logger().
*
Expand All @@ -88,7 +88,9 @@ object Capture {
private val mainThreadHandler by lazy { MainThreadHandler() }

/**
* Configures the static logger.
* Initializes the Capture SDK with the specified API key, providers, and configuration.
* Calling other SDK methods has no effect unless the logger has been initialized.
* Subsequent calls to this function will have no effect.
*
* @param apiKey The API key provided by bitdrift. This is required.
* @param sessionStrategy session strategy for the management of session id.
Expand All @@ -102,15 +104,15 @@ object Capture {
@Synchronized
@JvmStatic
@JvmOverloads
fun configure(
fun start(
apiKey: String,
sessionStrategy: SessionStrategy,
configuration: Configuration = Configuration(),
fieldProviders: List<FieldProvider> = listOf(),
dateProvider: DateProvider? = null,
apiUrl: HttpUrl = defaultCaptureApiUrl,
) {
configure(
start(
apiKey,
sessionStrategy,
configuration,
Expand All @@ -124,7 +126,7 @@ object Capture {
@Synchronized
@JvmStatic
@JvmOverloads
internal fun configure(
internal fun start(
apiKey: String,
sessionStrategy: SessionStrategy,
configuration: Configuration = Configuration(),
Expand All @@ -147,7 +149,7 @@ object Capture {
}

// Ideally we would use `getAndUpdate` in here but it's available for API 24 and up only.
if (default.compareAndSet(LoggerState.NotConfigured, LoggerState.ConfigurationStarted)) {
if (default.compareAndSet(LoggerState.NotStarted, LoggerState.Starting)) {
try {
val logger = LoggerImpl(
apiKey = apiKey,
Expand All @@ -158,27 +160,27 @@ object Capture {
sessionStrategy = sessionStrategy,
bridge = bridge,
)
default.set(LoggerState.Configured(logger))
default.set(LoggerState.Started(logger))
} catch (e: Throwable) {
Log.w("capture", "Capture initialization failed", e)
default.set(LoggerState.ConfigurationFailure)
Log.w("capture", "Failed to start Capture", e)
default.set(LoggerState.StartFailure)
}
} else {
Log.w("capture", "Attempted to initialize Capture more than once")
Log.w("capture", "Multiple attempts to start Capture")
}
}

/**
* The Id for the current ongoing session.
* It's equal to `null` prior to the configuration of Capture SDK.
* It's equal to `null` prior to the start of Capture SDK.
*/
@JvmStatic
val sessionId: String?
get() = logger()?.sessionId

/**
* The URL for the current ongoing session.
* It's equal to `null` prior to the configuration of Capture SDK.
* It's equal to `null` prior to the start of Capture SDK.
*/
@JvmStatic
val sessionUrl: String?
Expand All @@ -189,15 +191,15 @@ object Capture {
* is not reinstalled.
*
* The value of this property is different for apps from the same vendor running on
* the same device. It is equal to null prior to the configuration of bitdrift Capture SDK.
* the same device. It is equal to null prior to the start of bitdrift Capture SDK.
*/
@JvmStatic
val deviceId: String?
get() = logger()?.deviceId

/**
* Defines the initialization of a new session within the current configured logger
* If no logger is configured, this is a no-op.
* Defines the initialization of a new session within the currently running logger
* If no logger is started, this is a no-op.
*/
@JvmStatic
fun startNewSession() {
Expand All @@ -219,7 +221,7 @@ object Capture {
mainThreadHandler.run { completion(it) }
}
} ?: run {
mainThreadHandler.run { completion(Err(SdkNotConfiguredError)) }
mainThreadHandler.run { completion(Err(SdkNotStartedError)) }
}
}

Expand Down Expand Up @@ -334,7 +336,7 @@ object Capture {
}

/**
* Writes an app launch TTI log event. This event should be logged only once per Logger configuration.
* Writes an app launch TTI log event. This event should be logged only once per Logger start.
* Consecutive calls have no effect.
*
* @param duration The time between a user's intent to launch the app and when the app becomes
Expand All @@ -351,7 +353,7 @@ object Capture {
* @param name the name of the operation.
* @param level the severity of the log.
* @fields additional fields to include in the log.
* @return a [Span] object that can be used to signal the end of the operation if Capture has been configured.
* @return a [Span] object that can be used to signal the end of the operation if Capture has been started.
*/
@JvmStatic
fun startSpan(name: String, level: LogLevel, fields: Map<String, String>? = null): Span? {
Expand Down Expand Up @@ -405,7 +407,7 @@ object Capture {
* Used for testing purposes.
*/
internal fun resetShared() {
default.set(LoggerState.NotConfigured)
default.set(LoggerState.NotStarted)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,13 @@ internal object CaptureJniLibrary : IBridge {
)

/**
* Writes an SDK configured log.
* Writes an SDK started log.
*
* @param loggerId the ID of the logger to write to.
* @param fields the fields to include with the log.
* @param durationMs the duration of time the SDK configuration took.
*/
external fun writeSDKConfiguredLog(
external fun writeSDKStartLog(
loggerId: Long,
fields: Map<String, FieldValue>,
duration: Double,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ internal class LoggerImpl(
this.replayScreenLogger?.start()
}

CaptureJniLibrary.writeSDKConfiguredLog(
CaptureJniLibrary.writeSDKStartLog(
this.loggerId,
mapOf(),
duration.toDouble(DurationUnit.SECONDS),
Expand Down Expand Up @@ -274,7 +274,7 @@ internal class LoggerImpl(
* `SharedPreferences`.
*/
deviceCodeService.createTemporaryDeviceCode(deviceId, completion)
} ?: completion(Err(SdkNotConfiguredError))
} ?: completion(Err(SdkNotStartedError))
}

private fun appExitSaveCurrentSessionId(sessionId: String? = null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ sealed class ApiError(override val message: String) : Error(message) {
}

/**
* Represents a failed operation due to the SDK not being configured.
* Represents a failed operation due to the SDK not being started
*/
data object SdkNotConfiguredError : Error("SDK not configured")
data object SdkNotStartedError : Error("SDK not started")
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CaptureTest {
fun aConfigureSkipsLoggerCreationWhenContextNotInitialized() {
assertThat(Capture.logger()).isNull()

Logger.configure(
Logger.start(
apiKey = "test1",
sessionStrategy = SessionStrategy.Fixed(),
dateProvider = null,
Expand Down Expand Up @@ -70,7 +70,7 @@ class CaptureTest {

assertThat(Capture.logger()).isNull()

Logger.configure(
Logger.start(
apiKey = "test1",
sessionStrategy = SessionStrategy.Fixed(),
dateProvider = null,
Expand All @@ -80,7 +80,7 @@ class CaptureTest {
assertThat(logger).isNotNull()
assertThat(Logger.deviceId).isNotNull()

Logger.configure(
Logger.start(
apiKey = "test2",
sessionStrategy = SessionStrategy.Fixed(),
dateProvider = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ConfigurationTest {
// We start without configured logger.
Assertions.assertThat(Capture.logger()).isNull()

Capture.Logger.configure(
Capture.Logger.start(
apiKey = "test1",
sessionStrategy = SessionStrategy.Fixed(),
dateProvider = null,
Expand All @@ -76,7 +76,7 @@ class ConfigurationTest {

// We perform another attempt to configure the logger to verify that
// consecutive configure calls are no-ops.
Capture.Logger.configure(
Capture.Logger.start(
apiKey = "test1",
sessionStrategy = SessionStrategy.Fixed(),
dateProvider = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public static void initBitdriftCaptureInJava() {
return fields;
});

Capture.Logger.configure(
"<YOUR API KEY GOES HERE>",
new SessionStrategy.Fixed(),
new Configuration(),
fieldProviders,
null,
HttpUrl.get("https://api.bitdrift.io")
Capture.Logger.start(
"<YOUR API KEY GOES HERE>",
new SessionStrategy.Fixed(),
new Configuration(),
fieldProviders,
null,
HttpUrl.get("https://api.bitdrift.io")
);
}
}
2 changes: 1 addition & 1 deletion platform/jvm/jni_symbols.lds
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Java_io_bitdrift_capture_CaptureJniLibrary_removeLogField
Java_io_bitdrift_capture_CaptureJniLibrary_writeLog
Java_io_bitdrift_capture_CaptureJniLibrary_writeSessionReplayLog
Java_io_bitdrift_capture_CaptureJniLibrary_writeResourceUtilizationLog
Java_io_bitdrift_capture_CaptureJniLibrary_writeSDKConfiguredLog
Java_io_bitdrift_capture_CaptureJniLibrary_writeSDKStartLog
Java_io_bitdrift_capture_CaptureJniLibrary_shouldWriteAppUpdateLog
Java_io_bitdrift_capture_CaptureJniLibrary_writeAppUpdateLog
Java_io_bitdrift_capture_CaptureJniLibrary_writeAppLaunchTTILog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ClockTimeProfiler {

@Before
fun setUp() {
Capture.Logger.configure(
Capture.Logger.start(
apiKey = "android-benchmark-test",
apiUrl = "https://api-tests.bitdrift.io".toHttpUrl(),
sessionStrategy = SessionStrategy.Fixed(),
Expand Down
2 changes: 1 addition & 1 deletion platform/jvm/src/jni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ pub extern "system" fn Java_io_bitdrift_capture_CaptureJniLibrary_writeResourceU
}

#[no_mangle]
pub extern "system" fn Java_io_bitdrift_capture_CaptureJniLibrary_writeSDKConfiguredLog(
pub extern "system" fn Java_io_bitdrift_capture_CaptureJniLibrary_writeSDKStartLog(
mut env: JNIEnv<'_>,
_class: JClass<'_>,
logger_id: jlong,
Expand Down
Loading

0 comments on commit 61e94c4

Please sign in to comment.