Fixing issues in generator, unit tests, and documentation
Change-Id: I2ed96e1592eb1f62fce63d5607831903323b1826 Fixed some pep8 issues Change-Id: I31cdc81469db4a5ed3684b5d22daa91a56ff7688
This commit is contained in:
parent
c7dfeb7e75
commit
79e3609be8
22
README.md
22
README.md
|
@ -1,4 +1,4 @@
|
|||
monasca-statsd
|
||||
import monascastatsd.monasca_statsdmonasca-statsd
|
||||
================
|
||||
|
||||
A Monasca-Statsd Python client.
|
||||
|
@ -16,30 +16,32 @@ First install the library with `pip` or `easy_install`
|
|||
|
||||
Then start instrumenting your code:
|
||||
|
||||
``` python
|
||||
```
|
||||
# Import the module.
|
||||
from monascastatsd import monascastatsd
|
||||
import monascastatsd.monasca_statsd
|
||||
|
||||
mstatsd = monascastatsd.monasca_statsd.MonascaStatsd()
|
||||
|
||||
# Optionally, configure the host and port if you're running Statsd on a
|
||||
# non-standard port.
|
||||
monascastatsd.connect('localhost', 8125)
|
||||
mstatsd.connect('localhost', 8125)
|
||||
|
||||
# Increment a counter.
|
||||
monascastatsd.increment('page.views')
|
||||
mstatsd.increment('page.views')
|
||||
|
||||
# Record a gauge 50% of the time.
|
||||
monascastatsd.gauge('users.online', 123, sample_rate=0.5)
|
||||
mstatsd.gauge('users.online', 123, sample_rate=0.5)
|
||||
|
||||
# Sample a histogram.
|
||||
monascastatsd.histogram('file.upload.size', 1234)
|
||||
mstatsd.histogram('file.upload.size', 1234)
|
||||
|
||||
# Time a function call.
|
||||
@monascastatsd.timed('page.render')
|
||||
@mstatsd.timed('page.render')
|
||||
def render_page():
|
||||
# Render things ...
|
||||
|
||||
# Tag a metric.
|
||||
monascastatsd.histogram('query.time', 10, dimensions = {"version": "1.0", "environment": "dev"})
|
||||
# Add a dimension to a metric.
|
||||
mstatsd.histogram('query.time', 10, dimensions = {'version': '1.0', 'environment': 'dev'})
|
||||
```
|
||||
Documentation
|
||||
-------------
|
||||
|
|
|
@ -56,15 +56,19 @@ class MonascaStatsdGenerator(object):
|
|||
mstatsd.histogram('file.upload.size',
|
||||
random.randrange(1, 100),
|
||||
dimensions={'Version': '1.0'})
|
||||
|
||||
@mstatsd.timed('config_db_time',
|
||||
dimensions={'db_name': 'mydb'})
|
||||
def time_db():
|
||||
time.sleep(0.5)
|
||||
time_db()
|
||||
|
||||
# Send some regular statsd messages
|
||||
connection = statsd.Connection()
|
||||
counter = statsd.Counter('statsd_generator')
|
||||
counter += 1
|
||||
gauge = statsd.Gauge('statsd_generator')
|
||||
gauge.send('cpu_percent',
|
||||
random.uniform(1.0, 100.0))
|
||||
average = statsd.Average('statsd_generator', connection)
|
||||
average.send('cpu_avg', random.randrange(1, 100))
|
||||
print("Completed iteration " + str(index) +
|
||||
". Sleeping for " + str(self.delay) + " seconds...")
|
||||
time.sleep(self.delay)
|
||||
|
|
|
@ -86,25 +86,25 @@ class TestMonStatsd(unittest.TestCase):
|
|||
|
||||
def test_gauge_with_dimensions(self):
|
||||
self.monascastatsd.gauge('gt', 123.4,
|
||||
dimensions=['country:china',
|
||||
'age:45',
|
||||
'color:blue'])
|
||||
self.assertEqual("gt:123.4|g|#[" +
|
||||
"'country:china', " +
|
||||
"'age:45', " +
|
||||
"'color:blue']",
|
||||
dimensions={'country': 'china',
|
||||
'age': 45,
|
||||
'color': 'blue'})
|
||||
self.assertEqual("gt:123.4|g|#{" +
|
||||
"'color': 'blue', " +
|
||||
"'country': 'china', " +
|
||||
"'age': 45}",
|
||||
self.recv())
|
||||
|
||||
def test_counter_with_dimensions(self):
|
||||
self.monascastatsd.increment('ct',
|
||||
dimensions=['country:canada',
|
||||
'color:red'])
|
||||
self.assertEqual("ct:1|c|#['country:canada', 'color:red']",
|
||||
dimensions={'country': 'canada',
|
||||
'color': 'red'})
|
||||
self.assertEqual("ct:1|c|#{'color': 'red', 'country': 'canada'}",
|
||||
self.recv())
|
||||
|
||||
def test_histogram_with_dimensions(self):
|
||||
self.monascastatsd.histogram('h', 1, dimensions=['color:red'])
|
||||
self.assertEqual("h:1|h|#['color:red']", self.recv())
|
||||
self.monascastatsd.histogram('h', 1, dimensions={'color': 'red'})
|
||||
self.assertEqual("h:1|h|#{'color': 'red'}", self.recv())
|
||||
|
||||
def test_sample_rate(self):
|
||||
self.monascastatsd.increment('c', sample_rate=0)
|
||||
|
@ -120,14 +120,14 @@ class TestMonStatsd(unittest.TestCase):
|
|||
for _ in range(100):
|
||||
self.monascastatsd.gauge('gst',
|
||||
23,
|
||||
dimensions=['status:sampled'],
|
||||
dimensions={'status': 'sampled'},
|
||||
sample_rate=0.9)
|
||||
|
||||
def test_samples_with_dimensions(self):
|
||||
for _ in range(100):
|
||||
self.monascastatsd.gauge('gst',
|
||||
23,
|
||||
dimensions=['status:sampled'],
|
||||
dimensions={'status': 'sampled'},
|
||||
sample_rate=0.9)
|
||||
self.assertEqual('gst:23|g|@0.9|#status:sampled')
|
||||
|
||||
|
|
Loading…
Reference in New Issue