Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Opentracing doc update #5776

Merged
merged 6 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/5776.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove references to trace_deferred in opetnracing docs.
JorikSchellekens marked this conversation as resolved.
Show resolved Hide resolved
75 changes: 44 additions & 31 deletions synapse/logging/opentracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
an optional dependency. This does however limit the number of modifiable spans
at any point in the code to one. From here out references to `opentracing`
in the code snippets refer to the Synapses module.
Most methods provided in the module have a direct correlation to those provided
by opentracing. Refer to docs there for a more in-depth documentation on some of
the args and methods.

Tracing
-------
Expand All @@ -54,9 +57,9 @@

.. code-block:: python

from synapse.logging.opentracing import start_active_span
import synapse.logging.opentracing as opentracing

with start_active_span("operation name"):
with opentracing.start_active_span("operation name"):
# Do something we want to tracer

Forgetting to enter or exit a scope will result in some mysterious and grievous log
Expand All @@ -68,52 +71,62 @@
Tracing functions
-----------------

Functions can be easily traced using decorators. There is a decorator for
'normal' function and for functions which are actually deferreds. The name of
Functions can be easily traced using decorators. The name of
the function becomes the operation name for the span.

.. code-block:: python

from synapse.logging.opentracing import trace, trace_deferred
import synapse.logging.opentracing as opentracing

# Start a span using 'normal_function' as the operation name
@trace
def normal_function(*args, **kwargs):
# Start a span using 'interesting_function' as the operation name
@opentracing.trace
def interesting_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_deferred
@defer.inlineCallbacks
def deferred_function(*args, **kwargs):
# We start
yield we_wait
# we finish
return something_usual_and_useful

Operation names can be explicitly set for functions by using
``trace_using_operation_name`` and
``trace_deferred_using_operation_name``
``trace_using_operation_name``

.. code-block:: python

from synapse.logging.opentracing import (
trace_using_operation_name,
trace_deferred_using_operation_name
)
import synapse.logging.opentracing as opentracing

@trace_using_operation_name("A *much* better operation name")
def normal_function(*args, **kwargs):
@opentracing.trace_using_operation_name("A *much* better operation name")
def interesting_badly_named_function(*args, **kwargs):
# Does all kinds of cool and expected things
return something_usual_and_useful

@trace_deferred_using_operation_name("Another exciting operation name!")
@defer.inlineCallbacks
def deferred_function(*args, **kwargs):
# We start
yield we_wait
# we finish
return something_usual_and_useful
Setting Tags
------------

To set a tag on the active span do

.. code-block:: python

import synapse.logging.opentracing as opentracing

opentracing.set_tag(tag_name, tag_value)
JorikSchellekens marked this conversation as resolved.
Show resolved Hide resolved

There's a convenient decorator to tag all the args of the method. It uses
inspection in order to use the formal parameter names prefixed with 'ARG_' as
tag names. It uses kwarg names as tag names without the prefix.

.. code-block:: python

import synapse.logging.opentracing as opentracing

@opentracing.tag_args
def set_fates(clotho, lachesis, atropos, father="Zues", mother="Themis"):
pass

set_fates("the story", "the end", "the act")
# This will have the following tags
# - ARG_clotho: "the story"
# - ARG_lachesis: "the end"
# - ARG_atropos: "the act"
# - father: "Zues"
# - mother: "Themis"

Contexts and carriers
---------------------
Expand Down