Permalink
Browse files

Merge remote-tracking branch 'ais/master' into merge-branches

Conflicts:
	pystatsd/server.py
  • Loading branch information...
2 parents 255bfd0 + e9336c2 commit 62b49e2a254e9f54698680fe76bfacf4c0d2a4db @Jonty committed Apr 5, 2012
Showing with 22 additions and 15 deletions.
  1. +22 −15 pystatsd/server.py
View
@@ -1,6 +1,7 @@
import re
import socket
import threading
+import sys
import time
import types
import logging
@@ -81,8 +82,8 @@ def process(self, data):
for bit in bits:
sample_rate = 1;
fields = bit.split('|')
- if None==fields[1]:
- log.error('Bad line: %s' % bit)
+ if len(fields) < 2:
+ log.error('Bad line: %s' % data)
return
if (fields[1] == 'ms'):
@@ -109,8 +110,7 @@ def flush(self):
v = float(v)
v = v if self.no_aggregate_counters else v / (self.flush_interval / 1000)
- if self.debug:
- print "Sending %s => count=%s" % ( k, v )
+ log.debug("Sending %s => count=%s" % ( k, v ))
if self.transport == 'graphite':
msg = '%s.%s %s %s\n' % (self.counters_prefix, k, v, ts)
@@ -142,8 +142,7 @@ def flush(self):
self.timers[k] = []
- if self.debug:
- print "Sending %s ====> lower=%s, mean=%s, upper=%s, %dpct=%s, count=%s" % ( k, min, mean, max, self.pct_threshold, max_threshold, count )
+ log.debug("Sending %s ====> lower=%s, mean=%s, upper=%s, %dpct=%s, count=%s" % ( k, min, mean, max, self.pct_threshold, max_threshold, count ))
if self.transport == 'graphite':
@@ -172,21 +171,19 @@ def flush(self):
if self.transport == 'graphite':
+ log.info("Sending data to graphite")
stat_string += "statsd.numStats %s %d\n" % (stats, ts)
- graphite = socket.socket()
try:
+ graphite = socket()
graphite.connect((self.graphite_host, self.graphite_port))
graphite.sendall(stat_string)
graphite.close()
- except socket.error, e:
- log.error("Error communicating with Graphite: %s" % e)
- if self.debug:
- print "Error communicating with Graphite: %s" % e
+ except Exception, e:
+ log.error("Error while sending to graphite: %s", e)
self._set_timer()
- if self.debug:
- print "\n================== Flush completed. Waiting until next flush. Sent out %d metrics =======" % ( stats )
+ log.debug("\n================== Flush completed. Waiting until next flush. Sent out %d metrics =======" % ( stats ))
def _set_timer(self):
@@ -219,7 +216,6 @@ def run(self, options):
if setproctitle:
setproctitle('pystatsd')
server = Server(pct_threshold = options.pct,
- debug = options.debug,
transport = options.transport,
graphite_host = options.graphite_host,
graphite_port = options.graphite_port,
@@ -234,7 +230,6 @@ def run(self, options):
server.serve(options.name, options.port)
def run_server():
- import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--debug', dest='debug', action='store_true', help='debug mode', default=False)
@@ -252,11 +247,23 @@ def run_server():
parser.add_argument('--timers-prefix', dest='timers_prefix', help='prefix to append before sending timing data to graphite (default: stats.timers)', type=str, default='stats.timers')
parser.add_argument('-t', '--pct', dest='pct', help='stats pct threshold (default: 90)', type=int, default=90)
parser.add_argument('-D', '--daemon', dest='daemonize', action='store_true', help='daemonize', default=False)
+ parser.add_argument('-l', '--log-file', dest='log_file', help='Log file, default is STDOUT', type=str)
+ parser.add_argument('-L', '--log-level', dest='log_level', help='Log level, default is warn', type=str, default="warn")
parser.add_argument('--pidfile', dest='pidfile', action='store', help='pid file', default='/tmp/pystatsd.pid')
parser.add_argument('--restart', dest='restart', action='store_true', help='restart a running daemon', default=False)
parser.add_argument('--stop', dest='stop', action='store_true', help='stop a running daemon', default=False)
options = parser.parse_args(sys.argv[1:])
+ if options.log_file and not options.debug:
+ logfile = open(options.log_file, 'a')
+ else:
+ logfile = sys.stdout
+
+ log.addHandler(logging.StreamHandler(logfile))
+ if options.debug:
+ options.log_level = "debug"
+ log.setLevel(getattr(logging, options.log_level.upper()))
+
daemon = ServerDaemon(options.pidfile)
if options.daemonize:
daemon.start(options)

0 comments on commit 62b49e2

Please sign in to comment.