Skip to content

Commit 8dd5cea

Browse files
authored
[opentracer] Documentation (#517)
* add opentracing documentation basics
1 parent 1d76653 commit 8dd5cea

File tree

4 files changed

+168
-4
lines changed

4 files changed

+168
-4
lines changed

ddtrace/opentracer/README.rst

Whitespace-only changes.

ddtrace/opentracer/tracer.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ class Tracer(opentracing.Tracer):
3434
"""A wrapper providing an OpenTracing API for the Datadog tracer."""
3535

3636
def __init__(self, service_name=None, config=None, scope_manager=None):
37+
"""Initialize a new Datadog opentracer.
38+
39+
:param service_name: (optional) the name of the service that this
40+
tracer will be used with. Note if not provided, a service name will
41+
try to be determined based off of ``sys.argv``. If this fails a
42+
:class:`ddtrace.settings.ConfigException` will be raised.
43+
:param config: (optional) a configuration object to specify additional
44+
options. See the documentation for further information.
45+
:param scope_manager: (optional) the scope manager for this tracer to
46+
use. The available managers are listed in the Python OpenTracing repo
47+
here: https://github.com/opentracing/opentracing-python#scope-managers.
48+
If ``None`` is provided, defaults to
49+
:class:`opentracing.scope_managers.ThreadLocalScopeManager`.
50+
"""
3751
# Merge the given config with the default into a new dict
3852
config = config or {}
3953
self._config = merge_dicts(DEFAULT_CONFIG, config)
@@ -80,20 +94,24 @@ def start_active_span(self, operation_name, child_of=None, references=None,
8094
tags=None, start_time=None, ignore_active_span=False,
8195
finish_on_close=True):
8296
"""Returns a newly started and activated `Scope`.
83-
The returned `Scope` supports with-statement contexts. For example:
97+
The returned `Scope` supports with-statement contexts. For example::
98+
8499
with tracer.start_active_span('...') as scope:
85100
scope.span.set_tag('http.method', 'GET')
86101
do_some_work()
87102
# Span.finish() is called as part of Scope deactivation through
88103
# the with statement.
104+
89105
It's also possible to not finish the `Span` when the `Scope` context
90-
expires:
106+
expires::
107+
91108
with tracer.start_active_span('...',
92109
finish_on_close=False) as scope:
93110
scope.span.set_tag('http.method', 'GET')
94111
do_some_work()
95112
# Span.finish() is not called as part of Scope deactivation as
96113
# `finish_on_close` is `False`.
114+
97115
:param operation_name: name of the operation represented by the new
98116
span from the perspective of the current service.
99117
:param child_of: (optional) a Span or SpanContext instance representing
@@ -111,7 +129,7 @@ def start_active_span(self, operation_name, child_of=None, references=None,
111129
the current active `Scope` and creates a root `Span`.
112130
:param finish_on_close: whether span should automatically be finished
113131
when `Scope.close()` is called.
114-
:return: a `Scope`, already registered via the `ScopeManager`.
132+
:return: a `Scope`, already registered via the `ScopeManager`.
115133
"""
116134
otspan = self.start_span(
117135
operation_name=operation_name,
@@ -131,13 +149,17 @@ def start_span(self, operation_name=None, child_of=None, references=None,
131149
"""Starts and returns a new Span representing a unit of work.
132150
133151
Starting a root Span (a Span with no causal references)::
152+
134153
tracer.start_span('...')
135154
136155
Starting a child Span (see also start_child_span())::
156+
137157
tracer.start_span(
138158
'...',
139159
child_of=parent_span)
160+
140161
Starting a child Span in a more verbose way::
162+
141163
tracer.start_span(
142164
'...',
143165
references=[opentracing.child_of(parent_span)])

ddtrace/span.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def __init__(
5454
"""
5555
Create a new span. Call `finish` once the traced operation is over.
5656
57-
:param Tracer tracer: the tracer that will submit this span when finished.
57+
:param ddtrace.Tracer tracer: the tracer that will submit this span when
58+
finished.
5859
:param str name: the name of the traced operation.
5960
6061
:param str service: the service name

docs/index.rst

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,147 @@ following flag or environment variable::
449449

450450
$ PYTHONWARNINGS=all python app.py
451451

452+
.. _opentracing:
453+
454+
OpenTracing
455+
-----------
456+
457+
Datadog OpenTracing Tracer
458+
~~~~~~~~~~~~~~~~~~~~~~~~~~
459+
460+
The Datadog opentracer provides an OpenTracing-compatible API to the Datadog
461+
tracer so that you can use the Datadog tracer in your OpenTracing-compatible
462+
applications.
463+
464+
Installation
465+
~~~~~~~~~~~~
466+
467+
To include OpenTracing dependencies in your project with `ddtrace`, ensure you
468+
have the following in `setup.py`::
469+
470+
install_requires=[
471+
"ddtrace[opentracing]",
472+
],
473+
474+
Configuration
475+
~~~~~~~~~~~~~
476+
477+
The OpenTracing convention for initializing a tracer is to define an
478+
initiliazation method that will configure and instantiate a new tracer and
479+
overwrite the global `opentracing.tracer` reference.
480+
481+
Typically this method looks something like::
482+
483+
from ddtrace.opentracer import Tracer, set_global_tracer
484+
485+
def init_tracer(service_name):
486+
"""
487+
Initialize a new Datadog opentracer and set it as the
488+
global tracer.
489+
490+
This overwrites the opentracing.tracer reference.
491+
"""
492+
config = {
493+
'agent_hostname': 'localhost',
494+
'agent_port': 8126,
495+
}
496+
tracer = Tracer(service_name, config=config)
497+
set_global_tracer(tracer)
498+
return tracer
499+
500+
**Opentracer API**
501+
502+
.. autoclass:: ddtrace.opentracer.Tracer
503+
:members:
504+
:special-members: __init__
505+
506+
507+
The Datadog opentracer can be configured via the `config` dictionary parameter
508+
which accepts the following described fields.
509+
510+
+---------------------+---------------------------------------------------------+---------------+
511+
| Configuration Key | Description | Default Value |
512+
+=====================+=========================================================+===============+
513+
| `enabled` | enable or disable the tracer | `True` |
514+
+---------------------+---------------------------------------------------------+---------------+
515+
| `debug` | enable debug logging | `False` |
516+
+---------------------+---------------------------------------------------------+---------------+
517+
| `agent_hostname` | hostname of the Datadog agent to use | `localhost` |
518+
+---------------------+---------------------------------------------------------+---------------+
519+
| `agent_port` | port the Datadog agent is listening on | `8126` |
520+
+---------------------+---------------------------------------------------------+---------------+
521+
| `global_tags` | tags that will be applied to each span | `{}` |
522+
+---------------------+---------------------------------------------------------+---------------+
523+
| `sampler` | see `Sampling`_ | `AllSampler` |
524+
+---------------------+---------------------------------------------------------+---------------+
525+
| `priority_sampling` | see `Priority Sampling`_ | `False` |
526+
+---------------------+---------------------------------------------------------+---------------+
527+
| `settings` | see `Advanced Usage`_ | `{}` |
528+
+---------------------+---------------------------------------------------------+---------------+
529+
530+
531+
Usage
532+
~~~~~
533+
534+
**Manual tracing**
535+
536+
To explicitly trace::
537+
538+
import time
539+
import opentracing
540+
from ddtrace.opentracer import Tracer, set_global_tracer
541+
542+
def init_tracer(service_name):
543+
config = {
544+
'agent_hostname': 'localhost',
545+
'agent_port': 8126,
546+
}
547+
tracer = Tracer(service_name, config=config)
548+
set_global_tracer(tracer)
549+
return tracer
550+
551+
def my_operation():
552+
span = opentracing.tracer.start_span('my_operation_name')
553+
span.set_tag('my_interesting_tag', 'my_interesting_value')
554+
time.sleep(0.05)
555+
span.finish()
556+
557+
init_tracer('my_service_name')
558+
my_operation()
559+
560+
**Context Manager Tracing**
561+
562+
To trace a function using the span context manager::
563+
564+
import time
565+
import opentracing
566+
from ddtrace.opentracer import Tracer, set_global_tracer
567+
568+
def init_tracer(service_name):
569+
config = {
570+
'agent_hostname': 'localhost',
571+
'agent_port': 8126,
572+
}
573+
tracer = Tracer(service_name, config=config)
574+
set_global_tracer(tracer)
575+
return tracer
576+
577+
def my_operation():
578+
with opentracing.tracer.start_span('my_operation_name') as span:
579+
span.set_tag('my_interesting_tag', 'my_interesting_value')
580+
time.sleep(0.05)
581+
582+
init_tracer('my_service_name')
583+
my_operation()
584+
585+
See our tracing trace-examples_ repository for concrete, runnable examples of
586+
the Datadog opentracer.
587+
588+
.. _trace-examples: https://github.com/DataDog/trace-examples/tree/master/python
589+
590+
See also the `Python OpenTracing`_ repository for usage of the tracer.
591+
592+
.. _Python OpenTracing: https://github.com/opentracing/opentracing-python
452593

453594
Advanced Usage
454595
--------------

0 commit comments

Comments
 (0)