Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: gorsuch/clj-librato
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: aphyr/clj-librato
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 18 commits
  • 4 files changed
  • 5 contributors

Commits on Jan 3, 2013

  1. Copy the full SHA
    ceaae21 View commit details
  2. Copy the full SHA
    c021df8 View commit details

Commits on Jul 16, 2013

  1. Copy the full SHA
    b4e0a96 View commit details
  2. Copy the full SHA
    8930dcb View commit details
  3. Version 0.0.3

    aphyr committed Jul 16, 2013
    Copy the full SHA
    f5cad04 View commit details
  4. 0.0.4-SNAPSHOT

    aphyr committed Jul 16, 2013
    Copy the full SHA
    5dee0ba View commit details

Commits on Nov 22, 2013

  1. Add http options and connection-manager

    Adds the connection--manager function to return a connection manager.
    Adds an option map to each api function that is used in constructing the
    http request.  The return value of connection-manager can be passed in
    the options on the :connection-manager key to enable persistent
    connections.
    hugoduncan committed Nov 22, 2013
    Copy the full SHA
    309b9e4 View commit details
  2. Add back api compatability

    Also adds logging for deprecation warning.
    hugoduncan committed Nov 22, 2013
    Copy the full SHA
    bd2a95e View commit details

Commits on Dec 19, 2013

  1. Merge pull request #1 from hugoduncan/feature/add-persistent-connection

    Add http options and connection-manager
    aphyr committed Dec 19, 2013
    Copy the full SHA
    d6ba622 View commit details

Commits on Dec 26, 2013

  1. Typo: SHAPSHOT->SNAPSHOT

    aphyr committed Dec 26, 2013
    Copy the full SHA
    eaafb64 View commit details

Commits on Apr 30, 2014

  1. 0.0.4

    aphyr committed Apr 30, 2014
    Copy the full SHA
    a7de930 View commit details

Commits on May 3, 2014

  1. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    61237f8 View commit details

Commits on May 4, 2014

  1. Merge pull request #2 from mcna/master

    Add missing nil arguments for create-annotation options
    aphyr committed May 4, 2014
    Copy the full SHA
    1b1e846 View commit details

Commits on Jul 28, 2014

  1. provide better coordinates

    pyr committed Jul 28, 2014
    Copy the full SHA
    49ec2b7 View commit details
  2. Merge pull request #3 from pyr/bugfix/coordinates

    provide better coordinates
    aphyr committed Jul 28, 2014
    Copy the full SHA
    7bb0fb2 View commit details
  3. clj-librato 0.0.5

    aphyr committed Jul 28, 2014
    Copy the full SHA
    e7e0cf5 View commit details

Commits on Jun 13, 2015

  1. Copy the full SHA
    26b0d5f View commit details

Commits on Jul 13, 2015

  1. Merge pull request #4 from donbonifacio/patch-1

    Fix typos on documentation
    aphyr committed Jul 13, 2015
    Copy the full SHA
    776475f View commit details
Showing with 236 additions and 57 deletions.
  1. +2 −2 README.md
  2. +15 −4 project.clj
  3. +97 −47 src/clj_librato/metrics.clj
  4. +122 −4 test/clj_librato/metrics_test.clj
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# clj-librato

