Skip to content
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

Propagator Class [POC for review] #121

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

from requests.sessions import Session

from opentelemetry import propagator


# NOTE: Currently we force passing a tracer. But in turn, this forces the user
# to configure a SDK before enabling this integration. In turn, this means that
Expand Down Expand Up @@ -77,6 +79,10 @@ def instrumented_request(self, method, url, *args, **kwargs):
span.set_attribute("http.status_code", result.status_code)
span.set_attribute("http.status_text", result.reason)

propagator.global_propagator().inject(
Copy link
Member

Choose a reason for hiding this comment

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

Probably off-topic, but: You are injecting the headers after the request was sent here, and into the response headers instead of the request headers.

I also wonder if we should automatically fall back to setter.__setitem__ if not callable(setter). Requiring users to pass dunder methods is a bit arcane.

Copy link
Member Author

Choose a reason for hiding this comment

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

sorry yes, good point. Will fix that.

defaulting to setitem is a good idea. Just requires someone to write a wrapper object if that doesn't already exist. A bound callable isn't a bad idea either.

I'll file an issue and pick that up in another PR.

result.headers.__setitem__, result.headers
)

return result

# TODO: How to handle exceptions? Should we create events for them? Set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
import unittest
from unittest import mock

import opentelemetry.ext.http_requests
import requests
import urllib3
from opentelemetry import trace

import opentelemetry.ext.http_requests
from opentelemetry import propagator, trace
from opentelemetry.context import Context


class TestRequestsIntegration(unittest.TestCase):

# TODO: Copy & paste from test_wsgi_middleware
def setUp(self):
propagator.set_propagator(propagator.Propagator(Context, None, None))
self.span_attrs = {}
self.tracer = trace.tracer()
self.span_context_manager = mock.MagicMock()
Expand Down
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:
"""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.

Loading