Skip to content

Commit

Permalink
record trusted native frame lag by domain
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Jan 20, 2024
1 parent 89f5d72 commit 9478048
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/main/scala/Lag.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions src/main/scala/Monitor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/actor/RoundClientActor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
5 changes: 4 additions & 1 deletion src/main/scala/util/RequestHeader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down Expand Up @@ -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"

0 comments on commit 9478048

Please sign in to comment.