Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Send off metrics to RestfulMetrics

Per-URL expansions (visitors), total expansions and URL creations are
sent off to RestfulMetrics.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
  • Loading branch information...
commit bc21a41ac7f06a318f5340388772da3993f21fa9 1 parent 3f550fb
@algernon authored
Showing with 40 additions and 6 deletions.
  1. +5 −1 README.md
  2. +3 −1 project.clj
  3. +22 −0 src/byr/metrics.clj
  4. +10 −4 src/byr/web.clj
View
6 README.md
@@ -6,6 +6,7 @@ a handful of features only:
* Supports custom short URLs
* Shortens automatically if no custom URL is supplied
* Mappings are stored compactly in MongoDB
+ * Metrics are logged to RestfulMetrics
* Offers a minimalistic index on the root URI
## API
@@ -26,13 +27,15 @@ set.
## Configuration
-The application will look for three environment variables:
+The application will look for the following environment variables:
* `PORT`: If set, byr will listen on the specified port.
* `MONGOLAB_URI`: If set, will connect to MongoDB using the URI
specified. Otherwise to localhost.
* `BYR_BASE`: The base URL for the application, will be prepended to
the ids used for shortening.
+ * `RESTFUL_METRICS_API_KEY`: API key for [RestfulMetrics][3].
+ * `BYR_METRIC_APP_NAME`: Application name to use with RestfulMetrics.
## How it works
@@ -50,3 +53,4 @@ Distributed under the GNU GPL v3+.
[1]: http://www.heroku.com/
[2]: http://www.hotot.org/
+ [3]: http://www.restfulmetrics.com/
View
4 project.clj
@@ -5,7 +5,9 @@
:dependencies [[org.clojure/clojure "1.4.0"]
[com.novemberain/monger "1.1.1"]
[ring/ring-jetty-adapter "1.1.1"]
- [compojure "1.1.1"]]
+ [compojure "1.1.1"]
+ [aleph "0.3.0-alpha2"]
+ [org.clojure/data.json "0.1.2"]]
:plugins [[lein-swank "1.4.4"]
[lein-ring "0.7.1"]]
:ring {:handler byr.web/app})
View
22 src/byr/metrics.clj
@@ -0,0 +1,22 @@
+(ns byr.metrics
+ (:use [aleph.http]
+ [clojure.data.json]))
+
+(def metric-worker (agent nil))
+(def app-name (System/getenv "BYR_METRIC_APP_NAME"))
+(def api-key (System/getenv "RESTFUL_METRICS_API_KEY"))
+
+(defn metric-request
+ [name]
+
+ (sync-http-request {:method :post
+ :url (str "http://track.restfulmetrics.com/apps/" app-name "/metrics.json")
+ :headers {"Authorization" api-key}
+ :content-type "application/json"
+ :body (json-str {:metric {:name name
+ :value 1}})}))
+
+(defn fire-metric
+ [name]
+
+ (send metric-worker (metric-request name)))
View
14 src/byr/web.clj
@@ -1,6 +1,7 @@
(ns byr.web
(:use [compojure.core])
(:require [byr.db :as db]
+ [byr.metrics :as m]
[ring.adapter.jetty :as ring]
[ring.util.response :as resp]
[compojure.handler :as handler]
@@ -18,16 +19,21 @@
(let [url (byr-url-get id)]
(cond
(nil? url) nil
- :else (resp/redirect url))))
+ :else (do
+ (m/fire-metric (str "Expanded: " id))
+ (m/fire-metric "total-expansions")
+ (resp/redirect url)))))
(defn- byr-store-uri
[& params]
(try
(let [r (apply db/add-map params)]
- (-> (str base-url (:_id r))
- resp/response
- (resp/content-type "text/plain")))
+ (do
+ (m/fire-metric "short-id-created")
+ (-> (str base-url (:_id r))
+ resp/response
+ (resp/content-type "text/plain"))))
(catch Exception e
(-> "id already exists"
Please sign in to comment.
Something went wrong with that request. Please try again.