-
Notifications
You must be signed in to change notification settings - Fork 34
feat: app-level instrumentation #236
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
Open
LucasAlvesSoares
wants to merge
9
commits into
main
Choose a base branch
from
app-level-instrumentation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a94c91c
App-level instrumentation
LucasAlvesSoares 4667bc5
File structure
LucasAlvesSoares c0306cd
redis and sqlalchemy
LucasAlvesSoares 8ec08a6
django and flask
LucasAlvesSoares 10dd2cd
version bump
LucasAlvesSoares 65e8d14
user guide
LucasAlvesSoares fb1c062
Logging instrumentation
LucasAlvesSoares 42f2393
Version bump
LucasAlvesSoares b5e6ce3
Linting
LucasAlvesSoares File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
55 changes: 55 additions & 0 deletions
55
src/sap_cloud_sdk/core/telemetry/instrumentation/__init__.py
This file contains hidden or 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,55 @@ | ||
| from sap_cloud_sdk.core.telemetry.instrumentation.base import LibraryInstrumentor | ||
| from sap_cloud_sdk.core.telemetry.instrumentation._registry import ( | ||
| register, | ||
| get_registry, | ||
| ) | ||
|
|
||
| # Import concrete instrumentors to trigger their register() calls. | ||
| from sap_cloud_sdk.core.telemetry.instrumentation.instrumentors import ( # noqa: F401 | ||
| httpx, | ||
| requests, | ||
| grpc, | ||
| logging, | ||
| ) | ||
|
|
||
| # Optional — guarded so missing extras don't break the import. | ||
| try: | ||
| from sap_cloud_sdk.core.telemetry.instrumentation.instrumentors import starlette # noqa: F401 | ||
| except ImportError: | ||
| pass | ||
|
|
||
| try: | ||
| from sap_cloud_sdk.core.telemetry.instrumentation.instrumentors import fastapi # noqa: F401 | ||
| except ImportError: | ||
| pass | ||
|
|
||
| try: | ||
| from sap_cloud_sdk.core.telemetry.instrumentation.instrumentors import aiohttp # noqa: F401 | ||
| except ImportError: | ||
| pass | ||
|
|
||
| try: | ||
| from sap_cloud_sdk.core.telemetry.instrumentation.instrumentors import sqlalchemy # noqa: F401 | ||
| except ImportError: | ||
| pass | ||
|
|
||
| try: | ||
| from sap_cloud_sdk.core.telemetry.instrumentation.instrumentors import redis # noqa: F401 | ||
| except ImportError: | ||
| pass | ||
|
|
||
| try: | ||
| from sap_cloud_sdk.core.telemetry.instrumentation.instrumentors import django # noqa: F401 | ||
| except ImportError: | ||
| pass | ||
|
|
||
| try: | ||
| from sap_cloud_sdk.core.telemetry.instrumentation.instrumentors import flask # noqa: F401 | ||
| except ImportError: | ||
| pass | ||
|
|
||
| __all__ = [ | ||
| "LibraryInstrumentor", | ||
| "register", | ||
| "get_registry", | ||
| ] |
16 changes: 16 additions & 0 deletions
16
src/sap_cloud_sdk/core/telemetry/instrumentation/_registry.py
This file contains hidden or 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,16 @@ | ||
| from sap_cloud_sdk.core.telemetry.instrumentation.base import LibraryInstrumentor | ||
|
|
||
| _registry: list[LibraryInstrumentor] = [] | ||
|
|
||
|
|
||
| def register(instrumentor: LibraryInstrumentor) -> None: | ||
| """Add an instrumentor to the registry. | ||
|
|
||
| Call this at module level in each concrete instrumentor file, or from | ||
| third-party code that wants to plug in additional library coverage. | ||
| """ | ||
| _registry.append(instrumentor) | ||
|
|
||
|
|
||
| def get_registry() -> list[LibraryInstrumentor]: | ||
| return list(_registry) |
This file contains hidden or 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,48 @@ | ||
| import importlib.util | ||
| import logging | ||
| from abc import ABC, abstractmethod | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class LibraryInstrumentor(ABC): | ||
| """Base class for optional library instrumentors. | ||
|
|
||
| Subclasses wrap a single opentelemetry-instrumentation-* package. | ||
| The target library (e.g. httpx) is checked at runtime via find_spec so | ||
| that missing optional dependencies are silently skipped rather than | ||
| raising ImportError. | ||
| """ | ||
|
|
||
| #: Import name of the library being instrumented (e.g. "httpx"). | ||
| library_name: str | ||
|
|
||
| def instrument(self) -> None: | ||
| if not self._is_library_installed(): | ||
| logger.debug( | ||
| "%s not installed, skipping instrumentation", self.library_name | ||
| ) | ||
| return | ||
| if self.is_instrumented(): | ||
| logger.debug("%s already instrumented", self.library_name) | ||
| return | ||
| self._instrument() | ||
| logger.debug("Instrumented %s", self.library_name) | ||
|
|
||
| def uninstrument(self) -> None: | ||
| if not self.is_instrumented(): | ||
| return | ||
| self._uninstrument() | ||
| logger.debug("Uninstrumented %s", self.library_name) | ||
|
|
||
| @abstractmethod | ||
| def is_instrumented(self) -> bool: ... | ||
|
|
||
| @abstractmethod | ||
| def _instrument(self) -> None: ... | ||
|
|
||
| @abstractmethod | ||
| def _uninstrument(self) -> None: ... | ||
|
|
||
| def _is_library_installed(self) -> bool: | ||
| return importlib.util.find_spec(self.library_name) is not None |
Empty file.
22 changes: 22 additions & 0 deletions
22
src/sap_cloud_sdk/core/telemetry/instrumentation/instrumentors/aiohttp.py
This file contains hidden or 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,22 @@ | ||
| from opentelemetry.instrumentation.aiohttp_client import AioHttpClientInstrumentor | ||
|
|
||
| from sap_cloud_sdk.core.telemetry.instrumentation.base import LibraryInstrumentor | ||
| from sap_cloud_sdk.core.telemetry.instrumentation._registry import register | ||
|
|
||
| _instrumentor = AioHttpClientInstrumentor() | ||
|
|
||
|
|
||
| class AiohttpInstrumentor(LibraryInstrumentor): | ||
| library_name = "aiohttp" | ||
|
|
||
| def is_instrumented(self) -> bool: | ||
| return _instrumentor.is_instrumented_by_opentelemetry | ||
|
|
||
| def _instrument(self) -> None: | ||
| _instrumentor.instrument() | ||
|
|
||
| def _uninstrument(self) -> None: | ||
| _instrumentor.uninstrument() | ||
|
|
||
|
|
||
| register(AiohttpInstrumentor()) |
22 changes: 22 additions & 0 deletions
22
src/sap_cloud_sdk/core/telemetry/instrumentation/instrumentors/django.py
This file contains hidden or 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,22 @@ | ||
| from opentelemetry.instrumentation.django import DjangoInstrumentor | ||
|
|
||
| from sap_cloud_sdk.core.telemetry.instrumentation.base import LibraryInstrumentor | ||
| from sap_cloud_sdk.core.telemetry.instrumentation._registry import register | ||
|
|
||
| _instrumentor = DjangoInstrumentor() | ||
|
|
||
|
|
||
| class DjangoInstrumentorWrapper(LibraryInstrumentor): | ||
| library_name = "django" | ||
|
|
||
| def is_instrumented(self) -> bool: | ||
| return _instrumentor.is_instrumented_by_opentelemetry | ||
|
|
||
| def _instrument(self) -> None: | ||
| _instrumentor.instrument() | ||
|
|
||
| def _uninstrument(self) -> None: | ||
| _instrumentor.uninstrument() | ||
|
|
||
|
|
||
| register(DjangoInstrumentorWrapper()) |
22 changes: 22 additions & 0 deletions
22
src/sap_cloud_sdk/core/telemetry/instrumentation/instrumentors/fastapi.py
This file contains hidden or 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,22 @@ | ||
| from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor | ||
|
|
||
| from sap_cloud_sdk.core.telemetry.instrumentation.base import LibraryInstrumentor | ||
| from sap_cloud_sdk.core.telemetry.instrumentation._registry import register | ||
|
|
||
| _instrumentor = FastAPIInstrumentor() | ||
|
|
||
|
|
||
| class FastAPIInstrumentorWrapper(LibraryInstrumentor): | ||
| library_name = "fastapi" | ||
|
|
||
| def is_instrumented(self) -> bool: | ||
| return _instrumentor.is_instrumented_by_opentelemetry | ||
|
|
||
| def _instrument(self) -> None: | ||
| _instrumentor.instrument() | ||
|
|
||
| def _uninstrument(self) -> None: | ||
| _instrumentor.uninstrument() | ||
|
|
||
|
|
||
| register(FastAPIInstrumentorWrapper()) |
22 changes: 22 additions & 0 deletions
22
src/sap_cloud_sdk/core/telemetry/instrumentation/instrumentors/flask.py
This file contains hidden or 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,22 @@ | ||
| from opentelemetry.instrumentation.flask import FlaskInstrumentor | ||
|
|
||
| from sap_cloud_sdk.core.telemetry.instrumentation.base import LibraryInstrumentor | ||
| from sap_cloud_sdk.core.telemetry.instrumentation._registry import register | ||
|
|
||
| _instrumentor = FlaskInstrumentor() | ||
|
|
||
|
|
||
| class FlaskInstrumentorWrapper(LibraryInstrumentor): | ||
| library_name = "flask" | ||
|
|
||
| def is_instrumented(self) -> bool: | ||
| return _instrumentor.is_instrumented_by_opentelemetry | ||
|
|
||
| def _instrument(self) -> None: | ||
| _instrumentor.instrument() | ||
|
|
||
| def _uninstrument(self) -> None: | ||
| _instrumentor.uninstrument() | ||
|
|
||
|
|
||
| register(FlaskInstrumentorWrapper()) |
31 changes: 31 additions & 0 deletions
31
src/sap_cloud_sdk/core/telemetry/instrumentation/instrumentors/grpc.py
This file contains hidden or 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,31 @@ | ||
| from opentelemetry.instrumentation.grpc import ( | ||
| GrpcInstrumentorClient, | ||
| GrpcInstrumentorServer, | ||
| ) | ||
|
|
||
| from sap_cloud_sdk.core.telemetry.instrumentation.base import LibraryInstrumentor | ||
| from sap_cloud_sdk.core.telemetry.instrumentation._registry import register | ||
|
|
||
| _client_instrumentor = GrpcInstrumentorClient() | ||
| _server_instrumentor = GrpcInstrumentorServer() | ||
|
|
||
|
|
||
| class GrpcInstrumentorWrapper(LibraryInstrumentor): | ||
| library_name = "grpc" | ||
|
|
||
| def is_instrumented(self) -> bool: | ||
| return ( | ||
| _client_instrumentor.is_instrumented_by_opentelemetry | ||
| or _server_instrumentor.is_instrumented_by_opentelemetry | ||
| ) | ||
|
|
||
| def _instrument(self) -> None: | ||
| _client_instrumentor.instrument() | ||
| _server_instrumentor.instrument() | ||
|
|
||
| def _uninstrument(self) -> None: | ||
| _client_instrumentor.uninstrument() | ||
| _server_instrumentor.uninstrument() | ||
|
|
||
|
|
||
| register(GrpcInstrumentorWrapper()) |
22 changes: 22 additions & 0 deletions
22
src/sap_cloud_sdk/core/telemetry/instrumentation/instrumentors/httpx.py
This file contains hidden or 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,22 @@ | ||
| from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor | ||
|
|
||
| from sap_cloud_sdk.core.telemetry.instrumentation.base import LibraryInstrumentor | ||
| from sap_cloud_sdk.core.telemetry.instrumentation._registry import register | ||
|
|
||
| _instrumentor = HTTPXClientInstrumentor() | ||
|
|
||
|
|
||
| class HttpxInstrumentor(LibraryInstrumentor): | ||
| library_name = "httpx" | ||
|
|
||
| def is_instrumented(self) -> bool: | ||
| return _instrumentor.is_instrumented_by_opentelemetry | ||
|
|
||
| def _instrument(self) -> None: | ||
| _instrumentor.instrument() | ||
|
|
||
| def _uninstrument(self) -> None: | ||
| _instrumentor.uninstrument() | ||
|
|
||
|
|
||
| register(HttpxInstrumentor()) |
22 changes: 22 additions & 0 deletions
22
src/sap_cloud_sdk/core/telemetry/instrumentation/instrumentors/logging.py
This file contains hidden or 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,22 @@ | ||
| from opentelemetry.instrumentation.logging import LoggingInstrumentor | ||
|
|
||
| from sap_cloud_sdk.core.telemetry.instrumentation.base import LibraryInstrumentor | ||
| from sap_cloud_sdk.core.telemetry.instrumentation._registry import register | ||
|
|
||
| _instrumentor = LoggingInstrumentor() | ||
|
|
||
|
|
||
| class LoggingInstrumentorWrapper(LibraryInstrumentor): | ||
| library_name = "logging" | ||
|
|
||
| def is_instrumented(self) -> bool: | ||
| return _instrumentor.is_instrumented_by_opentelemetry | ||
|
|
||
| def _instrument(self) -> None: | ||
| _instrumentor.instrument(set_logging_format=True) | ||
|
|
||
| def _uninstrument(self) -> None: | ||
| _instrumentor.uninstrument() | ||
|
|
||
|
|
||
| register(LoggingInstrumentorWrapper()) |
22 changes: 22 additions & 0 deletions
22
src/sap_cloud_sdk/core/telemetry/instrumentation/instrumentors/redis.py
This file contains hidden or 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,22 @@ | ||
| from opentelemetry.instrumentation.redis import RedisInstrumentor | ||
|
|
||
| from sap_cloud_sdk.core.telemetry.instrumentation.base import LibraryInstrumentor | ||
| from sap_cloud_sdk.core.telemetry.instrumentation._registry import register | ||
|
|
||
| _instrumentor = RedisInstrumentor() | ||
|
|
||
|
|
||
| class RedisInstrumentorWrapper(LibraryInstrumentor): | ||
| library_name = "redis" | ||
|
|
||
| def is_instrumented(self) -> bool: | ||
| return _instrumentor.is_instrumented_by_opentelemetry | ||
|
|
||
| def _instrument(self) -> None: | ||
| _instrumentor.instrument() | ||
|
|
||
| def _uninstrument(self) -> None: | ||
| _instrumentor.uninstrument() | ||
|
|
||
|
|
||
| register(RedisInstrumentorWrapper()) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the agents using Redis?