Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make sub-events auto prefixed

  • Loading branch information...
commit 4a33499cdff4f9fe2afbe757a65561843d4cafd4 1 parent 3060736
@rhettg authored
View
8 README.md
@@ -42,7 +42,7 @@ are related to the parent event and can be joined together in post-processing by
For example, inside some application code (in `do_stuff()` above), you might execute some sql queries.
def execute(cursor, query, args):
- with ziggy.Context('request.sql'):
+ with ziggy.Context('sql'):
ziggy.set('query', query)
with ziggy.timeit('query_time'):
res = cursor.execute(query, args)
@@ -50,7 +50,7 @@ For example, inside some application code (in `do_stuff()` above), you might exe
return res
Each SQL query would then be logged as a seperate event. However, each event
-will have the unique id provided by the parent `request` context.
+will have the unique id provided by the parent `request` context. The name of the context will become `request.sql`.
You can provide you're own id or allow ziggy to autogenerate one for the top-level context.
@@ -58,12 +58,12 @@ Ziggy also provides the ability to do sampling. This means only a set
percentage of generate events will actually be logged. You can choose sampling
based on any level of the context:
- with ziggy.Context('request.memcache', sample=('request', 0.25)):
+ with ziggy.Context('memcache', sample=('..', 0.25)):
ziggy.set('key', key)
client.set(key, value)
In the above example, only 25% of requests will include the memcache data. If
-the sample argument where `('request.memcache', 0.25)` then 25% of all memcache
+the sample argument where `('memcache', 0.25)` then 25% of all memcache
events would be logged.
### Configuration
View
1  requirements.txt
@@ -3,3 +3,4 @@ pep8
pyflakes
pyzmq
bson
+tornado
View
5 tests/context_test.py
@@ -22,7 +22,8 @@ def test(self):
class NestedIDTestCase(TestCase):
def test(self):
with context.Context('test', 5):
- with context.Context('test.foo') as c:
+ with context.Context('foo') as c:
+ assert_equal(c.name, 'test.foo')
assert_equal(c.id, 5)
@@ -53,7 +54,7 @@ def test(self):
with parent_context:
sub_enabled = []
for _ in range(10):
- context = ziggy.Context('test.sub', sample=('test', 0.25))
+ context = ziggy.Context('sub', sample=('..', 0.25))
sub_enabled.append(1 if context.enabled else 0)
enabled.append(1 if context.enabled else 0)
assert all(sub_enabled) or not any(sub_enabled)
View
14 ziggy/context.py
@@ -26,13 +26,17 @@
class Context(object):
__slots__ = ["name", "data", "id", "_writable", "start_time", "_sample_checks", "enabled"]
def __init__(self, type_name, id=None, sample=None):
- self.name = type_name
+ parent_ctx = current_context()
+
+ if parent_ctx:
+ self.name = ".".join((parent_ctx.name, type_name))
+ else:
+ self.name = type_name
+
self.data = {}
self.start_time = time.time()
self._sample_checks = {}
- parent_ctx = _get_context(utils.parse_key(type_name)[:-1])
-
if id is not None:
self.id = id
elif parent_ctx:
@@ -47,8 +51,10 @@ def __init__(self, type_name, id=None, sample=None):
self.enabled = False
elif sample:
sample_name, rate = sample
- if sample_name == type_name:
+ if sample_name == type_name or sample_name == '.':
self.enabled = bool(random.random() <= rate)
+ elif parent_ctx and sample_name == '..':
+ self.enabled = parent_ctx.sampled_for(type_name, rate)
else:
self.enabled = _get_context(sample_name).sampled_for(type_name, rate)
else:
View
5 ziggy/tornado_utils.py
@@ -14,9 +14,12 @@
"""
import functools
+import logging
import traceback
import types
+log = logging.getLogger(__name__)
+
import tornado.web
import tornado.gen
import tornado.simple_httpclient
@@ -147,7 +150,7 @@ def run(self):
class AsyncHTTPClient(tornado.simple_httpclient.SimpleAsyncHTTPClient):
def __init__(self, *args, **kwargs):
- self.ziggy_name = 'tornado.httpclient'
+ self.ziggy_name = 'httpclient'
return super(AsyncHTTPClient, self).__init__(*args, **kwargs)
def fetch(self, request, callback, **kwargs):
Please sign in to comment.
Something went wrong with that request. Please try again.