A Clojure library for interacting with [Librato Metrics](https://metrics.librato.com). Currently at version 0.0.1, and focusing on sending collated metrics.
A Clojure library for interacting with [Librato Metrics](https://metrics.librato.com). Currently maintained by aphyr (https://github.com/aphyr/clj-librato).

Feel free to contribute!

@@ -10,7 +10,7 @@ Feel free to contribute!
(require '[clj-librato.metrics :as metrics])

; pass it an email, api key, list of gauges, and a list of counters
(metrics/collate "me@mydomain.com" "my-api-key" [{:name "gauge 1" value: 34 } {:name "gauge 2" value: 0}]
(metrics/collate "me@mydomain.com" "my-api-key" [{:name "gauge 1" :value 34 } {:name "gauge 2" :value 0}]
[{:name "a counter" :value 79213}])
````

19 changes: 15 additions & 4 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
(defproject clj-librato "0.0.2"
(defproject clj-librato "0.0.5"
:description "Clojure interface to the Librato service"
:dependencies [[org.clojure/clojure "1.4.0"]
[clj-http "0.5.6"]
[clj-json "0.5.1"]])
:dependencies [[org.clojure/clojure "1.5.1"]
[org.clojure/tools.logging "0.2.6"]
[clj-http "0.9.1"
:exclusions [commons-logging]]
[cheshire "5.2.0"]]
:profiles
{:dev {:dependencies [[log4j/log4j "1.2.16"
:exclusions [javax.mail/mail
javax.jms/jms
com.sun.jdmk/jmxtools
com.sun.jmx/jmxri]]
[org.slf4j/slf4j-log4j12 "1.7.5"]
[org.slf4j/jcl-over-slf4j "1.7.5"]]}
:test {:resource-paths ["resources" "test-resources"]}})
144 changes: 97 additions & 47 deletions src/clj_librato/metrics.clj
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
(ns clj-librato.metrics
(:use [slingshot.slingshot :only [try+]])
(:require [clj-json.core :as json]
(:require [cheshire.core :as json]
[clj-http.client :as client]
[clj-http.conn-mgr :as conn-mgr]
[clojure.string :as string]
[clojure.tools.logging :as logging]
clj-http.util))

(def uri-base "https://metrics-api.librato.com/v1/")

(defn uri
"The full URI of a particular resource, by path fragments."
[& path-fragments]
(apply str uri-base
(interpose "/" (map (comp clj-http.util/url-encode str)
(apply str uri-base
(interpose "/" (map (comp clj-http.util/url-encode str)
path-fragments))))

(defn unparse-kw
"Convert a clojure-style dashed keyword map into string underscores.
Recursive."
[m]
(cond
(map? m) (into {} (map (fn [[k v]]
[(string/replace (name k) "-" "_")
(unparse-kw v)])
(map? m) (into {} (map (fn [[k v]]
[(string/replace (name k) "-" "_")
(unparse-kw v)])
m))
(coll? m) (map unparse-kw m)
true m))
@@ -32,36 +34,58 @@
[m]
(into {} (map (fn [[k v]] [(keyword (string/replace k "_" "-")) v]) m)))

(defn connection-manager
"Return a connection manager that can be passed as :connection-manager in
a request."
[{:keys [timeout threads] :or {timeout 10 threads 2} :as options}]
(conn-mgr/make-reusable-conn-manager
(merge {:timeout timeout :threads threads :default-per-route threads}
options)))

(defn request
"Constructs the HTTP client request map."
"Constructs the HTTP client request map.
options will be merged verbatim into the request map."
([user api-key params]
{:basic-auth [user api-key]
:content-type :json
:accept :json
:throw-entire-message? true
:query-params (unparse-kw params)})

([user api-key params body]
(assoc (request user api-key params)
:body (json/generate-string (unparse-kw body)))))
:body (json/generate-string (unparse-kw body)))))

(defn collate [user api-key gauges counters]
"Posts a set of gauges and counters."
(client/post (uri "metrics")
(request user api-key {} {:gauges gauges :counters counters})))
(defn collate
"Posts a set of gauges and counters. options is a map of clj-http options."
([user api-key gauges counters]
(collate user api-key gauges counters nil))
([user api-key gauges counters options]
(assert (every? :name gauges))
(assert (every? :name counters))
(assert (every? :value gauges))
(assert (every? :value counters))
(client/post (uri "metrics")
(merge
options
(request user api-key {}
{:gauges gauges :counters counters})))))

(defn metric
"Gets a metric by name.
See http://dev.librato.com/v1/get/metrics"
([user api-key name]
(metric user api-key name {}))

(metric user api-key name {} nil))
([user api-key name params]
(metric user api-key name params nil))

([user api-key name params options]
(assert name)
(try+
(let [body (-> (client/get (uri "metrics" name)
(request user api-key params))
(merge
options
(request user api-key params)))
:body json/parse-string parse-kw)]
(assoc body :measurements
(into {} (map (fn [[source measurements]]
@@ -73,44 +97,70 @@

(defn create-annotation
"Creates a new annotation, and returns the created annotation as a map.
http://dev.librato.com/v1/post/annotations/:name"
[user api-key name annotation]
(assert name)
(-> (client/post (uri "annotations" name)
(request user api-key {} annotation))
:body
json/parse-string
parse-kw))
([user api-key name annotation]
(create-annotation user api-key name annotation nil))
([user api-key name annotation options]
{:pre [(or (nil? options)(map? options))]}
(assert name)
(-> (client/post (uri "annotations" name)
(merge
options
(request user api-key {} annotation)))
:body
json/parse-string
parse-kw)))

(defn update-annotation
"Updates an annotation.
http://dev.librato.com/v1/put/annotations/:name/events/:id"
[user api-key name id annotation]
(assert name)
(assert id)
(client/put (uri "annotations" name id)
(request user api-key {} annotation)))

(defn annotate
"Creates or updates an annotation. If id is given, updates. If id is
missing, creates a new annotation."
([user api-key name annotation]
(create-annotation user api-key name annotation))
([user api-key name id annotation]
(update-annotation user api-key name id annotation)))
(update-annotation user api-key name id annotation nil))
([user api-key name id annotation options]
(assert name)
(assert id)
(client/put (uri "annotations" name id)
(merge
options
(request user api-key {} annotation)))))

(let [warn-on-deprecate (atom true)]
;; Deprecated due to argument ambiguity.
;; A future version could rename create-annotation as annotate.
(defn annotate
"Creates or updates an annotation. If id is given, updates. If id is
missing, creates a new annotation."
([user api-key name annotation]
(create-annotation user api-key name annotation nil))
([user api-key name annotation options]
(if (map? annotation)
(create-annotation user api-key name annotation options)
(do
;; user api-key name id annotation
(update-annotation user api-key name annotation options)
(when @warn-on-deprecate
(reset! warn-on-deprecate false)
(logging/warn
(str "`annotate` called for annotation update is deprecated. "
"Please use update-annotation."))))))))

(defn annotation
"Find a particular annotation event.
See http://dev.librato.com/v1/get/annotations/:name/events/:id"
[user api-key name id]
(assert name)
(assert id)
(try+
(-> (client/get (uri "annotations" name id) (request user api-key {}))
:body
json/parse-string
parse-kw)
(catch [:status 404] _ nil)))
([user api-key name id]
(annotation user api-key name id nil))
([user api-key name id options]
(assert name)
(assert id)
(try+
(-> (client/get (uri "annotations" name id)
(merge
options
(request user api-key {})))
:body
json/parse-string
parse-kw)
(catch [:status 404] _ nil))))
Loading