Thanks to visit codestin.com
Credit goes to github.com

Skip to content

unifying propagator formatters #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all 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
31 changes: 17 additions & 14 deletions opentelemetry-api/src/opentelemetry/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
# 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.


"""
The OpenTelemetry context module provides abstraction layer on top of
thread-local storage and contextvars. The long term direction is to switch to
Expand All @@ -27,9 +25,8 @@
>>> Context.foo
2

When explicit thread is used, a helper function
``Context.with_current_context`` can be used to carry the context across
threads::
When explicit thread is used, a helper function `BaseRuntimeContext.with_current_context`
can be used to carry the context across threads::

from threading import Thread
from opentelemetry.context import Context
Expand Down Expand Up @@ -138,20 +135,26 @@ async def main():
asyncio.run(main())
"""

import typing

from .base_context import BaseRuntimeContext

__all__ = ["Context"]


Context = None # type: typing.Optional[BaseRuntimeContext]
def create_context() -> BaseRuntimeContext:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this is useful for this module's testing, I don't think this is something that should be exposed - I don't think users are really supposed to be creating more than one context.

"""Create a new context object.

Return a new RuntimeContext, instantiating the variant that
is the most appropriate for the version of Python being used.
"""

try:
from .async_context import AsyncRuntimeContext

return AsyncRuntimeContext()
except ImportError:
from .thread_local_context import ThreadLocalRuntimeContext

try:
from .async_context import AsyncRuntimeContext
return ThreadLocalRuntimeContext()

Context = AsyncRuntimeContext()
except ImportError:
from .thread_local_context import ThreadLocalRuntimeContext

Context = ThreadLocalRuntimeContext()
Context = create_context()

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
# limitations under the License.

import abc
import typing

from opentelemetry.distributedcontext import DistributedContext
from opentelemetry.context import BaseRuntimeContext


class BinaryFormat(abc.ABC):
Expand All @@ -27,14 +26,14 @@ class BinaryFormat(abc.ABC):

@staticmethod
@abc.abstractmethod
def to_bytes(context: DistributedContext) -> bytes:
"""Creates a byte representation of a DistributedContext.
def to_bytes(context: BaseRuntimeContext) -> bytes:
"""Creates a byte representation of a Context.

to_bytes should read values from a DistributedContext and return a data
to_bytes should read values from a Context and return a data
format to represent it, in bytes.

Args:
context: the DistributedContext to serialize
context: the Context to serialize.

Returns:
A bytes representation of the DistributedContext.
Expand All @@ -44,19 +43,15 @@ def to_bytes(context: DistributedContext) -> bytes:
@staticmethod
@abc.abstractmethod
def from_bytes(
byte_representation: bytes
) -> typing.Optional[DistributedContext]:
"""Return a DistributedContext that was represented by bytes.
context: BaseRuntimeContext, byte_representation: bytes
) -> None:
"""Populate context with context data that existed in the byte representation.

from_bytes should return back a DistributedContext that was constructed
from the data serialized in the byte_representation passed. If it is
not possible to read in a proper DistributedContext, return None.
from_bytes should add values into the context from the data serialized in the
byte_representation passed. If it is not possible to read in a proper
DistributedContext, return None.

Args:
byte_representation: the bytes to deserialize

Returns:
A bytes representation of the DistributedContext if it is valid.
Otherwise return None.

context: the Context to add values into.
byte_representation: the bytes to deserialize.
"""
Loading