Skip to content

Commit

Permalink
monitor mobile connections
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Jul 18, 2023
1 parent 8cefac9 commit a2b8ff0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/main/scala/Monitor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,27 @@ object Monitor:
def roundFrameLag(millis: Int) =
if millis > 1 && millis < 99999 then frameLagHistogram.record(millis.toLong, TimeUnit.MILLISECONDS)

object mobile:
private val Regex = """Lichess Mobile/(\S+) \(\d*\) as:(\S+) sri:\S+ os:(Android|iOS)/(\S+).*""".r
private val counter = Kamon.counter("mobile.connect")
def connect(req: util.RequestHeader) = if req.isLichessMobile then
req.userAgent match
case Regex(version, user, osName, osVersion) =>
counter
.withTags(
TagSet.from(
Map(
"version" -> version,
"osName" -> osName,
"osVersion" -> osVersion,
"auth" -> (if user == "anon" then "anon" else "auth"),
"route" -> req.path.drop(1).takeWhile('/' !=)
)
)
)
.increment()
case _ => ()

def time[A](metric: Monitor.type => kamon.metric.Timer)(f: => A): A =
val timer = metric(Monitor).start()
val res = f
Expand Down
4 changes: 3 additions & 1 deletion src/main/scala/netty/RequestHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ final private class RequestHandler(router: Router)(using Executor)
):

override def channelRead0(ctx: ChannelHandlerContext, req: FullHttpRequest): Unit =
router(util.RequestHeader(RequestUri(req.uri), req.headers)) foreach:
val request = util.RequestHeader(RequestUri(req.uri), req.headers)
router(request) foreach:
case Left(status) =>
sendErrorResponse(
ctx,
Expand All @@ -35,6 +36,7 @@ final private class RequestHandler(router: Router)(using Executor)
sendErrorResponse(ctx, response)
req.release()
case Right(endpoint) =>
Monitor.mobile.connect(request)
// Forward to ProtocolHandler with endpoint attribute
ctx.channel.attr(ProtocolHandler.key.endpoint).set(endpoint)
ctx.fireChannelRead(req)
Expand Down

0 comments on commit a2b8ff0

Please sign in to comment.