-
Notifications
You must be signed in to change notification settings - Fork 517
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(otel): Autoinstrumentation skeleton (#3143)
Expand the POTel PoC's autoinstrumentation capabilities. This change allows us to: - install and enable all available instrumentations by default - further configure instrumentations that accept optional arguments
- Loading branch information
1 parent
7a3ab15
commit 4a9556b
Showing
9 changed files
with
279 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,6 +124,7 @@ | |
"Miscellaneous": [ | ||
"loguru", | ||
"opentelemetry", | ||
"potel", | ||
"pure_eval", | ||
"trytond", | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
""" | ||
IMPORTANT: The contents of this file are part of a proof of concept and as such | ||
are experimental and not suitable for production use. They may be changed or | ||
removed at any time without prior notice. | ||
""" | ||
|
||
from sentry_sdk.integrations import DidNotEnable | ||
from sentry_sdk.integrations.opentelemetry.propagator import SentryPropagator | ||
from sentry_sdk.integrations.opentelemetry.span_processor import SentrySpanProcessor | ||
from sentry_sdk.utils import logger | ||
from sentry_sdk._types import TYPE_CHECKING | ||
|
||
try: | ||
from opentelemetry import trace # type: ignore | ||
from opentelemetry.instrumentation.distro import BaseDistro # type: ignore | ||
from opentelemetry.propagate import set_global_textmap # type: ignore | ||
from opentelemetry.sdk.trace import TracerProvider # type: ignore | ||
except ImportError: | ||
raise DidNotEnable("opentelemetry not installed") | ||
|
||
try: | ||
from opentelemetry.instrumentation.django import DjangoInstrumentor # type: ignore | ||
except ImportError: | ||
DjangoInstrumentor = None | ||
|
||
try: | ||
from opentelemetry.instrumentation.flask import FlaskInstrumentor # type: ignore | ||
except ImportError: | ||
FlaskInstrumentor = None | ||
|
||
if TYPE_CHECKING: | ||
# XXX pkg_resources is deprecated, there's a PR to switch to importlib: | ||
# https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2181 | ||
# we should align this when the PR gets merged | ||
from pkg_resources import EntryPoint | ||
from typing import Any | ||
|
||
|
||
CONFIGURABLE_INSTRUMENTATIONS = { | ||
DjangoInstrumentor: {"is_sql_commentor_enabled": True}, | ||
FlaskInstrumentor: {"enable_commenter": True}, | ||
} | ||
|
||
|
||
class _SentryDistro(BaseDistro): # type: ignore[misc] | ||
def _configure(self, **kwargs): | ||
# type: (Any) -> None | ||
provider = TracerProvider() | ||
provider.add_span_processor(SentrySpanProcessor()) | ||
trace.set_tracer_provider(provider) | ||
set_global_textmap(SentryPropagator()) | ||
|
||
def load_instrumentor(self, entry_point, **kwargs): | ||
# type: (EntryPoint, Any) -> None | ||
instrumentor = entry_point.load() | ||
|
||
if instrumentor in CONFIGURABLE_INSTRUMENTATIONS: | ||
for key, value in CONFIGURABLE_INSTRUMENTATIONS[instrumentor].items(): | ||
kwargs[key] = value | ||
|
||
instrumentor().instrument(**kwargs) | ||
logger.debug( | ||
"[OTel] %s instrumented (%s)", | ||
entry_point.name, | ||
", ".join([f"{k}: {v}" for k, v in kwargs.items()]), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.