Skip to content

Commit

Permalink
add validator url / network tracking in analytics (#343)
Browse files Browse the repository at this point in the history
* add validator url tracking params

* validator url access in other supervisor
  • Loading branch information
aforaleka authored May 10, 2024
1 parent efdbd7f commit 8f8ac34
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 66 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ allprojects {
}

group = "exchange.dydx.abacus"
version = "1.7.6"
version = "1.7.7"

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2366,7 +2366,7 @@ open class StateManagerAdaptor(
)
}

internal fun tracking(eventName: String, params: IMap<String, Any?>?) {
internal open fun tracking(eventName: String, params: IMap<String, Any?>? = null) {
val paramsAsString = jsonEncoder.encode(params)
ioImplementations.threading?.async(ThreadingType.main) {
ioImplementations.tracking?.log(eventName, paramsAsString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import exchange.dydx.abacus.utils.Logger
import exchange.dydx.abacus.utils.Numeric
import exchange.dydx.abacus.utils.ParsingHelper
import exchange.dydx.abacus.utils.UIImplementations
import exchange.dydx.abacus.utils.filterNotNull
import exchange.dydx.abacus.utils.iMapOf
import exchange.dydx.abacus.utils.isAddressValid
import exchange.dydx.abacus.utils.mutableMapOf
Expand Down Expand Up @@ -1532,17 +1533,9 @@ class V4StateManagerAdaptor(
}

override fun trackingParams(interval: Double): IMap<String, Any> {
val validatorUrl = this.validatorUrl
return if (validatorUrl != null) {
iMapOf(
"roundtripMs" to interval,
"validatorUrl" to validatorUrl,
)
} else {
iMapOf(
"roundtripMs" to interval,
)
}
return iMapOf(
"roundtripMs" to interval,
)
}

private fun didSetApiState(apiState: ApiState?, oldValue: ApiState?) {
Expand Down Expand Up @@ -1570,27 +1563,32 @@ class V4StateManagerAdaptor(
trackApiStateIfNeeded(apiState, null)
}

private fun apiStateParams(): IMap<String, Any>? {
val indexerTime = lastIndexerCallTime?.toEpochMilliseconds()
val validatorTime = lastValidatorCallTime?.toEpochMilliseconds()
val interval = indexerTime?.let { Clock.System.now().toEpochMilliseconds() - it }
return iMapOf(
"lastSuccessfulIndexerRPC" to indexerTime?.toDouble(),
"lastSuccessfulFullNodeRPC" to validatorTime?.toDouble(),
"elapsedTime" to interval?.toDouble(),
"blockHeight" to indexerState.blockAndTime?.block,
"nodeHeight" to validatorState.blockAndTime?.block,
"validatorUrl" to this.validatorUrl,
) as IMap<String, Any>?
}

private fun trackApiStateIfNeeded(apiState: ApiState?, oldValue: ApiState?) {
if (apiState?.abnormalState() == true || oldValue?.abnormalState() == true) {
val indexerTime = lastIndexerCallTime?.toEpochMilliseconds()?.toDouble()
val validatorTime = lastValidatorCallTime?.toEpochMilliseconds()?.toDouble()
val interval = if (indexerTime != null) {
(
Clock.System.now().toEpochMilliseconds()
.toDouble() - indexerTime
)
} else {
null
}
val params = mapOf(
"lastSuccessfulIndexerRPC" to indexerTime,
"lastSuccessfulFullNodeRPC" to validatorTime,
"elapsedTime" to interval,
"blockHeight" to indexerState.blockAndTime?.block,
"nodeHeight" to validatorState.blockAndTime?.block,
).filterValues { it != null } as Map<String, Any>

tracking(AnalyticsEvent.NetworkStatus.rawValue, params.toIMap())
tracking(AnalyticsEvent.NetworkStatus.rawValue)
}
}

override fun tracking(eventName: String, params: IMap<String, Any?>?) {
val requiredParams = apiStateParams()
val mergedParams = params?.let { ParsingHelper.merge(params.filterNotNull(), requiredParams) } ?: requiredParams
val paramsAsString = this.jsonEncoder.encode(mergedParams)
this.ioImplementations.threading?.async(ThreadingType.main) {
this.ioImplementations.tracking?.log(eventName, paramsAsString)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import exchange.dydx.abacus.state.manager.NetworkStatus
import exchange.dydx.abacus.state.model.TradingStateMachine
import exchange.dydx.abacus.state.model.updateHeight
import exchange.dydx.abacus.utils.IMap
import kollections.toIMap
import exchange.dydx.abacus.utils.ParsingHelper
import exchange.dydx.abacus.utils.iMapOf
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlin.math.max
Expand Down Expand Up @@ -245,32 +246,29 @@ internal class ConnectionStats(
updateApiState()
}

private fun apiStateParams(): IMap<String, Any>? {
val indexerTime = lastIndexerCallTime?.toEpochMilliseconds()
val validatorTime = lastValidatorCallTime?.toEpochMilliseconds()
val interval = indexerTime?.let { Clock.System.now().toEpochMilliseconds() - it }
return iMapOf(
"lastSuccessfulIndexerRPC" to indexerTime?.toDouble(),
"lastSuccessfulFullNodeRPC" to validatorTime?.toDouble(),
"elapsedTime" to interval?.toDouble(),
"blockHeight" to indexerState.blockAndTime?.block,
"nodeHeight" to validatorState.blockAndTime?.block,
"validatorUrl" to helper.validatorUrl,
) as IMap<String, Any>?
}

private fun trackApiStateIfNeeded(apiState: ApiState?, oldValue: ApiState?) {
if (apiState?.abnormalState() == true || oldValue?.abnormalState() == true) {
val indexerTime = lastIndexerCallTime?.toEpochMilliseconds()?.toDouble()
val validatorTime = lastValidatorCallTime?.toEpochMilliseconds()?.toDouble()
val interval = if (indexerTime != null) {
(
Clock.System.now().toEpochMilliseconds()
.toDouble() - indexerTime
)
} else {
null
}
val params = mapOf(
"lastSuccessfulIndexerRPC" to indexerTime,
"lastSuccessfulFullNodeRPC" to validatorTime,
"elapsedTime" to interval,
"blockHeight" to indexerState.blockAndTime?.block,
"nodeHeight" to validatorState.blockAndTime?.block,
).filterValues { it != null } as Map<String, Any>

tracking(AnalyticsEvent.NetworkStatus.rawValue, params.toIMap())
tracking(AnalyticsEvent.NetworkStatus.rawValue)
}
}

private fun tracking(eventName: String, params: IMap<String, Any>?) {
val paramsAsString = helper.jsonEncoder.encode(params)
private fun tracking(eventName: String, params: IMap<String, Any>? = null) {
val additionalParams = apiStateParams()
val paramsAsString = helper.jsonEncoder.encode(params?.let { ParsingHelper.merge(it, additionalParams) } ?: additionalParams)
helper.ioImplementations.threading?.async(ThreadingType.main) {
helper.ioImplementations.tracking?.log(eventName, paramsAsString)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ internal class ConnectionsSupervisor(
private val delegate: ConnectionDelegate,
) : NetworkSupervisor(stateMachine, helper, analyticsUtils), ConnectionStatsDelegate {
private val connectionStats = ConnectionStats(stateMachine, helper, this)
override var validatorUrl: String? = null

override var validatorUrl: String?
get() = helper.validatorUrl
set(value) {
field = value
helper.validatorUrl = value
didSetValidatorUrl(value)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class NetworkHelper(
internal var lastValidatorCallTime: Instant? = null
internal var lastIndexerCallTime: Instant? = null
internal val jsonEncoder = JsonEncoder()
internal var validatorUrl: String? = null

private var indexerRestriction: UsageRestriction? = null
set(value) {
Expand Down Expand Up @@ -530,11 +531,4 @@ class NetworkHelper(
}
}
}

private fun tracking(eventName: String, params: IMap<String, Any>?) {
val paramsAsString = jsonEncoder.encode(params)
ioImplementations.threading?.async(ThreadingType.main) {
ioImplementations.tracking?.log(eventName, paramsAsString)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import exchange.dydx.abacus.state.changes.StateChanges
import exchange.dydx.abacus.state.model.TradingStateMachine
import exchange.dydx.abacus.utils.AnalyticsUtils
import exchange.dydx.abacus.utils.IMap
import exchange.dydx.abacus.utils.ParsingHelper
import exchange.dydx.abacus.utils.filterNotNull
import exchange.dydx.abacus.utils.iMapOf
import kollections.iListOf

internal open class NetworkSupervisor(
Expand Down Expand Up @@ -82,7 +85,9 @@ internal open class NetworkSupervisor(
}

internal fun tracking(eventName: String, params: IMap<String, Any?>?) {
val paramsAsString = helper.jsonEncoder.encode(params)
val requiredParams = helper.validatorUrl?.let { iMapOf("validatorUrl" to it) } ?: iMapOf()
val mergedParams = params?.let { ParsingHelper.merge(params.filterNotNull(), requiredParams) } ?: requiredParams
val paramsAsString = helper.jsonEncoder.encode(mergedParams)
helper.ioImplementations.threading?.async(ThreadingType.main) {
helper.ioImplementations.tracking?.log(eventName, paramsAsString)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ internal class SubaccountSupervisor(
analyticsUtils: AnalyticsUtils,
private val configs: SubaccountConfigs,
private val accountAddress: String,
internal val subaccountNumber: Int
internal val subaccountNumber: Int,
) : DynamicNetworkSupervisor(stateMachine, helper, analyticsUtils) {
/*
Because faucet is done at subaccount level, we need SubaccountSupervisor even
Expand Down
2 changes: 1 addition & 1 deletion v4_abacus.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'v4_abacus'
spec.version = '1.7.6'
spec.version = '1.7.7'
spec.homepage = 'https://github.com/dydxprotocol/v4-abacus'
spec.source = { :http=> ''}
spec.authors = ''
Expand Down

0 comments on commit 8f8ac34

Please sign in to comment.