<commit_before>(ns riemann.stackdriver
  "Forwards events to Stackdriver."
  (:require riemann.time
            [clj-http.client :as client]
            [cheshire.core :refer [generate-string]]
            [clojure.string :as str]))

(def gateway-url "https://custom-gateway.stackdriver.com/v1/custom")

(defn metric-name
  "Constructs a metric name from an event."
  [opts event]
  (let [service ((:name opts) event)]
     (str/replace service #"\s+" ".")))

(defn generate-datapoint
  "Accepts riemann event/events and converts it into equivalent stackdriver datapoint."
  [opts event-or-events]
  (let [events (if (sequential? event-or-events) event-or-events (list event-or-events))]
    (map (fn [event]
      {:name (metric-name opts event)
       :value (:metric event)
       :collected_at (long (:time event))})
    events)))

(defn post-datapoint
  "Post the riemann metrics datapoints."
  [api-key uri data]
  (let [http-options {:body data
                      :content-type :json
                      :headers {"x-stackdriver-apikey" api-key}}]
    (client/post uri http-options)))

(defn stackdriver
  "Returns a function which accepts an event/events and sends it to Stackdriver."
  [opts]
  (let [ts (atom 0)
        opts (merge {:api-key "stackdriver-api-key"
                     :name :service} opts)]
    (fn [event]
      (let [post-data {:timestamp (swap! ts #(max (inc %) (long (riemann.time/unix-time))))
                       :proto_version 1
                       :data (generate-datapoint opts (remove (comp nil? :metric) event))}
            json-data (generate-string post-data)]
        (post-datapoint (:api-key opts) gateway-url json-data)))))<commit_msg>stackdriver plugin fixes: - fix handling of single events - do not post when there are no data points<commit_after>(ns riemann.stackdriver
  "Forwards events to Stackdriver."
  (:require riemann.time
            [clj-http.client :as client]
            [cheshire.core :refer [generate-string]]
            [clojure.string :as str]))

(def gateway-url "https://custom-gateway.stackdriver.com/v1/custom")

(defn metric-name
  "Constructs a metric name from an event."
  [opts event]
  (let [service ((:name opts) event)]
     (str/replace service #"\s+" ".")))

(defn generate-datapoints
  "Accepts riemann event/events and converts it into equivalent stackdriver datapoint."
  [opts event-or-events]
  (->> (if (sequential? event-or-events) event-or-events (list event-or-events))
       (remove (comp nil? :metric))
       (map (fn [event]
              {:name (metric-name opts event)
               :value (:metric event)
               :collected_at (long (:time event))}))))

(defn post-datapoints
  "Post the riemann metrics datapoints."
  [api-key uri data]
  (let [http-options {:body data
                      :content-type :json
                      :headers {"x-stackdriver-apikey" api-key}}]
    (client/post uri http-options)))

(defn stackdriver
  "Returns a function which accepts an event/events and sends it to Stackdriver."
  [opts]
  (let [ts (atom 0)
        opts (merge {:api-key "stackdriver-api-key"
                     :name :service} opts)]
    (fn [event-or-events]
      (when-let [data (not-empty (generate-datapoints opts event-or-events))]
        (->>  {:timestamp (swap! ts #(max (inc %) (long (riemann.time/unix-time))))
               :proto_version 1
               :data data}
              (generate-string)
              (post-datapoints (:api-key opts) gateway-url))))))
{'fraction_non_alphanumeric': 0.13563677898343804, 'fraction_numerical': 0.0017133066818960593, 'mean_word_length': 2.53125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '7529107', 'n_tokens_mistral': 1120, 'n_tokens_neox': 1075, 'n_words': 317}