Permalink
Browse files

Fixed HTTP interface to consistently return 404 on missed data.

  • Loading branch information...
1 parent 05a668c commit 061137f1ffb0b31016db9eaa1e9e2199b9311ccf @sbtourist committed Oct 7, 2012
Showing with 56 additions and 50 deletions.
  1. +35 −28 src/nimrod/core/store.clj
  2. +7 −11 src/nimrod/log/tailer.clj
  3. +14 −11 src/nimrod/web/server.clj
View
@@ -141,14 +141,18 @@
(remove-metric [this metric-ns metric-type metric-id]
(update-rate-stats [:operations-per-second] (clock) (seconds 1))
- (sql/with-connection connection-factory
- (sql/transaction
- (sql/delete-rows
- "metrics"
- ["ns=? AND type=? AND id=?" metric-ns metric-type metric-id])))
- (dosync
- (if-let [metrics (get-in @memory [metric-ns metric-type])]
- (alter memory assoc-in [metric-ns metric-type] (dissoc metrics metric-id)))))
+ (if (get-in @memory [metric-ns metric-type metric-id])
+ (do
+ (sql/with-connection connection-factory
+ (sql/transaction
+ (sql/delete-rows
+ "metrics"
+ ["ns=? AND type=? AND id=?" metric-ns metric-type metric-id])))
+ (dosync
+ (when-let [metrics (get-in @memory [metric-ns metric-type])]
+ (alter memory assoc-in [metric-ns metric-type] (dissoc metrics metric-id))))
+ true)
+ false))
(read-metric [this metric-ns metric-type metric-id]
(update-rate-stats [:operations-per-second] (clock) (seconds 1))
@@ -184,26 +188,29 @@
(remove-history [this metric-ns metric-type metric-id tags age from to]
(update-rate-stats [:operations-per-second] (clock) (seconds 1))
- (let
- [now (clock)
- actual-from (or from 0)
- actual-to (if (nil? to) (- now (or age default-age)) to)]
- (sql/with-connection connection-factory
- (let
- [timestamps
- (sql/transaction
- (sql/with-query-results r
- ["SELECT MIN(timestamp) AS mints, MAX(timestamp) AS maxts FROM history WHERE ns=? AND type=? AND id=?" metric-ns metric-type metric-id]
- (first r)))
- min-timestamp (max (timestamps :mints) actual-from)
- max-timestamp (min (timestamps :maxts) actual-to)]
- (loop [upbound (min (+ min-timestamp (days 1)) max-timestamp)]
+ (if (get-in @memory [metric-ns metric-type metric-id])
+ (let
+ [now (clock)
+ actual-from (or from 0)
+ actual-to (if (nil? to) (- now (or age default-age)) to)]
+ (sql/with-connection connection-factory
+ (let
+ [timestamps
(sql/transaction
- (sql/delete-rows "history"
- ["ns=? AND type=? AND id=? AND timestamp>=? AND timestamp<? AND check_tags(metric, ?)"
- metric-ns metric-type metric-id min-timestamp upbound (string/join "," tags)]))
- (when (< upbound max-timestamp)
- (recur (min (+ upbound (days 1)) max-timestamp))))))))
+ (sql/with-query-results r
+ ["SELECT MIN(timestamp) AS mints, MAX(timestamp) AS maxts FROM history WHERE ns=? AND type=? AND id=?" metric-ns metric-type metric-id]
+ (first r)))
+ min-timestamp (max (timestamps :mints) actual-from)
+ max-timestamp (min (timestamps :maxts) actual-to)]
+ (loop [upbound (min (+ min-timestamp (days 1)) max-timestamp)]
+ (sql/transaction
+ (sql/delete-rows "history"
+ ["ns=? AND type=? AND id=? AND timestamp>=? AND timestamp<? AND check_tags(metric, ?)"
+ metric-ns metric-type metric-id min-timestamp upbound (string/join "," tags)]))
+ (when (< upbound max-timestamp)
+ (recur (min (+ upbound (days 1)) max-timestamp))))))
+ true)
+ false))
(aggregate-history [this metric-ns metric-type metric-id tags age from to aggregators]
(update-rate-stats [:operations-per-second] (clock) (seconds 1))
@@ -241,7 +248,7 @@
(sql/with-query-results
all-types
["SELECT type FROM metrics WHERE ns=? GROUP BY type ORDER BY type" metric-ns]
- (into [] (for [type all-types] (type :type)))))))
+ (when (seq all-types) (into [] (for [type all-types] (type :type))))))))
(stats [this]
(show-stats [[:operations-per-second]] (clock) (seconds 1))))
View
@@ -34,7 +34,7 @@
(defn start-tailer [id log interval end]
(let [tailer (create-tailer id log interval end)]
- (if end (log/info (str "Start processing log from end: " log)) (log/info (str "Start processing log from beginning: " log)))
+ (if end (log/info (str "Start processing log from end of file: " log)) (log/info (str "Start processing log from start of file: " log)))
(dosync
(if (contains? @tailers id)
(throw (IllegalStateException. (str "Duplicated log identifier: " id)))
@@ -44,16 +44,12 @@
id))
(defn stop-tailer [id]
- (let [tailer (ref nil)]
- (dosync
- (if (@tailers id)
- (do
- (ref-set tailer (@tailers id))
- (alter tailers dissoc id))
- (throw (IllegalStateException. (str "No tailer for id: " id)))))
- (if @tailer
- (.stop (@tailer :tailer))
- (log/info (str "Stop listening to log: " (@tailer :log))))))
+ (if-let [tailer (@tailers id)]
+ (do
+ (.stop (tailer :tailer))
+ (log/info (str "Stop processing log: " (tailer :log)))
+ true)
+ false))
(defn list-tailers []
(into (sorted-map)
View
@@ -91,17 +91,20 @@
(http/GET "/logs/" [:as request]
(redirect-response (drop-last-char (request :uri))))
(http/GET "/logs/:log-id" [log-id]
- (std-response :ok (map path-of (list-types @metrics-store log-id))))
+ (if-let [types (list-types @metrics-store log-id)]
+ (cors-response :ok (map path-of types))
+ (cors-response :not-found)))
(http/GET "/logs/:log-id/" [:as request]
(redirect-response (drop-last-char (request :uri))))
(http/POST "/logs/:log-id/stop" [log-id]
- (stop-tailer log-id)
- (std-response :no-content))
+ (if (stop-tailer log-id)
+ (std-response :no-content)
+ (std-response :not-found)))
(http/GET ["/logs/:log-id/:metric-type"] [log-id metric-type]
(if-let [metric-type (type-of metric-type)]
- (if-let [result (list-metrics @metrics-store log-id metric-type)]
- (cors-response :ok result)
+ (if-let [metrics (list-metrics @metrics-store log-id metric-type)]
+ (cors-response :ok metrics)
(cors-response :not-found))
(cors-response :error {:error (str "Bad metric type: " metric-type)})))
(http/GET ["/logs/:log-id/:metric-type/"] [log-id metric-type :as request]
@@ -115,9 +118,9 @@
(cors-response :error {:error (str "Bad metric type: " metric-type)})))
(http/POST ["/logs/:log-id/:metric-type/:metric-id/reset" :metric-id #"[^/?#]+"] [log-id metric-type metric-id]
(if-let [metric-type (type-of metric-type)]
- (do
- (remove-metric @metrics-store log-id metric-type metric-id)
- (std-response :no-content))
+ (if (remove-metric @metrics-store log-id metric-type metric-id)
+ (std-response :no-content)
+ (std-response :not-found))
(std-response :error {:error (str "Bad metric type: " metric-type)})))
(http/GET ["/logs/:log-id/:metric-type/:metric-id/history" :metric-id #"[^/?#]+" :tags #"[^/?#]+"]
@@ -137,9 +140,9 @@
(http/POST ["/logs/:log-id/:metric-type/:metric-id/history/delete" :metric-id #"[^/?#]+" :tags #"[^/?#]+"]
[log-id metric-type metric-id tags age from to]
(if-let [metric-type (type-of metric-type)]
- (do
- (remove-history @metrics-store log-id metric-type metric-id (or (extract-tags tags) #{}) (age-to-millis age) (time-to-millis (clock) from) (time-to-millis (clock) to))
- (std-response :no-content))
+ (if (remove-history @metrics-store log-id metric-type metric-id (or (extract-tags tags) #{}) (age-to-millis age) (time-to-millis (clock) from) (time-to-millis (clock) to))
+ (std-response :no-content)
+ (std-response :not-found))
(std-response :error {:error (str "Bad metric type: " metric-type)})))
(route/not-found ""))

0 comments on commit 061137f

Please sign in to comment.