Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

allow parameterised config

  • Loading branch information...
commit 6ea10790963b090def6d6dc52c779fcf561e3e3f 1 parent 11641c8
Tom Denley authored June 21, 2012
5  config.clj
... ...
@@ -0,0 +1,5 @@
  1
+{:port 8125
  2
+ :mgmt-port 8126
  3
+ :flush-interval 10000
  4
+ :backends '[backends.console
  5
+             backends.graphite]}
41  src/clj_statsd_svr.clj
@@ -5,19 +5,12 @@
5 5
   (:import [java.net DatagramPacket DatagramSocket])
6 6
   (:import [java.util.concurrent Executors LinkedBlockingQueue TimeUnit]))
7 7
 
8  
-;configuration
9  
-(def config {:port 8125
10  
-             :mgmt-port 8126
11  
-             :flush-interval 10000
12  
-             :backends '[backends.console backends.graphite]})
13  
-
14 8
 ;initialisation
15 9
 (def statistics (agent { :counters {} :timers {} :gauges {} }))
16 10
 
17 11
 ;backends
18  
-(doseq [backend (config :backends)] (require backend))
19  
-(defn backend-send [function & args]
20  
-  (doall (for [backend (config :backends)] (future (apply (ns-resolve backend function) args)))))
  12
+(defn backend-send [backends f & args]
  13
+  (doall (for [backend backends] (future (apply (ns-resolve backend f) args)))))
21 14
 
22 15
 ;statistics
23 16
 (defn update-stat [stats stat bucket f]
@@ -73,25 +66,25 @@
73 66
     (assoc @snapshot :timestamp (System/currentTimeMillis))))
74 67
 
75 68
 (defn distribute [report config]
76  
-  (backend-send 'publish report config))
  69
+  (backend-send (config :backends) 'publish report config))
77 70
 
78 71
 ;manangement
79 72
 (defn seconds-since [time-millis]
80 73
   (int (/ (- (System/currentTimeMillis) (or time-millis 0)) 1000)))
81 74
 
82  
-(defn vitals [startup-time-millis]
  75
+(defn vitals [backends startup-time-millis]
83 76
   (str "uptime: " (seconds-since startup-time-millis) "\r\n"
84 77
        "messages.bad_lines_seen: " (or (:bad_lines_seen (:counters @statistics)) 0) "\r\n"
85 78
        "messages.last_msg_seen: " (seconds-since (:last_msg_seen (:gauges @statistics))) "\r\n" 
86  
-       (reduce str (map #(str @% "\r\n") (backend-send 'status)))))
  79
+       (reduce str (map #(str @% "\r\n") (backend-send backends 'status)))))
87 80
 
88  
-(defn manage-via [socket startup-time-millis]
  81
+(defn manage-via [socket backends startup-time-millis]
89 82
   (let [in (.useDelimiter (java.util.Scanner. (.getInputStream socket)) #"[^\w\.\t]")
90 83
         out (java.io.PrintWriter. (.getOutputStream socket) true)
91 84
         done (atom false)]
92 85
     (def commands {"quit"     #(do (swap! done (fn [x] (not x))) "bye")
93 86
                    "help"     #(str "Commands: " (reduce (fn [x y] (str x ", " y)) (keys commands)))
94  
-                   "stats"    #(vitals startup-time-millis)
  87
+                   "stats"    #(vitals backends startup-time-millis)
95 88
                    "counters" #(@statistics :counters)
96 89
                    "timers"   #(@statistics :timers)
97 90
                    "gauges"   #(@statistics :gauges)})
@@ -100,17 +93,29 @@
100 93
         (.println out (str (response) "\n"))))
101 94
     (.close socket)))
102 95
 
103  
-(defn start-manager [port-no startup-time-millis]
  96
+(defn start-manager [port-no backends startup-time-millis]
104 97
   (let [server (java.net.ServerSocket. port-no)]
105  
-    (.start (Thread. #(while true (let [socket (.accept server)] (future (manage-via socket startup-time-millis))))))))
  98
+    (.start (Thread. #(while true (let [socket (.accept server)] (future (manage-via socket backends startup-time-millis))))))))
106 99
 
107 100
 ;lifecycle
108  
-(defn -main []
  101
+(defn start [config]
109 102
   (let [worker-count 2
110 103
         work-queue (LinkedBlockingQueue.)
111 104
         work-executor (Executors/newFixedThreadPool worker-count)
112 105
         report-executor (Executors/newSingleThreadScheduledExecutor)]
113 106
     (start-receiver (config :port) work-queue)
114 107
     (dotimes [_ worker-count] (.submit work-executor (new-worker work-queue)))
115  
-    (start-manager (config :mgmt-port) (System/currentTimeMillis)) 
  108
+    (start-manager (config :mgmt-port) (config :backends) (System/currentTimeMillis)) 
116 109
     (.scheduleAtFixedRate report-executor #(distribute (make-report) config) (config :flush-interval) (config :flush-interval) TimeUnit/MILLISECONDS)))
  110
+
  111
+;configuration
  112
+(def default-config {:port 8125
  113
+                     :mgmt-port 8126
  114
+                     :flush-interval 10000
  115
+                     :backends '[backends.console]})
  116
+
  117
+;main
  118
+(defn -main [& [config-file]]
  119
+  (let [config (if config-file (load-file config-file) default-config)]
  120
+    (doseq [backend (config :backends)] (require backend))
  121
+    (start config)))

0 notes on commit 6ea1079

Please sign in to comment.
Something went wrong with that request. Please try again.