Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Unbreak python example #205

Merged
merged 13 commits into from

2 participants

@SaveTheRbtz

Unbreak example and doctests.
Various style fixes, prettifications and simplifications

Tested with: nosetests + nc

@mrtazz
Owner

Awesome, thanks for fixing this!

@mrtazz mrtazz merged commit 37b2567 into etsy:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 9, 2012
  1. @SaveTheRbtz

    examples: python: unbreak example

    SaveTheRbtz authored
    After Statsd -> StatsdClient renaming example is borken.
  2. @SaveTheRbtz

    examples: python: unbreak StatsdClient.send()

    SaveTheRbtz authored
    send() is defined as staticmethod, so it' does not have self. Fixing
    this also requires all other methods convertion to non-static which is
    logical assuming the should all use self.addr set by __init__().
  3. @SaveTheRbtz

    examples: python: remove local_settings.py overrides

    SaveTheRbtz authored
    It's rather strange to override parameters passed to __init__ via custom
    settings file.
  4. @SaveTheRbtz
  5. @SaveTheRbtz
  6. @SaveTheRbtz
  7. @SaveTheRbtz
  8. @SaveTheRbtz

    examples: python: StatsdClient.send() returns outcome

    SaveTheRbtz authored
    It's useful but unused.
  9. @SaveTheRbtz
  10. @SaveTheRbtz
  11. @SaveTheRbtz
  12. @SaveTheRbtz
  13. @SaveTheRbtz
This page is out of date. Refresh to see the latest.
Showing with 33 additions and 56 deletions.
  1. +33 −56 examples/python_example.py
View
89 examples/python_example.py
@@ -3,102 +3,79 @@
# Steve Ivy <steveivy@gmail.com>
# http://monkinetic.com
-# this file expects local_settings.py to be in the same dir, with statsd host and port information:
-#
-# statsd_host = 'localhost'
-# statsd_port = 8125
+from random import random
+from socket import socket, AF_INET, SOCK_DGRAM
# Sends statistics to the stats daemon over UDP
class StatsdClient(object):
def __init__(self, host='localhost', port=8125):
- self.host = host
- self.port = port
- try:
- import local_settings as settings
- self.host = settings.statsd_host
- self.port = settings.statsd_port
- except:
- pass
- self.addr=(host, port)
+ self.addr = (host, port)
- @staticmethod
- def timing(stat, time, sample_rate=1):
+ def timing(self, stat, time, sample_rate=1):
"""
Log timing information
- >>> from python_example import Statsd
- >>> Statsd.timing('some.time', 500)
+ >>> from python_example import StatsdClient
+ >>> client = StatsdClient()
+ >>> client.timing('some.time', 500)
"""
stats = {}
stats[stat] = "%d|ms" % time
- Statsd.send(stats, sample_rate)
+ self.send(stats, sample_rate)
- @staticmethod
- def increment(stats, sample_rate=1):
+ def increment(self, stats, sample_rate=1):
"""
Increments one or more stats counters
- >>> Statsd.increment('some.int')
- >>> Statsd.increment('some.int',0.5)
+ >>> client = StatsdClient()
+ >>> client.increment('some.int')
+ >>> client.increment('some.int', 0.5)
"""
- Statsd.update_stats(stats, 1, sample_rate)
+ self.update_stats(stats, 1, sample_rate)
- @staticmethod
- def decrement(stats, sample_rate=1):
+ def decrement(self, stats, sample_rate=1):
"""
Decrements one or more stats counters
- >>> Statsd.decrement('some.int')
+ >>> client = StatsdClient()
+ >>> client.decrement('some.int')
"""
- Statsd.update_stats(stats, -1, sample_rate)
+ self.update_stats(stats, -1, sample_rate)
- @staticmethod
- def update_stats(stats, delta=1, sampleRate=1):
+ def update_stats(self, stats, delta=1, sampleRate=1):
"""
Updates one or more stats counters by arbitrary amounts
- >>> Statsd.update_stats('some.int',10)
+ >>> client = StatsdClient()
+ >>> client.update_stats('some.int', 10)
"""
- if (type(stats) is not list):
+ if isinstance(stats, list):
stats = [stats]
data = {}
for stat in stats:
data[stat] = "%s|c" % delta
+ self.send(data, sampleRate)
- Statsd.send(data, sampleRate)
-
- @staticmethod
- def send(data, sample_rate=1):
+ def send(self, data, sample_rate=1):
"""
Squirt the metrics over UDP
"""
- try:
- import local_settings as settings
- host = settings.statsd_host
- port = settings.statsd_port
- addr=(host, port)
- except:
- exit(1)
-
sampled_data = {}
- if(sample_rate < 1):
- import random
- if random.random() <= sample_rate:
- for stat in data.keys():
- value = data[stat]
+ if (sample_rate < 1):
+ if random() <= sample_rate:
+ for stat, value in data.items():
sampled_data[stat] = "%s|@%s" %(value, sample_rate)
else:
- sampled_data=data
+ sampled_data = data
- from socket import socket, AF_INET, SOCK_DGRAM
udp_sock = socket(AF_INET, SOCK_DGRAM)
try:
- for stat in sampled_data.keys():
- value = sampled_data[stat]
+ for stat, value in sampled_data.items():
send_data = "%s:%s" % (stat, value)
udp_sock.sendto(send_data, self.addr)
- except:
+ except Exception:
import sys
- from pprint import pprint
- print "Unexpected error:", pprint(sys.exc_info())
- pass # we don't care
+ import traceback
+ print >>sys.stderr, "Unexpected error: ", traceback.format_exc()
+ return False
+ return True
if __name__=="__main__":
Something went wrong with that request. Please try again.