-
Notifications
You must be signed in to change notification settings - Fork 30
/
metrics.clj
129 lines (112 loc) · 4.62 KB
/
metrics.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
(ns puppetlabs.http.client.metrics
(:require [clojure.string :as clj-string]
[puppetlabs.http.client.common :as common]
[schema.core :as schema])
(:import (com.codahale.metrics MetricRegistry)
(com.puppetlabs.http.client.metrics ClientMetricData Metrics
Metrics$MetricType)))
(schema/defn get-base-metric-data :- common/BaseMetricData
[data :- ClientMetricData]
{:count (.getCount data)
:mean (.getMean data)
:aggregate (.getAggregate data)
:metric-name (.getMetricName data)})
(schema/defn get-url-metric-data :- common/UrlMetricData
[data :- ClientMetricData]
(assoc (get-base-metric-data data) :url (.getUrl data)))
(schema/defn get-url-and-method-metric-data :- common/UrlAndMethodMetricData
[data :- ClientMetricData]
(assoc (get-url-metric-data data) :method (.getMethod data)))
(schema/defn get-metric-id-metric-data :- common/MetricIdMetricData
[data :- ClientMetricData]
(assoc (get-base-metric-data data) :metric-id (.getMetricId data)))
(defn get-java-metric-type
[metric-type]
(case metric-type
:full-response Metrics$MetricType/FULL_RESPONSE))
(defn uppercase-method
[method]
(clj-string/upper-case (name method)))
(defn build-metric-namespace
[metric-prefix server-id]
(Metrics/buildMetricNamespace metric-prefix server-id))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Public
(schema/defn ^:always-validate url->metric-url :- schema/Str
[url :- schema/Str]
(Metrics/urlToMetricUrl url))
(schema/defn ^:always-validate get-client-metrics
:- (schema/maybe common/AllMetrics)
"Returns the http client-specific metrics from the metric registry."
[metric-registry :- MetricRegistry]
(let [metrics (Metrics/getClientMetrics metric-registry)]
{:url (.getUrlTimers metrics)
:url-and-method (.getUrlAndMethodTimers metrics)
:metric-id (.getMetricIdTimers metrics)}))
(schema/defn ^:always-validate get-client-metrics-by-url
:- common/Metrics
"Returns the http client-specific url metrics matching the specified url."
[metric-registry :- MetricRegistry
url :- schema/Str]
(Metrics/getClientMetricsByUrl
metric-registry
url))
(schema/defn ^:always-validate get-client-metrics-by-url-and-method
:- common/Metrics
"Returns the http client-specific url metrics matching the specified url."
[metric-registry :- MetricRegistry
url :- schema/Str
method :- common/HTTPMethod]
(Metrics/getClientMetricsByUrlAndMethod
metric-registry
url
method))
(schema/defn ^:always-validate get-client-metrics-by-metric-id
:- common/Metrics
"Returns the http client-specific url metrics matching the specified url."
[metric-registry :- MetricRegistry
metric-id :- common/MetricId]
(Metrics/getClientMetricsByMetricId
metric-registry
(into-array String (map name metric-id))))
(schema/defn ^:always-validate get-client-metrics-data
:- common/AllMetricsData
"Returns a summary of the metric data for all http client timers, organized
in a map by category."
[metric-registry :- MetricRegistry]
(let [data (Metrics/getClientMetricsData metric-registry)]
{:url (map get-url-metric-data (.getUrlData data))
:url-and-method (map get-url-and-method-metric-data (.getUrlAndMethodData data))
:metric-id (map get-metric-id-metric-data (.getMetricIdData data))}))
(schema/defn ^:always-validate get-client-metrics-data-by-url
:- [common/UrlMetricData]
"Returns a summary of the metric data for all http client timers filtered by
url."
[metric-registry :- MetricRegistry
url :- schema/Str]
(let [data (Metrics/getClientMetricsDataByUrl
metric-registry
url)]
(map get-url-metric-data data)))
(schema/defn ^:always-validate get-client-metrics-data-by-url-and-method
:- [common/UrlAndMethodMetricData]
"Returns a summary of the metric data for all http client timers filtered by
url and method."
[metric-registry :- MetricRegistry
url :- schema/Str
method :- common/HTTPMethod]
(let [data (Metrics/getClientMetricsDataByUrlAndMethod
metric-registry
url
(uppercase-method method))]
(map get-url-and-method-metric-data data)))
(schema/defn ^:always-validate get-client-metrics-data-by-metric-id
:- [common/MetricIdMetricData]
"Returns a summary of the metric data for all http client timers filtered by
metric-id."
[metric-registry :- MetricRegistry
metric-id :- common/MetricId]
(let [data (Metrics/getClientMetricsDataByMetricId
metric-registry
(into-array String (map name metric-id)))]
(map get-metric-id-metric-data data)))