Skip to content

Commit

Permalink
chore: add more gravsearch metrics (#2666)
Browse files Browse the repository at this point in the history
  • Loading branch information
BalduinLandolt committed May 16, 2023
1 parent 423b352 commit 873eb46
Showing 1 changed file with 17 additions and 14 deletions.
Expand Up @@ -11,6 +11,8 @@ import akka.http.scaladsl.server.Route
import zio._
import zio.metrics._

import java.time.temporal.ChronoUnit

import dsp.errors.BadRequestException
import org.knora.webapi._
import org.knora.webapi.config.AppConfig
Expand Down Expand Up @@ -238,27 +240,28 @@ final case class SearchRouteV2(searchValueMinLength: Int)(
post(entity(as[String])(query => requestContext => gravsearch(query, requestContext)))
}

private val gravsearchDuration = Metric.timer("gravsearch", ChronoUnit.MILLIS, Chunk.iterate(1.0, 17)(_ * 2))
private val gravsearchDurationSummary =
Metric.summary("gravsearch_summary", 1.day, 100, 0.03d, Chunk(0.01, 0.1, 0.2, 0.5, 0.8, 0.9, 0.99))
private val gravsearchFailCounter = Metric.counter("gravsearch_fail").fromConst(1)
private val gravsearchTimeoutCounter = Metric.counter("gravsearch_timeout").fromConst(1)

private def gravsearch(query: String, requestContext: RequestContext) = {
val start = java.lang.System.currentTimeMillis().toDouble
val durationMetric =
Metric.histogram("gravsearch", MetricKeyType.Histogram.Boundaries.fromChunk(Chunk(0, 10, 100, 1000, 5000, 20000)))
val failConter = Metric.counter("gravsearch_fail").fromConst(1)
val timeoutConter = Metric.counter("gravsearch_timeout").fromConst(1)
val constructQuery = GravsearchParser.parseQuery(query)
val targetSchemaTask = RouteUtilV2.getOntologySchema(requestContext)
val schemaOptionsTask = RouteUtilV2.getSchemaOptions(requestContext)
val request = for {
val task = for {
start <- Clock.instant.map(_.toEpochMilli).map(_.toDouble)
targetSchema <- targetSchemaTask
requestingUser <- Authenticator.getUserADM(requestContext)
schemaOptions <- schemaOptionsTask
} yield GravsearchRequestV2(constructQuery, targetSchema, schemaOptions, requestingUser)
val task = request
.flatMap(request => MessageRelay.ask[KnoraResponseV2](request))
.tapError {
case _: TriplestoreTimeoutException => ZIO.unit @@ timeoutConter
case _ => ZIO.unit @@ failConter
}
.tap(_ => Clock.instant.map(_.toEpochMilli).map(_.-(start)).debug @@ durationMetric)
request = GravsearchRequestV2(constructQuery, targetSchema, schemaOptions, requestingUser)
response <- MessageRelay.ask[KnoraResponseV2](request).tapError {
case _: TriplestoreTimeoutException => ZIO.unit @@ gravsearchTimeoutCounter
case _ => ZIO.unit @@ gravsearchFailCounter
} @@ gravsearchDuration.trackDuration
_ <- Clock.instant.map(_.toEpochMilli).map(_.-(start)) @@ gravsearchDurationSummary
} yield response
RouteUtilV2.completeResponse(task, requestContext, targetSchemaTask, schemaOptionsTask.map(Some(_)))
}

Expand Down

0 comments on commit 873eb46

Please sign in to comment.