Skip to content

Commit

Permalink
Add create context key to contrib (#502)
Browse files Browse the repository at this point in the history
  • Loading branch information
euniceek authored Jun 11, 2021
1 parent 865837f commit 4be32b5
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- 'release/*'
pull_request:
env:
CORE_REPO_SHA: 3b236f5309c36d089c0d9dc6dc6c985bb1668776
CORE_REPO_SHA: c4cdffd0c8bd47b2e5c4f4a823722ca514f10db3

jobs:
build:
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `opentelemetry-instrumentation-botocore` now supports
context propagation for lambda invoke via Payload embedded headers.
([#458](https:/open-telemetry/opentelemetry-python-contrib/pull/458))
- Added support for CreateKey functionality.
([#502](https:/open-telemetry/opentelemetry-python-contrib/pull/502))

## [0.21b0](https:/open-telemetry/opentelemetry-python/releases/tag/v1.2.0-0.21b0) - 2021-05-11

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import typing

from opentelemetry.context import Context, attach, detach, set_value
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
from opentelemetry.sdk.trace import Span, SpanProcessor
from opentelemetry.sdk.trace.export import SpanExporter
from opentelemetry.trace import INVALID_TRACE_ID
Expand Down Expand Up @@ -163,7 +164,7 @@ def export(self) -> None:
del self.traces_spans_ended_count[trace_id]

if len(export_trace_ids) > 0:
token = attach(set_value("suppress_instrumentation", True))
token = attach(set_value(_SUPPRESS_INSTRUMENTATION_KEY, True))

for trace_id in export_trace_ids:
with self.traces_lock:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def strip_query_params(url: yarl.URL) -> str:
from opentelemetry.instrumentation.aiohttp_client.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.utils import (
_SUPPRESS_INSTRUMENTATION_KEY,
http_status_to_status_code,
unwrap,
)
Expand Down Expand Up @@ -155,7 +156,7 @@ async def on_request_start(
trace_config_ctx: types.SimpleNamespace,
params: aiohttp.TraceRequestStartParams,
):
if context_api.get_value("suppress_instrumentation"):
if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY):
trace_config_ctx.span = None
return

Expand Down Expand Up @@ -248,7 +249,7 @@ def _instrument(
"""
# pylint:disable=unused-argument
def instrumented_init(wrapped, instance, args, kwargs):
if context_api.get_value("suppress_instrumentation"):
if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY):
return wrapped(*args, **kwargs)

trace_configs = list(kwargs.get("trace_configs") or ())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from opentelemetry.instrumentation.aiohttp_client import (
AioHttpClientInstrumentor,
)
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace import StatusCode
Expand Down Expand Up @@ -448,7 +449,7 @@ async def uninstrument_request(server: aiohttp.test_utils.TestServer):

def test_suppress_instrumentation(self):
token = context.attach(
context.set_value("suppress_instrumentation", True)
context.set_value(_SUPPRESS_INSTRUMENTATION_KEY, True)
)
try:
run_with_test_server(
Expand All @@ -462,7 +463,7 @@ def test_suppress_instrumentation(self):
async def suppressed_request(server: aiohttp.test_utils.TestServer):
async with aiohttp.test_utils.TestClient(server) as client:
token = context.attach(
context.set_value("suppress_instrumentation", True)
context.set_value(_SUPPRESS_INSTRUMENTATION_KEY, True)
)
await client.get(TestAioHttpClientInstrumentor.URL)
context.detach(token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@
from opentelemetry.instrumentation.botocore.package import _instruments
from opentelemetry.instrumentation.botocore.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.utils import unwrap
from opentelemetry.instrumentation.utils import (
_SUPPRESS_INSTRUMENTATION_KEY,
unwrap,
)
from opentelemetry.propagate import inject
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace import SpanKind, get_tracer
Expand Down Expand Up @@ -128,7 +131,7 @@ def _patch_lambda_invoke(api_params):

# pylint: disable=too-many-branches
def _patched_api_call(self, original_func, instance, args, kwargs):
if context_api.get_value("suppress_instrumentation"):
if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY):
return original_func(*args, **kwargs)

# pylint: disable=protected-access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from opentelemetry import trace as trace_api
from opentelemetry.context import attach, detach, set_value
from opentelemetry.instrumentation.botocore import BotocoreInstrumentor
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
from opentelemetry.propagate import get_global_textmap, set_global_textmap
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.test.mock_textmap import MockTextMapPropagator
Expand Down Expand Up @@ -509,7 +510,7 @@ def test_suppress_instrumentation_xray_client(self):
xray_client = self.session.create_client(
"xray", region_name="us-east-1"
)
token = attach(set_value("suppress_instrumentation", True))
token = attach(set_value(_SUPPRESS_INSTRUMENTATION_KEY, True))
xray_client.put_trace_segments(TraceSegmentDocuments=["str1"])
xray_client.put_trace_segments(TraceSegmentDocuments=["str2"])
detach(token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.requests.package import _instruments
from opentelemetry.instrumentation.requests.version import __version__
from opentelemetry.instrumentation.utils import http_status_to_status_code
from opentelemetry.instrumentation.utils import (
_SUPPRESS_INSTRUMENTATION_KEY,
http_status_to_status_code,
)
from opentelemetry.propagate import inject
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace import SpanKind, get_tracer
Expand All @@ -54,7 +57,9 @@

# A key to a context variable to avoid creating duplicate spans when instrumenting
# both, Session.request and Session.send, since Session.request calls into Session.send
_SUPPRESS_HTTP_INSTRUMENTATION_KEY = "suppress_http_instrumentation"
_SUPPRESS_HTTP_INSTRUMENTATION_KEY = context.create_key(
"suppress_http_instrumentation"
)


# pylint: disable=unused-argument
Expand Down Expand Up @@ -111,9 +116,9 @@ def call_wrapped():
def _instrumented_requests_call(
method: str, url: str, call_wrapped, get_or_create_headers
):
if context.get_value("suppress_instrumentation") or context.get_value(
_SUPPRESS_HTTP_INSTRUMENTATION_KEY
):
if context.get_value(
_SUPPRESS_INSTRUMENTATION_KEY
) or context.get_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY):
return call_wrapped()

# See
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import opentelemetry.instrumentation.requests
from opentelemetry import context, trace
from opentelemetry.instrumentation.requests import RequestsInstrumentor
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
from opentelemetry.propagate import get_global_textmap, set_global_textmap
from opentelemetry.sdk import resources
from opentelemetry.semconv.trace import SpanAttributes
Expand Down Expand Up @@ -165,7 +166,7 @@ def test_uninstrument_session(self):

def test_suppress_instrumentation(self):
token = context.attach(
context.set_value("suppress_instrumentation", True)
context.set_value(_SUPPRESS_INSTRUMENTATION_KEY, True)
)
try:
result = self.perform_request(self.URL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,21 @@
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.urllib.package import _instruments
from opentelemetry.instrumentation.urllib.version import __version__
from opentelemetry.instrumentation.utils import http_status_to_status_code
from opentelemetry.instrumentation.utils import (
_SUPPRESS_INSTRUMENTATION_KEY,
http_status_to_status_code,
)
from opentelemetry.propagate import inject
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace import SpanKind, get_tracer
from opentelemetry.trace.status import Status
from opentelemetry.util.http import remove_url_credentials

# A key to a context variable to avoid creating duplicate spans when instrumenting
_SUPPRESS_HTTP_INSTRUMENTATION_KEY = "suppress_http_instrumentation"
# both, Session.request and Session.send, since Session.request calls into Session.send
_SUPPRESS_HTTP_INSTRUMENTATION_KEY = context.create_key(
"suppress_http_instrumentation"
)


class URLLibInstrumentor(BaseInstrumentor):
Expand Down Expand Up @@ -129,9 +135,9 @@ def call_wrapped():
def _instrumented_open_call(
_, request, call_wrapped, get_or_create_headers
): # pylint: disable=too-many-locals
if context.get_value("suppress_instrumentation") or context.get_value(
_SUPPRESS_HTTP_INSTRUMENTATION_KEY
):
if context.get_value(
_SUPPRESS_INSTRUMENTATION_KEY
) or context.get_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY):
return call_wrapped()

method = request.get_method().upper()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from opentelemetry.instrumentation.urllib import ( # pylint: disable=no-name-in-module,import-error
URLLibInstrumentor,
)
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
from opentelemetry.propagate import get_global_textmap, set_global_textmap
from opentelemetry.sdk import resources
from opentelemetry.semconv.trace import SpanAttributes
Expand Down Expand Up @@ -198,7 +199,7 @@ def test_uninstrument_session(self):

def test_suppress_instrumentation(self):
token = context.attach(
context.set_value("suppress_instrumentation", True)
context.set_value(_SUPPRESS_INSTRUMENTATION_KEY, True)
)
try:
result = self.perform_request(self.URL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def span_name_callback(method: str, url: str, headers):
from opentelemetry.instrumentation.urllib3.package import _instruments
from opentelemetry.instrumentation.urllib3.version import __version__
from opentelemetry.instrumentation.utils import (
_SUPPRESS_INSTRUMENTATION_KEY,
http_status_to_status_code,
unwrap,
)
Expand All @@ -64,7 +65,11 @@ def span_name_callback(method: str, url: str, headers):
from opentelemetry.trace import Span, SpanKind, get_tracer
from opentelemetry.trace.status import Status

_SUPPRESS_HTTP_INSTRUMENTATION_KEY = "suppress_http_instrumentation"
# A key to a context variable to avoid creating duplicate spans when instrumenting
# both, Session.request and Session.send, since Session.request calls into Session.send
_SUPPRESS_HTTP_INSTRUMENTATION_KEY = context.create_key(
"suppress_http_instrumentation"
)

_UrlFilterT = typing.Optional[typing.Callable[[str], str]]
_SpanNameT = typing.Optional[
Expand Down Expand Up @@ -214,7 +219,7 @@ def _apply_response(span: Span, response: urllib3.response.HTTPResponse):

def _is_instrumentation_suppressed() -> bool:
return bool(
context.get_value("suppress_instrumentation")
context.get_value(_SUPPRESS_INSTRUMENTATION_KEY)
or context.get_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
import urllib3.exceptions

from opentelemetry import context, trace
from opentelemetry.instrumentation.urllib3 import URLLib3Instrumentor
from opentelemetry.instrumentation.urllib3 import (
_SUPPRESS_HTTP_INSTRUMENTATION_KEY,
URLLib3Instrumentor,
)
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
from opentelemetry.propagate import get_global_textmap, set_global_textmap
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.test.mock_textmap import MockTextMapPropagator
Expand Down Expand Up @@ -165,8 +169,8 @@ def test_uninstrument(self):

def test_suppress_instrumntation(self):
suppression_keys = (
"suppress_instrumentation",
"suppress_http_instrumentation",
_SUPPRESS_HTTP_INSTRUMENTATION_KEY,
_SUPPRESS_INSTRUMENTATION_KEY,
)
for key in suppression_keys:
self.memory_exporter.clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@

from wrapt import ObjectProxy

from opentelemetry.context import create_key
from opentelemetry.trace import StatusCode

# FIXME This is a temporary location for the suppress instrumentation key.
# Once the decision around how to suppress instrumentation is made in the
# spec, this key should be moved accordingly.
_SUPPRESS_INSTRUMENTATION_KEY = create_key("suppress_instrumentation")


def extract_attributes_from_object(
obj: any, attributes: Sequence[str], existing: Dict[str, str] = None
Expand Down
9 changes: 4 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,10 @@ commands_pre =
; cases but it saves a lot of boilerplate in this file.
test: pip install {toxinidir}/opentelemetry-python-core/opentelemetry-api[test]
test: pip install {toxinidir}/opentelemetry-python-core/opentelemetry-semantic-conventions[test]
test: pip install {toxinidir}/opentelemetry-instrumentation[test]
test: pip install {toxinidir}/opentelemetry-python-core/opentelemetry-sdk[test]
test: pip install {toxinidir}/opentelemetry-python-core/tests/util[test]

test: pip install {toxinidir}/opentelemetry-instrumentation[test]

celery: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-celery[test]

grpc: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-grpc[test]
Expand Down Expand Up @@ -325,8 +324,8 @@ deps =
commands_pre =
python -m pip install {toxinidir}/opentelemetry-python-core/opentelemetry-api
python -m pip install {toxinidir}/opentelemetry-python-core/opentelemetry-semantic-conventions
python -m pip install {toxinidir}/opentelemetry-python-core/opentelemetry-sdk
python -m pip install {toxinidir}/opentelemetry-instrumentation
python -m pip install {toxinidir}/opentelemetry-python-core/opentelemetry-sdk
python -m pip install {toxinidir}/util/opentelemetry-util-http

changedir = docs
Expand All @@ -352,9 +351,9 @@ commands_pre =
sudo apt-get install libsnappy-dev
python -m pip install {toxinidir}/opentelemetry-python-core/opentelemetry-api
python -m pip install {toxinidir}/opentelemetry-python-core/opentelemetry-semantic-conventions
python -m pip install -e {toxinidir}/opentelemetry-instrumentation[test]
python -m pip install {toxinidir}/opentelemetry-python-core/opentelemetry-sdk
python -m pip install {toxinidir}/opentelemetry-python-core/tests/util
python -m pip install -e {toxinidir}/opentelemetry-instrumentation[test]
python -m pip install -e {toxinidir}/util/opentelemetry-util-http[test]
python -m pip install -e {toxinidir}/instrumentation/opentelemetry-instrumentation-wsgi[test]
python -m pip install -e {toxinidir}/instrumentation/opentelemetry-instrumentation-dbapi[test]
Expand Down Expand Up @@ -420,9 +419,9 @@ changedir =
commands_pre =
pip install -e {toxinidir}/opentelemetry-python-core/opentelemetry-api \
-e {toxinidir}/opentelemetry-python-core/opentelemetry-semantic-conventions \
-e {toxinidir}/opentelemetry-instrumentation \
-e {toxinidir}/opentelemetry-python-core/opentelemetry-sdk \
-e {toxinidir}/opentelemetry-python-core/tests/util \
-e {toxinidir}/opentelemetry-instrumentation \
-e {toxinidir}/instrumentation/opentelemetry-instrumentation-asyncpg \
-e {toxinidir}/instrumentation/opentelemetry-instrumentation-celery \
-e {toxinidir}/instrumentation/opentelemetry-instrumentation-dbapi \
Expand Down

0 comments on commit 4be32b5

Please sign in to comment.