<commit_before>package lila

import com.github.benmanes.caffeine.cache.{ Cache => CaffeineCache }
import kamon.tag.TagSet
import kamon.metric.{ Counter, Timer }

import lila.common.ApiVersion

object mon {

  object http {
    private val t = timer("http.time")
    def time(action: String, client: String, method: String, code: Int) =
      t.withTags(
        Map(
          "action" -> action,
          "client" -> client,
          "method" -> method,
          "code"   -> code.toLong
        )
      )
    def error(action: String, client: String, method: String, code: Int) =
      counter("http.error").withTags(
        Map(
          "action" -> action,
          "client" -> client,
          "method" -> method,
          "code"   -> code.toLong
        )
      )
    def path(p: String) = counter("http.path.count").withTag("path", p)
    val userGamesCost   = counter("http.userGames.cost").withoutTags
    def csrfError(tpe: String, action: String, client: String) =
      counter("http.csrf.error").withTags(Map("type" -> tpe, "action" -> action, "client" -> client))
    val fingerPrint          = timer("http.fingerPrint.time").withoutTags
    def jsmon(event: String) = counter("http.jsmon").withTag("event", event)
    val imageBytes           = histogram("http.image.bytes").withoutTags
  }
  object syncache {
    def miss(name: String)    = counter("syncache.miss").withTag("name", name)
    def timeout(name: String) = counter("syncache.timeout").withTag("name", name)
    def compute(name: String) = timer("syncache.compute").withTag("name", name)
    def wait(name: String)    = timer("syncache.wait").withTag("name", name)
  }
  def caffeineStats(cache: CaffeineCache[_, _], name: String): Unit = {
    val stats = cache.stats
    gauge("caffeine.request").withTags(Map("name" -> name, "hit" -> true)).update(stats.hitCount)
    gauge("caffeine.request").withTags(Map("name" -> name, "hit" -> false)).update(stats.missCount)
    histogram("caffeine.hit.rate").withTag("name", name).record((stats.hitRate * 100000).toLong)
    if (stats.totalLoadTime > 0) {
      gauge("caffeine.load.count")
        .withTags(Map("name" -> name, "success" -> "success"))
        .update(stats.loadSuccessCount)
      gauge("caffeine.load.count")
        .withTags(Map("name" -> name, "success" -> "failure"))
        .update(stats.loadFailureCount)
      gauge("caffeine.loadTime.cumulated")
        .withTag("name", name)
        .update(stats.totalLoadTime / 1000000) // in millis; too much nanos for Kamon to handle)
      timer("caffeine.loadTime.penalty").withTag("name", name).record(stats.averageLoadPenalty.toLong)
    }
    gauge("caffeine.eviction.count").withTag("name", name).update(stats.evictionCount)
    gauge("caffeine.entry.count").withTag("name", name).update(cache.estimatedSize)
  }
  object evalCache {
    private val r = counter("evalCache.request")
    def request(ply: Int, isHit: Boolean) =
      r.withTags(Map("ply" -> (if (ply < 13) ply.toString else "13+"), "hit" -> isHit))
    object upgrade {
      val count     = counter("evalCache.upgrade.count").withoutTags
      val members   = gauge("evalCache.upgrade.members").withoutTags
      val evals     = gauge("evalCache.upgrade.evals").withoutTags
      val expirable = gauge("evalCache.upgrade.expirable").withoutTags
    }
  }<commit_msg>tweak eval cache monitoring<commit_after>package lila

import com.github.benmanes.caffeine.cache.{ Cache => CaffeineCache }
import kamon.tag.TagSet
import kamon.metric.{ Counter, Timer }

import lila.common.ApiVersion

object mon {

  object http {
    private val t = timer("http.time")
    def time(action: String, client: String, method: String, code: Int) =
      t.withTags(
        Map(
          "action" -> action,
          "client" -> client,
          "method" -> method,
          "code"   -> code.toLong
        )
      )
    def error(action: String, client: String, method: String, code: Int) =
      counter("http.error").withTags(
        Map(
          "action" -> action,
          "client" -> client,
          "method" -> method,
          "code"   -> code.toLong
        )
      )
    def path(p: String) = counter("http.path.count").withTag("path", p)
    val userGamesCost   = counter("http.userGames.cost").withoutTags
    def csrfError(tpe: String, action: String, client: String) =
      counter("http.csrf.error").withTags(Map("type" -> tpe, "action" -> action, "client" -> client))
    val fingerPrint          = timer("http.fingerPrint.time").withoutTags
    def jsmon(event: String) = counter("http.jsmon").withTag("event", event)
    val imageBytes           = histogram("http.image.bytes").withoutTags
  }
  object syncache {
    def miss(name: String)    = counter("syncache.miss").withTag("name", name)
    def timeout(name: String) = counter("syncache.timeout").withTag("name", name)
    def compute(name: String) = timer("syncache.compute").withTag("name", name)
    def wait(name: String)    = timer("syncache.wait").withTag("name", name)
  }
  def caffeineStats(cache: CaffeineCache[_, _], name: String): Unit = {
    val stats = cache.stats
    gauge("caffeine.request").withTags(Map("name" -> name, "hit" -> true)).update(stats.hitCount)
    gauge("caffeine.request").withTags(Map("name" -> name, "hit" -> false)).update(stats.missCount)
    histogram("caffeine.hit.rate").withTag("name", name).record((stats.hitRate * 100000).toLong)
    if (stats.totalLoadTime > 0) {
      gauge("caffeine.load.count")
        .withTags(Map("name" -> name, "success" -> "success"))
        .update(stats.loadSuccessCount)
      gauge("caffeine.load.count")
        .withTags(Map("name" -> name, "success" -> "failure"))
        .update(stats.loadFailureCount)
      gauge("caffeine.loadTime.cumulated")
        .withTag("name", name)
        .update(stats.totalLoadTime / 1000000) // in millis; too much nanos for Kamon to handle)
      timer("caffeine.loadTime.penalty").withTag("name", name).record(stats.averageLoadPenalty.toLong)
    }
    gauge("caffeine.eviction.count").withTag("name", name).update(stats.evictionCount)
    gauge("caffeine.entry.count").withTag("name", name).update(cache.estimatedSize)
  }
  object evalCache {
    private val r = counter("evalCache.request")
    def request(ply: Int, isHit: Boolean) =
      r.withTags(Map("ply" -> (if (ply < 15) ply.toString else "15+"), "hit" -> isHit))
    object upgrade {
      val count     = counter("evalCache.upgrade.count").withoutTags
      val members   = gauge("evalCache.upgrade.members").withoutTags
      val evals     = gauge("evalCache.upgrade.evals").withoutTags
      val expirable = gauge("evalCache.upgrade.expirable").withoutTags
    }
  }
{'fraction_non_alphanumeric': 0.15973676338618006, 'fraction_numerical': 0.0053843852826802275, 'mean_word_length': 3.153416149068323, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1862885', 'n_tokens_mistral': 2143, 'n_tokens_neox': 2077, 'n_words': 409}