-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #240 from emeraldpay/feat/additional-ws-metrics
- Loading branch information
Showing
10 changed files
with
169 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/ConnectionTimeMonitoring.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Copyright (c) 2023 EmeraldPay, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.emeraldpay.dshackle.upstream.ethereum | ||
|
||
import io.emeraldpay.dshackle.upstream.rpcclient.RpcMetrics | ||
import java.time.Duration | ||
import java.time.Instant | ||
import java.util.concurrent.atomic.AtomicReference | ||
import java.util.function.Consumer | ||
|
||
/** | ||
* A consumer for a connection events that records the connection time metrics | ||
*/ | ||
class ConnectionTimeMonitoring( | ||
val metrics: RpcMetrics, | ||
) : Consumer<WsConnection.ConnectionStatus> { | ||
|
||
private val connectedAt = AtomicReference<Instant?>(null) | ||
|
||
val connectionTime: Duration? | ||
get() = connectedAt.get()?.let { | ||
Duration.between(it, Instant.now()).coerceAtLeast(Duration.ofMillis(0)) | ||
} | ||
|
||
override fun accept(t: WsConnection.ConnectionStatus) { | ||
if (t == WsConnection.ConnectionStatus.CONNECTED) { | ||
connectedAt.set(Instant.now()) | ||
} else if (t == WsConnection.ConnectionStatus.DISCONNECTED) { | ||
connectionTime?.let(metrics::recordConnectionTime) | ||
connectedAt.set(null) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/test/kotlin/io/emeraldpay/dshackle/upstream/ethereum/ConnectionTimeMonitoringTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Copyright (c) 2023 EmeraldPay, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.emeraldpay.dshackle.upstream.ethereum | ||
|
||
import io.emeraldpay.dshackle.upstream.rpcclient.RpcMetrics | ||
import io.kotest.core.spec.style.ShouldSpec | ||
import io.kotest.matchers.comparables.shouldBeGreaterThan | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.shouldNotBe | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import java.time.Duration | ||
|
||
class ConnectionTimeMonitoringTest : ShouldSpec({ | ||
|
||
should("Update time on CONNECT") { | ||
val monitoring = ConnectionTimeMonitoring(mockk()) | ||
|
||
monitoring.connectionTime shouldBe null | ||
monitoring.accept(WsConnection.ConnectionStatus.CONNECTED) | ||
Thread.sleep(25) | ||
monitoring.connectionTime shouldNotBe null | ||
monitoring.connectionTime!! shouldBeGreaterThan Duration.ofMillis(20) | ||
} | ||
|
||
should("Record time on DISCONNECT") { | ||
val rpc = mockk<RpcMetrics>(relaxed = true) | ||
val monitoring = ConnectionTimeMonitoring(rpc) | ||
|
||
monitoring.accept(WsConnection.ConnectionStatus.CONNECTED) | ||
Thread.sleep(25) | ||
monitoring.accept(WsConnection.ConnectionStatus.DISCONNECTED) | ||
|
||
// erases the time after recording it | ||
monitoring.connectionTime shouldBe null | ||
|
||
verify { | ||
rpc.recordConnectionTime(more(Duration.ofMillis(20))) | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters