<commit_before>(ns kixi.datastore.metrics
  (:require [clojure.string :as str]
            [com.stuartsierra.component :as component]
            [kixi.metrics.name-safety :refer [safe-name]]
            [kixi.metrics.reporters.json-console :as reporter]
            [metrics
             [core :refer [new-registry]]
             [histograms :refer [histogram update!]]
             [meters :refer [mark! meter]]]
            [metrics.jvm.core :as jvm]
            [metrics.ring.expose :as expose]
            [taoensso.timbre :as log]))

(def upper-name 
  (memoize 
   (fn [level]
     (str/upper-case (name level)))))

(def uri-method-status->metric-name
  (memoize 
   (fn [uri method status]
     (let [name (-> (str "." (upper-name method))
                    safe-name
                    (str "." status))]
       ["resources" (safe-name uri) name]))))

(defn request-ctx->metric-name
  [ctx]
  (uri-method-status->metric-name
   (get-in ctx [:request :uri])
   (:method ctx)
   (get-in ctx [:response :status])))

(defn now
  []
  (System/currentTimeMillis))

(defn append-rates
  [metric-name]
  (update metric-name 2
          #(str % ".rates")))

(defn insert-time
  "Interceptor that stores the current time in the context under [:metrics ky]"
  [ky]
  (fn [ctx]
    (assoc-in ctx [:metrics ky] (now))))

(defn record-metrics
  "Interceptor that records the time since [:metrics ky]"
  [registry ky]
  (fn [ctx]
    (let [metric-name (request-ctx->metric-name ctx)
          met (meter registry (append-rates metric-name))]
      (when-let [start-time (get-in ctx [:metrics ky])]
      ;On exception this won't get the modified context, i.e. the one with start time in it. This is due to the manifold chain and catch mechinism used.
        (update! (histogram registry metric-name) 
                 (- (now) start-time)))
      (mark! met))<commit_msg>Switch to a timer so that we can specify time units and have nanoseconds as the default across microservices<commit_after>(ns kixi.datastore.metrics
  (:require [clojure.string :as str]
            [com.stuartsierra.component :as component]
            [kixi.metrics.name-safety :refer [safe-name]]
            [kixi.metrics.reporters.json-console :as reporter]
            [metrics
             [core :refer [new-registry]]
             [timers :refer [time! timer]]
             [meters :refer [mark! meter]]]
            [metrics.jvm.core :as jvm]
            [metrics.ring.expose :as expose]
            [taoensso.timbre :as log])
  (:import (java.util.concurrent TimeUnit)))

(def upper-name 
  (memoize 
   (fn [level]
     (str/upper-case (name level)))))

(def uri-method-status->metric-name
  (memoize 
   (fn [uri method status]
     (let [name (-> (str "." (upper-name method))
                    safe-name
                    (str "." status))]
       ["resources" (safe-name uri) name]))))

(defn request-ctx->metric-name
  [ctx]
  (uri-method-status->metric-name
   (get-in ctx [:request :uri])
   (:method ctx)
   (get-in ctx [:response :status])))

(defn now
  []
  (System/currentTimeMillis))

(defn append-rates
  [metric-name]
  (update metric-name 2
          #(str % ".rates")))

(defn insert-time
  "Interceptor that stores the current time in the context under [:metrics ky]"
  [ky]
  (fn [ctx]
    (assoc-in ctx [:metrics ky] (now))))

(defn record-metrics
  "Interceptor that records the time since [:metrics ky]"
  [registry ky]
  (fn [ctx]
    (let [metric-name (request-ctx->metric-name ctx)
          met (meter registry (append-rates metric-name))]
      (when-let [start-time (get-in ctx [:metrics ky])]
        ;On exception this won't get the modified context, i.e. the one with start time in it. This is due to the manifold chain and catch mechinism used.
        (.update (timer registry metric-name) (- (now) start-time) TimeUnit/MILLISECONDS))
      (mark! met))
{'fraction_non_alphanumeric': 0.1395709485655208, 'fraction_numerical': 0.0005169294391315585, 'mean_word_length': 2.4708520179372195, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '5501227', 'n_tokens_mistral': 1295, 'n_tokens_neox': 1249, 'n_words': 369}