Add subsection for using a decorator in python manual tracing doc (#1403)
* Add subsection for using a decorator; * ing
This commit is contained in:
parent
440510defe
commit
6d8c21fd94
|
@ -43,9 +43,10 @@ With a call to `get_tracer`, you can create spans.
|
||||||
To create a span, you'll typically want it to be started as the current span.
|
To create a span, you'll typically want it to be started as the current span.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
def do_work():
|
||||||
with tracer.start_as_current_span("span-name") as span:
|
with tracer.start_as_current_span("span-name") as span:
|
||||||
# do some work that 'span' will track
|
# do some work that 'span' will track
|
||||||
|
print("doing some work...")
|
||||||
# When the 'with' block goes out of scope, 'span' is closed for you
|
# When the 'with' block goes out of scope, 'span' is closed for you
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -58,13 +59,14 @@ If you have a distinct sub-operation you'd like to track as a part of another
|
||||||
one, you can create spans to represent the relationship:
|
one, you can create spans to represent the relationship:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
def do_work():
|
||||||
with tracer.start_as_current_span("parent") as parent:
|
with tracer.start_as_current_span("parent") as parent:
|
||||||
# do some work that 'parent' tracks
|
# do some work that 'parent' tracks
|
||||||
|
print("doing some work...")
|
||||||
# Create a nested span to track nested work
|
# Create a nested span to track nested work
|
||||||
with tracer.start_as_current_span("child") as child:
|
with tracer.start_as_current_span("child") as child:
|
||||||
# do some work that 'child' tracks
|
# do some work that 'child' tracks
|
||||||
|
print("doing some nested work...")
|
||||||
# the nested span is closed when it's out of scope
|
# the nested span is closed when it's out of scope
|
||||||
|
|
||||||
# This span is also closed when it goes out of scope
|
# This span is also closed when it goes out of scope
|
||||||
|
@ -73,6 +75,27 @@ with tracer.start_as_current_span("parent") as parent:
|
||||||
When you view spans in a trace visualization tool, `child` will be tracked as a
|
When you view spans in a trace visualization tool, `child` will be tracked as a
|
||||||
nested span under `parent`.
|
nested span under `parent`.
|
||||||
|
|
||||||
|
## Creating spans with decorators
|
||||||
|
|
||||||
|
It's common to have a single span track the execution of an entire function. In
|
||||||
|
that scenario, there is a decorator you can use to reduce code:
|
||||||
|
|
||||||
|
```python
|
||||||
|
@tracer.start_as_current_span("do_work")
|
||||||
|
def do_work():
|
||||||
|
print("doing some work...")
|
||||||
|
```
|
||||||
|
|
||||||
|
Use of the decorator is equivalent to creating the span inside `do_work()` and
|
||||||
|
ending it when `do_work()` is finished.
|
||||||
|
|
||||||
|
To use the decorator, you must have a `tracer` instance available global to your
|
||||||
|
function declaration.
|
||||||
|
|
||||||
|
If you need to add [attributes](#add-attributes-to-a-span),
|
||||||
|
[events](#adding-events), or [links](#adding-links) then it's less convenient to
|
||||||
|
use a decorator.
|
||||||
|
|
||||||
## Get the current span
|
## Get the current span
|
||||||
|
|
||||||
Sometimes it's helpful to access whatever the current span is at a point in time
|
Sometimes it's helpful to access whatever the current span is at a point in time
|
||||||
|
|
Loading…
Reference in New Issue