clj-http-client/test/puppetlabs/http/client/test_common.clj
Ruth Linehan df4e36a1aa (TK-316) Add metrics support
This commit adds metrics support to the http client (clojure and java, sync
and async). A metric registry can optionally be passed into the client as a
client option on creation. If a metric registry is present, timers will be
added to time each request.

By default, a timer is added for the URL (stripped of username, password,
query string, and path fragments) and the URL plus the method used for the
request. In addition, a request can include a `metric-id` option, which takes
a tuple of metric ids. If this request option is specified, a timer will be
created for each element of the metric id tuple - thus if the tuple is [:foo
:bar :baz] there will be a foo timer, a foo.bar timer, and a foo.bar.baz
timer.

In addition, each timer has a "MetricType" - currently there is only one
metric type, bytes-read, which is stopped when the full response has been
read. In the future, we may add "response-init" timers that get stopped when
the first byte of the response has been read.

This commit also adds a `get-client-metrics`/`.getClientMetrics` function that
takes a client instance and returns the http client-specific metrics from the
metric registry and a `get-client-metrics-data`/`.getClientMetricsData`
function for clojure and java sync and async clients to get out metrics data
from the client. This function takes a client instance and returns a map of
metric name to a map of metric data (for clojure) or a ClientMetricData object
(for java), both of which include the mean, count, and aggregate for the timer

These `get-client-metrics*`/`.getClientMetrics*` functions also have versions
that take a url, url and method, or metric id to allow for filtering of the
timers/metrics data returned by these functions.

The clojure versions of these functions take a metric filter map. There are
also metric filter builder functions to build up the type of metric filter
desired from a url, a url and method, or a metric id. These will prevent users
from having to know the specifics of how to build a metric themselves; instead
they can use a convenience function.

An empty metric id can be passed in to the filter to return all metric-id
timers.
2016-04-19 13:13:10 -07:00

54 lines
1.7 KiB
Clojure

(ns puppetlabs.http.client.test-common
(:require [ring.middleware.params :as ring-params]
[puppetlabs.trapperkeeper.core :as tk]
[puppetlabs.trapperkeeper.testutils.logging :as testlogging])
(:import (java.net ConnectException SocketTimeoutException)))
(defn query-params-test
[req]
{:status 200
:body (str (:query-params req))})
(def app-wrapped
(ring-params/wrap-params query-params-test))
(tk/defservice test-params-web-service
[[:WebserverService add-ring-handler]]
(init [this context]
(add-ring-handler app-wrapped "/params")
context))
(def queryparams {"foo" "bar"
"baz" "lux"})
(def query-options {:method :get
:url "http://localhost:8080/params/"
:query-params queryparams
:as :text})
(defn redirect-test-handler
[req]
(condp = (:uri req)
"/hello/world" {:status 200 :body "Hello, World!"}
"/hello" {:status 302
:headers {"Location" "/hello/world"}
:body ""}
{:status 404 :body "D'oh"}))
(tk/defservice redirect-web-service
[[:WebserverService add-ring-handler]]
(init [this context]
(add-ring-handler redirect-test-handler "/hello")
context))
(defmacro connect-exception-thrown?
[& body]
`(try
(testlogging/with-test-logging ~@body)
(catch SocketTimeoutException _# true)
(catch ConnectException _# true)))
(defn elapsed-within-range?
[start-time-milliseconds duration-milliseconds]
(<= (System/currentTimeMillis) (+ start-time-milliseconds
duration-milliseconds)))