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

Add create context key to contrib #502

Merged
merged 26 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
eb4e9c6
Add context key in contrib repo
euniceek May 18, 2021
18d99ba
Fix lint errors
euniceek May 18, 2021
5e07eb3
Apply suggestions from code review
eddyleelin May 21, 2021
391ded9
Fix orders of test
euniceek May 21, 2021
96dd6fe
Update changelog
euniceek May 21, 2021
2b60bed
Fix orders
euniceek May 21, 2021
6bd41c7
Update changelog link
euniceek May 21, 2021
dceeec3
Update changelog to be in added section
euniceek May 24, 2021
84d1586
Change export key name
euniceek May 24, 2021
01dacae
Refactor suppress instrumentation key
euniceek May 26, 2021
075bdd0
Remove create key in span processor
euniceek May 26, 2021
f05b9f5
Move suppress key to instrumentation package
euniceek May 31, 2021
b4c0f0e
Fix lint error
euniceek May 31, 2021
4128fa7
Merge branch 'open-telemetry:main' into context-key-contrib
euniceek May 31, 2021
196684b
Update changelog
euniceek May 31, 2021
212dcb9
Fix import errors
euniceek May 31, 2021
f6cfd54
Merge remote-tracking branch 'origin/master' into context-key-contrib
euniceek Jun 3, 2021
7f6b893
Add comment on suppress instrumentation key
euniceek Jun 3, 2021
11b5ece
Update SHA
euniceek Jun 6, 2021
6ea87b6
Merge remote-tracking branch 'origin/master' into context-key-contrib
euniceek Jun 6, 2021
1604150
Add FIXME comment
euniceek Jun 6, 2021
bd9108e
Change instrumentation order in tox.ini
euniceek Jun 6, 2021
73c67be
Change instrumentation order in tox.ini
euniceek Jun 6, 2021
3c8330b
Merge branch 'open-telemetry:main' into context-key-contrib
euniceek Jun 7, 2021
82c4f60
Merge branch 'open-telemetry:main' into context-key-contrib
euniceek Jun 11, 2021
88191c4
Merge branch 'open-telemetry:main' into context-key-contrib
euniceek Jun 11, 2021
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
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://github.com/open-telemetry/opentelemetry-python-contrib/pull/458))
- Added support for CreateKey functionality.
([#502](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/502))

## [0.21b0](https://github.com/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")
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we leave a comment saying that this is a temporary solution, and we are waiting on a decision to how we are handling supressing instrumentations?



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