Skip to content

Commit

Permalink
Add metric for counting number of sessions stored (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
akolosov-n authored Aug 21, 2024
1 parent 823b4b1 commit f8aa937
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class DtlsServerMetricsCallbacks(
private val sessionsClosed = registry.counter("$metricsPrefix.sessions.closed")
private val sessionsFailedBuilder = Counter.builder("$metricsPrefix.sessions.failed")
private val sessionsExpired = registry.counter("$metricsPrefix.sessions.expired")
private val sessionsStored = registry.counter("$metricsPrefix.sessions.stored")
private val sessionsReloaded = registry.counter("$metricsPrefix.sessions.reloaded")
private val messagesDropped = registry.counter("$metricsPrefix.messages.dropped")

Expand Down Expand Up @@ -79,6 +80,8 @@ class DtlsServerMetricsCallbacks(
sessionsClosed.increment()
DtlsSessionLifecycleCallbacks.Reason.EXPIRED ->
sessionsExpired.increment()
DtlsSessionLifecycleCallbacks.Reason.STORED ->
sessionsStored.increment()
else -> {}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ class DtlsServer(
}

fun closeSession(addr: InetSocketAddress) {
sessions.remove(addr)?.storeAndClose()
sessions.remove(addr)?.apply {
storeAndClose()
logger.info("[{}] [CID:{}] DTLS session was stored", peerAddress, (this as? DtlsSession)?.sessionContext?.cid?.toHex() ?: "na")
}
}

fun loadSession(sessBuf: SessionWithContext?, adr: InetSocketAddress, cid: ByteArray): Boolean {
Expand Down Expand Up @@ -267,11 +270,14 @@ class DtlsServer(
sessionStartTimestamp = sessionStartTimestamp
)
storeSession(ctx.ownCid, session)
reportSessionFinished(DtlsSessionLifecycleCallbacks.Reason.STORED)
} catch (ex: Exception) {
logger.error("[{}] [CID:{}] DTLS failed to store session: {}", peerAddress, ownCidHex, ex.message)
reportSessionFinished(DtlsSessionLifecycleCallbacks.Reason.FAILED, ex)
}
} else {
close()
reportSessionFinished(DtlsSessionLifecycleCallbacks.Reason.CLOSED)
}
}

Expand Down Expand Up @@ -315,7 +321,7 @@ class DtlsServer(
sessions.remove(peerAddress, this)
storeAndClose()
logger.info("[{}] [CID:{}] DTLS connection expired", peerAddress, ownCidHex)
lifecycleCallbacks.sessionFinished(peerAddress, DtlsSessionLifecycleCallbacks.Reason.EXPIRED)
reportSessionFinished(DtlsSessionLifecycleCallbacks.Reason.EXPIRED)
}

private val ownCidHex: String get() = ctx.ownCid?.toHex() ?: "na"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ interface DtlsSessionLifecycleCallbacks {
SUCCEEDED,
FAILED,
CLOSED,
EXPIRED
EXPIRED,
STORED
}

fun handshakeStarted(adr: InetSocketAddress) = Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ class DtlsServerTransportTest {
client.close()

verify {
sslLifecycleCallbacks.sessionFinished(any(), DtlsSessionLifecycleCallbacks.Reason.STORED)
sslLifecycleCallbacks.sessionFinished(any(), DtlsSessionLifecycleCallbacks.Reason.EXPIRED)
}
}
Expand Down Expand Up @@ -391,8 +392,10 @@ class DtlsServerTransportTest {
sslLifecycleCallbacks.handshakeStarted(any())
sslLifecycleCallbacks.handshakeFinished(any(), any(), any(), DtlsSessionLifecycleCallbacks.Reason.SUCCEEDED)
sslLifecycleCallbacks.sessionStarted(any(), any(), false)
sslLifecycleCallbacks.sessionFinished(any(), DtlsSessionLifecycleCallbacks.Reason.STORED)
sslLifecycleCallbacks.sessionFinished(any(), DtlsSessionLifecycleCallbacks.Reason.EXPIRED)
sslLifecycleCallbacks.sessionStarted(any(), any(), true)
sslLifecycleCallbacks.sessionFinished(any(), DtlsSessionLifecycleCallbacks.Reason.STORED)
sslLifecycleCallbacks.sessionFinished(any(), DtlsSessionLifecycleCallbacks.Reason.EXPIRED)
}

Expand Down

0 comments on commit f8aa937

Please sign in to comment.