From ffe9d4e1d9822f51e48d4d05896434fcd0cea11a Mon Sep 17 00:00:00 2001 From: Amos Law Date: Fri, 25 Sep 2020 09:41:55 -0700 Subject: [PATCH 1/3] Add coding examples for context --- docs/examples/basic_context/README.rst | 39 +++++++++++++++++++ docs/examples/basic_context/async_context.py | 38 ++++++++++++++++++ docs/examples/basic_context/child_context.py | 27 +++++++++++++ .../basic_context/implicit_context.py | 25 ++++++++++++ docs/faq-and-cookbook.rst | 16 ++++++++ 5 files changed, 145 insertions(+) create mode 100644 docs/examples/basic_context/README.rst create mode 100644 docs/examples/basic_context/async_context.py create mode 100644 docs/examples/basic_context/child_context.py create mode 100644 docs/examples/basic_context/implicit_context.py diff --git a/docs/examples/basic_context/README.rst b/docs/examples/basic_context/README.rst new file mode 100644 index 00000000000..361192b96fc --- /dev/null +++ b/docs/examples/basic_context/README.rst @@ -0,0 +1,39 @@ +Basic Context +============= + +These examples show how context is propagated through Spans in OpenTelemetry. + +There are three different examples: + +* implicit_context: Shows how starting a span implicitly creates context. + +* child_context: Shows how context is propagated through child spans. + +* async_context: Shows how context can be shared in another coroutine. + +The source files of these examples are available :scm_web:`here `. + +Installation +------------ + +.. code-block:: sh + + pip install opentelemetry-api + pip install opentelemetry-sdk + +Run the Example +--------------- + +.. code-block:: sh + + python .py + +The output will be shown in the console. + +Useful links +------------ + +- OpenTelemetry_ +- :doc:`../../api/trace` + +.. _OpenTelemetry: https://github.com/open-telemetry/opentelemetry-python/ diff --git a/docs/examples/basic_context/async_context.py b/docs/examples/basic_context/async_context.py new file mode 100644 index 00000000000..9d8cc8da241 --- /dev/null +++ b/docs/examples/basic_context/async_context.py @@ -0,0 +1,38 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import asyncio + +from opentelemetry import baggage, trace +from opentelemetry.sdk.trace import TracerProvider + +trace.set_tracer_provider(TracerProvider()) +tracer = trace.get_tracer(__name__) + +loop = asyncio.get_event_loop() + + +async def async_span(span): + with tracer.use_span(span): + ctx = baggage.set_baggage("foo", "bar") + return ctx + + +async def main(): + span = tracer.start_span(name="span", parent=None) + ctx = await async_span(span) + print(baggage.get_all(context=ctx)) + + +loop.run_until_complete(main()) diff --git a/docs/examples/basic_context/child_context.py b/docs/examples/basic_context/child_context.py new file mode 100644 index 00000000000..65d2c7ea20a --- /dev/null +++ b/docs/examples/basic_context/child_context.py @@ -0,0 +1,27 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from opentelemetry import baggage, trace + +tracer = trace.get_tracer(__name__) + +global_ctx = baggage.set_baggage("context", "global") +with tracer.start_as_current_span(name="root span", parent=None) as root_span: + parent_ctx = baggage.set_baggage("context", "parent") + with tracer.start_as_current_span(name="child span", parent=root_span) as child_span: + child_ctx = baggage.set_baggage("context", "child") + +print(baggage.get_baggage("context", global_ctx)) +print(baggage.get_baggage("context", parent_ctx)) +print(baggage.get_baggage("context", child_ctx)) diff --git a/docs/examples/basic_context/implicit_context.py b/docs/examples/basic_context/implicit_context.py new file mode 100644 index 00000000000..4ffaa8615b4 --- /dev/null +++ b/docs/examples/basic_context/implicit_context.py @@ -0,0 +1,25 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from opentelemetry import baggage, trace +from opentelemetry.sdk.trace import TracerProvider + +trace.set_tracer_provider(TracerProvider()) +tracer = trace.get_tracer(__name__) + +with tracer.start_span(name="root span", parent=None) as root_span: + ctx = baggage.set_baggage("foo", "bar") + +print('Global context baggage: {}'.format(baggage.get_all(context=None))) +print('Span context baggage: {}'.format(baggage.get_all(context=ctx))) diff --git a/docs/faq-and-cookbook.rst b/docs/faq-and-cookbook.rst index c7f630fce78..bdebe154a35 100644 --- a/docs/faq-and-cookbook.rst +++ b/docs/faq-and-cookbook.rst @@ -25,3 +25,19 @@ Getting and modifying a span current_span = trace.get_current_span() current_span.set_attribute("hometown", "seattle") + +Capturing baggage at different contexts +--------------------------------------- + +.. code-block:: python + + from opentelemetry import trace + + tracer = trace.get_tracer(__name__) + with tracer.start_as_current_span(name="root span", parent=None) as root_span: + parent_ctx = baggage.set_baggage("context", "parent") + with tracer.start_as_current_span(name="child span", parent=root_span) as child_span: + child_ctx = baggage.set_baggage("context", "child") + + print(baggage.get_baggage("context", parent_ctx)) + print(baggage.get_baggage("context", child_ctx)) From 5700e6a53eb09e7081c645c896797e2fb2fc27c2 Mon Sep 17 00:00:00 2001 From: Amos Law Date: Fri, 25 Sep 2020 10:20:02 -0700 Subject: [PATCH 2/3] Lint --- docs/examples/basic_context/child_context.py | 4 +++- docs/examples/basic_context/implicit_context.py | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/examples/basic_context/child_context.py b/docs/examples/basic_context/child_context.py index 65d2c7ea20a..3cd2eac83d2 100644 --- a/docs/examples/basic_context/child_context.py +++ b/docs/examples/basic_context/child_context.py @@ -19,7 +19,9 @@ global_ctx = baggage.set_baggage("context", "global") with tracer.start_as_current_span(name="root span", parent=None) as root_span: parent_ctx = baggage.set_baggage("context", "parent") - with tracer.start_as_current_span(name="child span", parent=root_span) as child_span: + with tracer.start_as_current_span( + name="child span", parent=root_span + ) as child_span: child_ctx = baggage.set_baggage("context", "child") print(baggage.get_baggage("context", global_ctx)) diff --git a/docs/examples/basic_context/implicit_context.py b/docs/examples/basic_context/implicit_context.py index 4ffaa8615b4..3fa388596bc 100644 --- a/docs/examples/basic_context/implicit_context.py +++ b/docs/examples/basic_context/implicit_context.py @@ -21,5 +21,5 @@ with tracer.start_span(name="root span", parent=None) as root_span: ctx = baggage.set_baggage("foo", "bar") -print('Global context baggage: {}'.format(baggage.get_all(context=None))) -print('Span context baggage: {}'.format(baggage.get_all(context=ctx))) +print("Global context baggage: {}".format(baggage.get_all(context=None))) +print("Span context baggage: {}".format(baggage.get_all(context=ctx))) From 997f88d1c7084067bead224a9690f3b683f84212 Mon Sep 17 00:00:00 2001 From: Amos Law Date: Wed, 21 Oct 2020 03:16:03 -0400 Subject: [PATCH 3/3] Pass in parent via context --- docs/examples/basic_context/async_context.py | 2 +- docs/examples/basic_context/child_context.py | 4 ++-- docs/examples/basic_context/implicit_context.py | 4 ++-- docs/faq-and-cookbook.rst | 6 ++++-- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/docs/examples/basic_context/async_context.py b/docs/examples/basic_context/async_context.py index 9d8cc8da241..41e049e2a6b 100644 --- a/docs/examples/basic_context/async_context.py +++ b/docs/examples/basic_context/async_context.py @@ -30,7 +30,7 @@ async def async_span(span): async def main(): - span = tracer.start_span(name="span", parent=None) + span = tracer.start_span(name="span") ctx = await async_span(span) print(baggage.get_all(context=ctx)) diff --git a/docs/examples/basic_context/child_context.py b/docs/examples/basic_context/child_context.py index 3cd2eac83d2..d2a6d50136a 100644 --- a/docs/examples/basic_context/child_context.py +++ b/docs/examples/basic_context/child_context.py @@ -17,10 +17,10 @@ tracer = trace.get_tracer(__name__) global_ctx = baggage.set_baggage("context", "global") -with tracer.start_as_current_span(name="root span", parent=None) as root_span: +with tracer.start_as_current_span(name="root span") as root_span: parent_ctx = baggage.set_baggage("context", "parent") with tracer.start_as_current_span( - name="child span", parent=root_span + name="child span", context=parent_ctx ) as child_span: child_ctx = baggage.set_baggage("context", "child") diff --git a/docs/examples/basic_context/implicit_context.py b/docs/examples/basic_context/implicit_context.py index 3fa388596bc..fbfdf8b55dc 100644 --- a/docs/examples/basic_context/implicit_context.py +++ b/docs/examples/basic_context/implicit_context.py @@ -18,8 +18,8 @@ trace.set_tracer_provider(TracerProvider()) tracer = trace.get_tracer(__name__) -with tracer.start_span(name="root span", parent=None) as root_span: +with tracer.start_span(name="root span") as root_span: ctx = baggage.set_baggage("foo", "bar") -print("Global context baggage: {}".format(baggage.get_all(context=None))) +print("Global context baggage: {}".format(baggage.get_all())) print("Span context baggage: {}".format(baggage.get_all(context=ctx))) diff --git a/docs/faq-and-cookbook.rst b/docs/faq-and-cookbook.rst index bdebe154a35..4b70f2a92f9 100644 --- a/docs/faq-and-cookbook.rst +++ b/docs/faq-and-cookbook.rst @@ -34,9 +34,11 @@ Capturing baggage at different contexts from opentelemetry import trace tracer = trace.get_tracer(__name__) - with tracer.start_as_current_span(name="root span", parent=None) as root_span: + with tracer.start_as_current_span(name="root span") as root_span: parent_ctx = baggage.set_baggage("context", "parent") - with tracer.start_as_current_span(name="child span", parent=root_span) as child_span: + with tracer.start_as_current_span( + name="child span", context=parent_ctx + ) as child_span: child_ctx = baggage.set_baggage("context", "child") print(baggage.get_baggage("context", parent_ctx))