55 lines
1.7 KiB
Clojure
55 lines
1.7 KiB
Clojure
(ns auto-ap.background.metrics
|
|
(:require [com.unbounce.dogstatsd.core :as statsd]
|
|
[mount.core :as mount]
|
|
[clj-http.client :as http]
|
|
[clojure.tools.logging :as log]
|
|
[config.core :refer [env]]
|
|
[unilog.context :as lc]))
|
|
|
|
(defn get-container-data []
|
|
(try
|
|
(let [result
|
|
(some-> env
|
|
(:ecs-container-metadata-uri-v4)
|
|
(http/get {:as :json})
|
|
:body)]
|
|
result)
|
|
(catch Exception e
|
|
(log/warn "cannot find container tags" e)
|
|
{})))
|
|
|
|
(mount/defstate container-data
|
|
:start (get-container-data)
|
|
:stop nil)
|
|
|
|
(defn get-container-tags []
|
|
[(str "container:" (:DockerId container-data))
|
|
(str "ip:" (-> container-data :Networks first :IPv4Addresses first))])
|
|
|
|
|
|
(mount/defstate container-tags
|
|
:start (get-container-tags)
|
|
:stop nil)
|
|
|
|
(defn set-logging-context []
|
|
(when (seq container-data)
|
|
(lc/push-context "container" (:DockerId container-data))
|
|
(lc/push-context "ip" (-> container-data :Networks first :IPv4Addresses first))))
|
|
|
|
(defn stop-logging-context []
|
|
(when (seq container-data)
|
|
(lc/pull-context "container")
|
|
(lc/pull-context "ip")))
|
|
|
|
(mount/defstate logging-context
|
|
:start (set-logging-context)
|
|
:stop (stop-logging-context))
|
|
|
|
(mount/defstate metrics-setup
|
|
:start (statsd/setup! :host "127.0.0.1" :port 8125 :prefix "integreat.app" :tags (into #{(str "env:" (:dd-env env))
|
|
(str "service:" (:dd-service env))}
|
|
(container-tags)))
|
|
:stop (statsd/shutdown!))
|
|
|
|
|