From 9478048fcb80a83aaf010932d62bfb79af8f29b0 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Sat, 20 Jan 2024 10:31:52 +0100 Subject: [PATCH] record trusted native frame lag by domain --- src/main/scala/Lag.scala | 5 +++-- src/main/scala/Monitor.scala | 8 +++++--- src/main/scala/actor/RoundClientActor.scala | 2 +- src/main/scala/util/RequestHeader.scala | 5 ++++- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/scala/Lag.scala b/src/main/scala/Lag.scala index c2c7a6cd..97be8a9d 100644 --- a/src/main/scala/Lag.scala +++ b/src/main/scala/Lag.scala @@ -2,6 +2,7 @@ package lila.ws import com.github.blemale.scaffeine.{ Cache, Scaffeine } import lila.ws.ipc.LilaIn +import lila.ws.util.Domain final class Lag(lilaRedis: Lila, groupedWithin: util.GroupedWithin): @@ -19,8 +20,8 @@ final class Lag(lilaRedis: Lila, groupedWithin: util.GroupedWithin): export clientReports.apply as recordClientLag - def recordTrustedLag(millis: Int, userId: Option[User.Id]) = - Monitor.lag.roundFrameLag(millis) + def recordTrustedLag(millis: Int, userId: Option[User.Id], domain: Domain) = + Monitor.lag.roundFrameLag(millis, domain) userId.foreach: uid => trustedStats.put( uid, diff --git a/src/main/scala/Monitor.scala b/src/main/scala/Monitor.scala index 51c704f6..6b1891d4 100644 --- a/src/main/scala/Monitor.scala +++ b/src/main/scala/Monitor.scala @@ -5,6 +5,7 @@ import com.typesafe.scalalogging.Logger import kamon.Kamon import kamon.tag.TagSet import java.util.concurrent.TimeUnit +import lila.ws.util.Domain final class Monitor( config: Config, @@ -131,10 +132,11 @@ object Monitor: object lag: - private val frameLagHistogram = Kamon.timer("round.lag.frame").withoutTags() + private val frameLagHistogram = Kamon.timer("round.lag.frame") - def roundFrameLag(millis: Int) = - if millis > 1 && millis < 99999 then frameLagHistogram.record(millis.toLong, TimeUnit.MILLISECONDS) + def roundFrameLag(millis: Int, domain: Domain) = + if millis > 1 && millis < 99999 then + frameLagHistogram.withTag("domain", domain.value).record(millis.toLong, TimeUnit.MILLISECONDS) object mobile: private val Regex = """Lichess Mobile/(\S+)(?: \(\d*\))? as:(\S+) sri:\S+ os:(Android|iOS)/.*""".r diff --git a/src/main/scala/actor/RoundClientActor.scala b/src/main/scala/actor/RoundClientActor.scala index 20a927a7..e9b50fe7 100644 --- a/src/main/scala/actor/RoundClientActor.scala +++ b/src/main/scala/actor/RoundClientActor.scala @@ -72,7 +72,7 @@ object RoundClientActor: msg match case ClientOut.RoundPongFrame(lagMillis) => - services.lag.recordTrustedLag(lagMillis, req.user) + services.lag.recordTrustedLag(lagMillis, req.user, req.domain) Behaviors.same case ClientCtrl.Broom(oldSeconds) => diff --git a/src/main/scala/util/RequestHeader.scala b/src/main/scala/util/RequestHeader.scala index 51b99bde..04381ea2 100644 --- a/src/main/scala/util/RequestHeader.scala +++ b/src/main/scala/util/RequestHeader.scala @@ -9,6 +9,9 @@ import ornicar.scalalib.zeros.given opaque type RequestUri = String object RequestUri extends OpaqueString[RequestUri] +opaque type Domain = String +object Domain extends OpaqueString[Domain] + final class RequestHeader(val uri: RequestUri, headers: HttpHeaders): val (path, parameters) = @@ -50,6 +53,6 @@ final class RequestHeader(val uri: RequestUri, headers: HttpHeaders): def name: String = s"$uri UA: $userAgent" - def host: String = header(HttpHeaderNames.HOST) getOrElse "?" + def domain = Domain(header(HttpHeaderNames.HOST) getOrElse "?") override def toString = s"$name origin: $origin"