-
Notifications
You must be signed in to change notification settings - Fork 477
Add Trace.wrap decorator #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9bf2df8
c9c2bef
845eddc
9564ccb
f30c4e0
6fa5c74
78968d9
089021d
c73e1b3
44d4f72
8faed3b
ff0fbef
a582a89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
|
|
||
| import functools | ||
| import logging | ||
| import threading | ||
|
|
||
|
|
@@ -63,6 +63,53 @@ def configure(self, enabled=None, hostname=None, port=None, sampler=None): | |
| if sampler is not None: | ||
| self.sampler = sampler | ||
|
|
||
| def wrap(self, name=None, service=None, resource=None, span_type=None): | ||
| """A decorator used to trace an entire function. | ||
|
|
||
| :param str name: the name of the operation being traced. If not set, | ||
| defaults to the fully qualified function name. | ||
| :param str service: the name of the service being traced. If not set, | ||
| it will inherit the service from it's parent. | ||
| :param str resource: an optional name of the resource being tracked. | ||
| :param str span_type: an optional operation type. | ||
|
|
||
| >>> @tracer.wrap('my.wrapped.function', service='my.service') | ||
| def run(): | ||
| return 'run' | ||
| >>> @tracer.wrap() # name will default to 'execute' if unset | ||
| def execute(): | ||
| return 'executed' | ||
|
|
||
| You can access the parent span using `tracer.current_span()` to set | ||
| tags: | ||
|
|
||
| >>> @tracer.wrap() | ||
| def execute(): | ||
| span = tracer.current_span() | ||
| span.set_tag('a', 'b') | ||
|
|
||
| You can also create more spans within a traced function. These spans | ||
| will be children of the decorator's span: | ||
|
|
||
| >>> @tracer.wrap('parent') | ||
| def parent_function(): | ||
| with tracer.trace('child'): | ||
| pass | ||
| """ | ||
|
|
||
| def wrap_decorator(func): | ||
| if name is None: | ||
| span_name = '{}.{}'.format(func.__module__, func.__name__) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (comment got buried so reposting): It's worth discussing whether we want to include the module name as well as the function name here. Including the module name is good because we can tell exactly which function is being traced. Including the module name is bad because we may get really long span names, and our interface doesn't display these well at this time. I'm learning towards including the module name.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i really want to nudge people towards short spans. |
||
| else: | ||
| span_name = name | ||
|
|
||
| @functools.wraps(func) | ||
| def func_wrapper(*args, **kwargs): | ||
| with self.trace(span_name, service=service, resource=resource, span_type=span_type): | ||
| func(*args, **kwargs) | ||
| return func_wrapper | ||
| return wrap_decorator | ||
|
|
||
| def trace(self, name, service=None, resource=None, span_type=None): | ||
| """Return a span that will trace an operation called `name`. | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's worth discussing whether we want to include the module name as well as the function name here.
Including the module name is good because we can tell exactly which function is being traced.
Including the module name is bad because we may get really long span names, and our interface doesn't display these well at this time.
I'm learning towards including the module name.