1
0

Better decorator names

This commit is contained in:
Jorik Schellekens
2019-07-17 13:55:53 +01:00
parent 854d75c41b
commit d67376f7fc
2 changed files with 27 additions and 22 deletions

View File

@@ -116,13 +116,13 @@ function becomes the operation name for the span.
.. code-block:: python
# Start a span using 'normal_function' as the operation name
@trace_function
@trace
def normal_function(*args, **kwargs):
# Does all kinds of cool and expected things
return something_usual_and_useful
# Start a span using 'deferred_function' as the operation name
@trace_defered_function
@trace_deferred
# Yes, there is a typo in the lib. I will fix this
def deferred_function(*args, **kwargs):
# We start
@@ -131,17 +131,17 @@ function becomes the operation name for the span.
defer.returnValue(something_usual_and_useful)
Operation names can be explicitely set for functions by using
``trace_function_using_operation_name`` and
``trace_defered_function_using_operation_name``
``trace_using_operation_name`` and
``trace_deferred_using_operation_name``
.. code-block:: python
@trace_function_using_operation_name("A *much* better operation name")
@trace_using_operation_name("A *much* better operation name")
def normal_function(*args, **kwargs):
# Does all kinds of cool and expected things
return something_usual_and_useful
@trace_defered_function_using_operation_name("An operation name that fixes the typo!")
@trace_deferred_using_operation_name("An operation name that fixes the typo!")
# Yes, there is a typo in the lib. I will fix this
def deferred_function(*args, **kwargs):
# We start

View File

@@ -424,7 +424,7 @@ def extract_text_map(carrier):
return opentracing.tracer.extract(opentracing.Format.TEXT_MAP, carrier)
def trace_defered_function(func):
def trace_deferred(func):
"""Decorator to trace a defered function. Sets the operation name to that of the
function's."""
@@ -457,50 +457,55 @@ def trace_servlet(servlet_name, func):
def trace_defered_function(func):
@wraps(func)
@defer.inlineCallbacks
def _trace_defered_function_inner(self, *args, **kwargs):
def _trace_deferred_inner(self, *args, **kwargs):
with start_active_span(func.__name__):
r = yield func(self, *args, **kwargs)
defer.returnValue(r)
return _trace_defered_function_inner
return _trace_deferred_inner
def trace_defered_function_using_operation_name(name):
def trace_defered_function(func):
def trace_deferred_using_operation_name(name):
"""Decorator to trace a defered function. Explicitely sets the operation_name to name"""
def trace_deferred(func):
@wraps(func)
@defer.inlineCallbacks
def _trace_defered_function_inner(self, *args, **kwargs):
def _trace_deferred_inner(self, *args, **kwargs):
# Start scope
with start_active_span(name):
r = yield func(self, *args, **kwargs)
defer.returnValue(r)
return _trace_defered_function_inner
return _trace_deferred_inner
return trace_defered_function
return trace_deferred
def trace_function(func):
def trace(func):
"""Decorator to trace a normal function. Sets the operation name to that of the
function's."""
@wraps(func)
def _trace_function_inner(self, *args, **kwargs):
def _trace_inner(self, *args, **kwargs):
with start_active_span(func.__name__):
return func(self, *args, **kwargs)
return _trace_function_inner
return _trace_inner
def trace_function_using_operation_name(operation_name):
def trace_using_operation_name(operation_name):
"""Decorator to trace a function. Explicitely sets the operation_name to name"""
def trace_function(func):
def trace(func):
@wraps(func)
def _trace_function_inner(self, *args, **kwargs):
def _trace_inner(self, *args, **kwargs):
with start_active_span(operation_name):
return func(self, *args, **kwargs)
return _trace_function_inner
return _trace_inner
return trace_function
return trace
def tag_args(func):