Skip to content

Commit 51ed457

Browse files
authored
Add coding examples for context (open-telemetry#1165)
1 parent cdd9fd2 commit 51ed457

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Basic Context
2+
=============
3+
4+
These examples show how context is propagated through Spans in OpenTelemetry.
5+
6+
There are three different examples:
7+
8+
* implicit_context: Shows how starting a span implicitly creates context.
9+
10+
* child_context: Shows how context is propagated through child spans.
11+
12+
* async_context: Shows how context can be shared in another coroutine.
13+
14+
The source files of these examples are available :scm_web:`here <docs/examples/basic_context/>`.
15+
16+
Installation
17+
------------
18+
19+
.. code-block:: sh
20+
21+
pip install opentelemetry-api
22+
pip install opentelemetry-sdk
23+
24+
Run the Example
25+
---------------
26+
27+
.. code-block:: sh
28+
29+
python <example_name>.py
30+
31+
The output will be shown in the console.
32+
33+
Useful links
34+
------------
35+
36+
- OpenTelemetry_
37+
- :doc:`../../api/trace`
38+
39+
.. _OpenTelemetry: https://github.com/open-telemetry/opentelemetry-python/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import asyncio
16+
17+
from opentelemetry import baggage, trace
18+
from opentelemetry.sdk.trace import TracerProvider
19+
20+
trace.set_tracer_provider(TracerProvider())
21+
tracer = trace.get_tracer(__name__)
22+
23+
loop = asyncio.get_event_loop()
24+
25+
26+
async def async_span(span):
27+
with tracer.use_span(span):
28+
ctx = baggage.set_baggage("foo", "bar")
29+
return ctx
30+
31+
32+
async def main():
33+
span = tracer.start_span(name="span")
34+
ctx = await async_span(span)
35+
print(baggage.get_all(context=ctx))
36+
37+
38+
loop.run_until_complete(main())
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from opentelemetry import baggage, trace
16+
17+
tracer = trace.get_tracer(__name__)
18+
19+
global_ctx = baggage.set_baggage("context", "global")
20+
with tracer.start_as_current_span(name="root span") as root_span:
21+
parent_ctx = baggage.set_baggage("context", "parent")
22+
with tracer.start_as_current_span(
23+
name="child span", context=parent_ctx
24+
) as child_span:
25+
child_ctx = baggage.set_baggage("context", "child")
26+
27+
print(baggage.get_baggage("context", global_ctx))
28+
print(baggage.get_baggage("context", parent_ctx))
29+
print(baggage.get_baggage("context", child_ctx))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from opentelemetry import baggage, trace
16+
from opentelemetry.sdk.trace import TracerProvider
17+
18+
trace.set_tracer_provider(TracerProvider())
19+
tracer = trace.get_tracer(__name__)
20+
21+
with tracer.start_span(name="root span") as root_span:
22+
ctx = baggage.set_baggage("foo", "bar")
23+
24+
print("Global context baggage: {}".format(baggage.get_all()))
25+
print("Span context baggage: {}".format(baggage.get_all(context=ctx)))

docs/faq-and-cookbook.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,21 @@ Getting and modifying a span
2525
2626
current_span = trace.get_current_span()
2727
current_span.set_attribute("hometown", "seattle")
28+
29+
Capturing baggage at different contexts
30+
---------------------------------------
31+
32+
.. code-block:: python
33+
34+
from opentelemetry import trace
35+
36+
tracer = trace.get_tracer(__name__)
37+
with tracer.start_as_current_span(name="root span") as root_span:
38+
parent_ctx = baggage.set_baggage("context", "parent")
39+
with tracer.start_as_current_span(
40+
name="child span", context=parent_ctx
41+
) as child_span:
42+
child_ctx = baggage.set_baggage("context", "child")
43+
44+
print(baggage.get_baggage("context", parent_ctx))
45+
print(baggage.get_baggage("context", child_ctx))

0 commit comments

Comments
 (0)