<commit_before>(ns wikia.common.perfmonitoring.core
  (:import (java.net DatagramSocket DatagramPacket InetAddress))
  (:require [cheshire.core :as json]
            [wikia.common.logger :as log]
            [environ.core :refer [env]]))

(declare format-content
         format-series-name
         send-data
         write-listener)

(def ^{:private true} host (env :perfmonitoring-host))
(def ^{:private true} port (Integer. (env :perfmonitoring-port 5551)))
(def ^{:private true} app (.toLowerCase (env :perfmonitoring-app "wikia")))
(def ^{:private true} series-name (env :perfmonitoring-series-name :metrics))
(def ^{:private true} config (atom nil))

(defn get-series-name
  []
  series-name)

(defn init []
  (when (env :perfmonitoring-host)
    (swap! config #(or % {:host (InetAddress/getByName host)
                          :port port
                          :socket (DatagramSocket.)}))))

(defn format-series-name [series-name]
  (let [series-name (if (keyword? series-name)
                      (name series-name)
                      series-name)]
    (keyword (str app "_" (clojure.string/replace (.toLowerCase series-name) "-" "_")))))

(defn format-content [point]
  (let [series-name (:series-name point)
        point (dissoc point :series-name)
        columns (keys point)
        vals (vals point)]
    [{:name series-name
      :columns columns
      :points (list vals)}]))

(defn send-data [content]
  content
  (when-let [packet (try
                      (DatagramPacket.
                        ^"[B" (.getBytes content)
                        ^Integer (count content)
                        ^InetAddress (:host @config)
                        ^Integer (:port @config))
                      (catch Exception e
                        nil))]
    (.send (:socket @config) packet)))

(defn publish
  ([series-name point]
    (when @config
      (let [point (merge {:series-name (format-series-name series-name), :k8s "k8s"} point)]
        (future (send-data (json/generate-string (format-content point)))))))
  ([point]
    (publish (get-series-name) point)))

(defn current-time
  []
  (System/currentTimeMillis))

(defmacro timing [metric & body]
  `(let [start# (current-time)]
     (try
       ~@body
       (finally
         (publish (get-series-name) {~metric (- (current-time) start#)})))))
<commit_msg>SER-2523 | Include k8s label when publishing<commit_after>(ns wikia.common.perfmonitoring.core
  (:import (java.net DatagramSocket DatagramPacket InetAddress))
  (:require [cheshire.core :as json]
            [wikia.common.logger :as log]
            [environ.core :refer [env]]))

(declare format-content
         format-series-name
         send-data
         write-listener)

(def ^{:private true} host (env :perfmonitoring-host))
(def ^{:private true} port (Integer. (env :perfmonitoring-port 5551)))
(def ^{:private true} app (.toLowerCase (env :perfmonitoring-app "wikia")))
(def ^{:private true} series-name (env :perfmonitoring-series-name :metrics))
(def ^{:private true} config (atom nil))

(defn get-series-name
  []
  series-name)

(defn init []
  (when (env :perfmonitoring-host)
    (swap! config #(or % {:host (InetAddress/getByName host)
                          :port port
                          :socket (DatagramSocket.)}))))

(defn format-series-name [series-name]
  (let [series-name (if (keyword? series-name)
                      (name series-name)
                      series-name)]
    (keyword (str app "_" (clojure.string/replace (.toLowerCase series-name) "-" "_")))))

(defn format-content [point]
  (let [series-name (:series-name point)
        point (dissoc point :series-name)
        columns (keys point)
        vals (vals point)]
    [{:name series-name
      :columns columns
      :points (list vals)}]))

(defn send-data [content]
  content
  (when-let [packet (try
                      (DatagramPacket.
                        ^"[B" (.getBytes content)
                        ^Integer (count content)
                        ^InetAddress (:host @config)
                        ^Integer (:port @config))
                      (catch Exception e
                        nil))]
    (.send (:socket @config) packet)))

(defn publish
  ([series-name point]
    (when @config
      (let [point (merge {:series-name (format-series-name series-name) :k8s "k8s"} point)]
        (future (send-data (json/generate-string (format-content point)))))))
  ([point]
    (publish (get-series-name) point)))

(defn current-time
  []
  (System/currentTimeMillis))

(defmacro timing [metric & body]
  `(let [start# (current-time)]
     (try
       ~@body
       (finally
         (publish (get-series-name) {~metric (- (current-time) start#)})))))

{'fraction_non_alphanumeric': 0.14627434377646062, 'fraction_numerical': 0.0035986452159187127, 'mean_word_length': 2.294979079497908, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '238686', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1424, 'n_words': 377}