Skip to content

Commit

Permalink
refactor: make correlationId accessible from logger
Browse files Browse the repository at this point in the history
  • Loading branch information
precious-ossai-cko committed Mar 26, 2024
1 parent 7e057a8 commit e0c6461
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import com.squareup.moshi.Moshi
import java.util.UUID

public object CheckoutApiServiceFactory {
private val correlationId = UUID.randomUUID().toString()
private lateinit var correlationId: String

@JvmStatic
public fun create(
Expand All @@ -42,7 +42,8 @@ public object CheckoutApiServiceFactory {
): CheckoutApiService {
val logger = EventLoggerProvider.provide()

logger.setup(context, environment, correlationId = correlationId)
logger.setup(context, environment)
correlationId = logger.correlationId

return CheckoutApiClient(
provideTokenRepository(context, publicKey, environment),
Expand Down
5 changes: 2 additions & 3 deletions checkout/src/main/java/com/checkout/logging/EventLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ internal class EventLogger(private val logger: CheckoutEventLogger) : Logger<Log
var needToSetup = true

@VisibleForTesting
var correlationId: String = UUID.randomUUID().toString()
override lateinit var correlationId: String

override fun setup(
context: Context,
environment: Environment,
correlationId: String,
identifier: String,
version: String,
) {
this.correlationId = correlationId
if (needToSetup) {
logger.enableRemoteProcessor(
environment.toLoggingEnvironment(),
Expand All @@ -40,6 +38,7 @@ internal class EventLogger(private val logger: CheckoutEventLogger) : Logger<Log
}

override fun resetSession() {
correlationId = UUID.randomUUID().toString()
logger.addMetadata(METADATA_CORRELATION_ID, correlationId)
sentLogs.clear()
}
Expand Down
7 changes: 5 additions & 2 deletions checkout/src/main/java/com/checkout/logging/Logger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ public interface Logger<T> {
* @param environment - [Environment] type.
* @param identifier - [String] library logging identifier, used as a prefix for an event type.
* @param version - [String] library version.
* @param correlationId - [String] correlationId for metadata logs
*/
public fun setup(
context: Context,
environment: Environment,
correlationId: String,
identifier: String = BuildConfig.PRODUCT_IDENTIFIER,
version: String = BuildConfig.PRODUCT_VERSION,
)

/**
* correlationId for metadata logs
*/
public var correlationId: String

/**
* Resets logger session.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ internal class CheckoutApiServiceFactoryTest {
// Given
val mockContext = mockk<Context>()
val mockEnvironment = Environment.SANDBOX
val mockCorrelationId = "testCorrelationId"

// When
CheckoutApiServiceFactory.create("", mockEnvironment, mockContext)

// Then
verify { mockLogger.setup(eq(mockContext), eq(mockEnvironment), eq(mockCorrelationId)) }
verify { mockLogger.setup(eq(mockContext), eq(mockEnvironment)) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ internal class EventLoggerTest {
// Given
val mockContext: Context = mockk(relaxed = true)
val mockEnvironment = Environment.SANDBOX
val mockCorrelationId = "testCorrelationId"
val expectedMetadata =
RemoteProcessorMetadata.from(
mockContext,
Expand All @@ -49,7 +48,7 @@ internal class EventLoggerTest {
)

// When
eventLogger.setup(mockContext, mockEnvironment, mockCorrelationId)
eventLogger.setup(mockContext, mockEnvironment)

// Then
verify {
Expand All @@ -65,10 +64,9 @@ internal class EventLoggerTest {
// Given
val mockContext: Context = mockk(relaxed = true)
val mockEnvironment = Environment.PRODUCTION
val mockCorrelationId = "testCorrelationId"

// When
eventLogger.setup(mockContext, mockEnvironment, mockCorrelationId)
eventLogger.setup(mockContext, mockEnvironment)

// Then
verify { eventLogger.resetSession() }
Expand All @@ -79,11 +77,10 @@ internal class EventLoggerTest {
// Given
val mockContext: Context = mockk(relaxed = true)
val mockEnvironment = Environment.SANDBOX
val mockCorrelationId = "testCorrelationId"
(eventLogger as? EventLogger)?.needToSetup = false

// When
eventLogger.setup(mockContext, mockEnvironment, mockCorrelationId)
eventLogger.setup(mockContext, mockEnvironment)

// Then
verify(exactly = 0) { eventLogger.resetSession() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import com.checkout.threedsecure.ThreeDSExecutor
import com.checkout.threedsecure.logging.ThreeDSEventLogger
import com.checkout.threedsecure.model.ThreeDSRequest
import com.checkout.threedsecure.usecase.ProcessThreeDSUseCase
import java.util.UUID

public class PaymentFormMediator(
private val config: PaymentFormConfig,
Expand All @@ -25,9 +24,8 @@ public class PaymentFormMediator(
* 3DS executor which can be used to execute and handle 3DS flow with WebView.
*/
private val threeDSExecutor: Executor<ThreeDSRequest> by lazy(LazyThreadSafetyMode.NONE) {
val correlationId = UUID.randomUUID().toString()
val logger = EventLoggerProvider.provide().apply {
setup(config.context, config.environment, correlationId = correlationId)
setup(config.context, config.environment)
}
ThreeDSExecutor(ProcessThreeDSUseCase(), ThreeDSEventLogger(logger))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import com.checkout.frames.usecase.CardTokenizationUseCase
import com.checkout.frames.usecase.ClosePaymentFlowUseCase
import com.checkout.frames.utils.extensions.logEvent
import com.checkout.logging.EventLoggerProvider
import java.util.UUID

internal class FramesInjector(private val component: FramesDIComponent) : Injector {

Expand Down Expand Up @@ -59,15 +58,8 @@ internal class FramesInjector(private val component: FramesDIComponent) : Inject
supportedCardSchemeList: List<CardScheme> = emptyList(),
prefillData: PrefillData? = null,
): Injector {
val correlationId = UUID.randomUUID().toString()
val logger = EventLoggerProvider.provide().apply {
setup(
context,
environment,
correlationId = correlationId,
BuildConfig.LOGGING_IDENTIFIER,
BuildConfig.PRODUCT_VERSION,
)
setup(context, environment, BuildConfig.LOGGING_IDENTIFIER, BuildConfig.PRODUCT_VERSION)
logEvent(PaymentFormEventType.INITIALISED)
}
val closePaymentFlowUseCase = ClosePaymentFlowUseCase(paymentFlowHandler::onBackPressed)
Expand Down

0 comments on commit e0c6461

Please sign in to comment.