Skip to content

Commit

Permalink
🎨 Tracing: Add more autoinstrumentation, enhance setup_tracing() - …
Browse files Browse the repository at this point in the history
…DON'T DELETE PR BRANCH (#6561)

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: Dustin Kaiser <[email protected]>
Co-authored-by: Andrei Neagu <[email protected]>
Co-authored-by: Andrei Neagu <[email protected]>
Co-authored-by: Odei Maiz <[email protected]>
Co-authored-by: Sylvain <[email protected]>
Co-authored-by: Pedro Crespo-Valero <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: matusdrobuliak66 <[email protected]>
Co-authored-by: Julian Querido <[email protected]>
Co-authored-by: Odei Maiz <[email protected]>
  • Loading branch information
11 people authored Nov 5, 2024
1 parent d8f1720 commit cd20a2c
Show file tree
Hide file tree
Showing 47 changed files with 826 additions and 128 deletions.
1 change: 1 addition & 0 deletions packages/aws-library/requirements/_base.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ aiocache
arrow
pydantic[email]
types-aiobotocore[ec2,s3,ssm]
opentelemetry-instrumentation-botocore
sh
19 changes: 18 additions & 1 deletion packages/aws-library/requirements/_base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ arrow==1.3.0
# -r requirements/../../../packages/service-library/requirements/../../../packages/models-library/requirements/_base.in
# -r requirements/../../../packages/service-library/requirements/_base.in
# -r requirements/_base.in
async-timeout==4.0.3
# via redis
attrs==24.2.0
# via
# aiohttp
Expand Down Expand Up @@ -127,7 +129,10 @@ opentelemetry-api==1.27.0
# opentelemetry-exporter-otlp-proto-grpc
# opentelemetry-exporter-otlp-proto-http
# opentelemetry-instrumentation
# opentelemetry-instrumentation-botocore
# opentelemetry-instrumentation-redis
# opentelemetry-instrumentation-requests
# opentelemetry-propagator-aws-xray
# opentelemetry-sdk
# opentelemetry-semantic-conventions
opentelemetry-exporter-otlp==1.27.0
Expand All @@ -141,9 +146,18 @@ opentelemetry-exporter-otlp-proto-grpc==1.27.0
opentelemetry-exporter-otlp-proto-http==1.27.0
# via opentelemetry-exporter-otlp
opentelemetry-instrumentation==0.48b0
# via opentelemetry-instrumentation-requests
# via
# opentelemetry-instrumentation-botocore
# opentelemetry-instrumentation-redis
# opentelemetry-instrumentation-requests
opentelemetry-instrumentation-botocore==0.48b0
# via -r requirements/_base.in
opentelemetry-instrumentation-redis==0.48b0
# via -r requirements/../../../packages/service-library/requirements/_base.in
opentelemetry-instrumentation-requests==0.48b0
# via -r requirements/../../../packages/service-library/requirements/_base.in
opentelemetry-propagator-aws-xray==1.0.2
# via opentelemetry-instrumentation-botocore
opentelemetry-proto==1.27.0
# via
# opentelemetry-exporter-otlp-proto-common
Expand All @@ -156,6 +170,8 @@ opentelemetry-sdk==1.27.0
# opentelemetry-exporter-otlp-proto-http
opentelemetry-semantic-conventions==0.48b0
# via
# opentelemetry-instrumentation-botocore
# opentelemetry-instrumentation-redis
# opentelemetry-instrumentation-requests
# opentelemetry-sdk
opentelemetry-util-http==0.48b0
Expand Down Expand Up @@ -297,6 +313,7 @@ wrapt==1.16.0
# aiobotocore
# deprecated
# opentelemetry-instrumentation
# opentelemetry-instrumentation-redis
yarl==1.12.1
# via
# aio-pika
Expand Down
1 change: 1 addition & 0 deletions packages/postgres-database/requirements/_base.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
alembic
pydantic
sqlalchemy[postgresql_psycopg2binary,postgresql_asyncpg] # SEE extras in https://github.com/sqlalchemy/sqlalchemy/blob/main/setup.cfg#L43
opentelemetry-instrumentation-asyncpg
yarl
25 changes: 25 additions & 0 deletions packages/postgres-database/requirements/_base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ async-timeout==4.0.3
# via asyncpg
asyncpg==0.29.0
# via sqlalchemy
deprecated==1.2.14
# via
# opentelemetry-api
# opentelemetry-semantic-conventions
greenlet==3.1.1
# via sqlalchemy
idna==3.10
# via yarl
importlib-metadata==8.4.0
# via opentelemetry-api
mako==1.3.5
# via
# -c requirements/../../../requirements/constraints.txt
Expand All @@ -16,12 +22,25 @@ markupsafe==2.1.5
# via mako
multidict==6.1.0
# via yarl
opentelemetry-api==1.27.0
# via
# opentelemetry-instrumentation
# opentelemetry-instrumentation-asyncpg
# opentelemetry-semantic-conventions
opentelemetry-instrumentation==0.48b0
# via opentelemetry-instrumentation-asyncpg
opentelemetry-instrumentation-asyncpg==0.48b0
# via -r requirements/_base.in
opentelemetry-semantic-conventions==0.48b0
# via opentelemetry-instrumentation-asyncpg
psycopg2-binary==2.9.9
# via sqlalchemy
pydantic==1.10.18
# via
# -c requirements/../../../requirements/constraints.txt
# -r requirements/_base.in
setuptools==75.2.0
# via opentelemetry-instrumentation
sqlalchemy==1.4.54
# via
# -c requirements/../../../requirements/constraints.txt
Expand All @@ -31,5 +50,11 @@ typing-extensions==4.12.2
# via
# alembic
# pydantic
wrapt==1.16.0
# via
# deprecated
# opentelemetry-instrumentation
yarl==1.12.1
# via -r requirements/_base.in
zipp==3.20.2
# via importlib-metadata
6 changes: 4 additions & 2 deletions packages/postgres-database/requirements/_tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ pyyaml==6.0.2
# pre-commit
ruff==0.6.7
# via -r requirements/../../../requirements/devenv.txt
setuptools==75.1.0
# via pip-tools
setuptools==75.2.0
# via
# -c requirements/_base.txt
# pip-tools
tomlkit==0.13.2
# via pylint
typing-extensions==4.12.2
Expand Down
1 change: 1 addition & 0 deletions packages/service-library/requirements/_base.in
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ faststream
opentelemetry-api
opentelemetry-exporter-otlp
opentelemetry-instrumentation-requests
opentelemetry-instrumentation-redis
opentelemetry-sdk
psutil
pydantic
Expand Down
11 changes: 10 additions & 1 deletion packages/service-library/requirements/_base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ arrow==1.3.0
# via
# -r requirements/../../../packages/models-library/requirements/_base.in
# -r requirements/_base.in
async-timeout==4.0.3
# via redis
attrs==24.2.0
# via
# aiohttp
Expand Down Expand Up @@ -93,6 +95,7 @@ opentelemetry-api==1.27.0
# opentelemetry-exporter-otlp-proto-grpc
# opentelemetry-exporter-otlp-proto-http
# opentelemetry-instrumentation
# opentelemetry-instrumentation-redis
# opentelemetry-instrumentation-requests
# opentelemetry-sdk
# opentelemetry-semantic-conventions
Expand All @@ -107,7 +110,11 @@ opentelemetry-exporter-otlp-proto-grpc==1.27.0
opentelemetry-exporter-otlp-proto-http==1.27.0
# via opentelemetry-exporter-otlp
opentelemetry-instrumentation==0.48b0
# via opentelemetry-instrumentation-requests
# via
# opentelemetry-instrumentation-redis
# opentelemetry-instrumentation-requests
opentelemetry-instrumentation-redis==0.48b0
# via -r requirements/_base.in
opentelemetry-instrumentation-requests==0.48b0
# via -r requirements/_base.in
opentelemetry-proto==1.27.0
Expand All @@ -122,6 +129,7 @@ opentelemetry-sdk==1.27.0
# opentelemetry-exporter-otlp-proto-http
opentelemetry-semantic-conventions==0.48b0
# via
# opentelemetry-instrumentation-redis
# opentelemetry-instrumentation-requests
# opentelemetry-sdk
opentelemetry-util-http==0.48b0
Expand Down Expand Up @@ -219,6 +227,7 @@ wrapt==1.16.0
# via
# deprecated
# opentelemetry-instrumentation
# opentelemetry-instrumentation-redis
yarl==1.12.1
# via
# aio-pika
Expand Down
2 changes: 2 additions & 0 deletions packages/service-library/requirements/_test.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
# testing

asgi_lifespan
botocore
coverage
docker
faker
flaky
numpy
openapi-spec-validator
pillow
pip
pytest
pytest-aiohttp
pytest-asyncio
Expand Down
8 changes: 8 additions & 0 deletions packages/service-library/requirements/_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ attrs==24.2.0
# jsonschema
# pytest-docker
# referencing
botocore==1.35.50
# via -r requirements/_test.in
certifi==2024.8.30
# via
# -c requirements/../../../requirements/constraints.txt
Expand Down Expand Up @@ -91,6 +93,8 @@ idna==3.10
# yarl
iniconfig==2.0.0
# via pytest
jmespath==1.0.1
# via botocore
jsonschema==4.23.0
# via
# -c requirements/_aiohttp.txt
Expand Down Expand Up @@ -141,6 +145,8 @@ pathable==0.4.3
# jsonschema-path
pillow==10.4.0
# via -r requirements/_test.in
pip==24.3.1
# via -r requirements/_test.in
pluggy==1.5.0
# via pytest
pprintpp==0.4.0
Expand Down Expand Up @@ -188,6 +194,7 @@ pytest-xdist==3.6.1
python-dateutil==2.9.0.post0
# via
# -c requirements/_base.txt
# botocore
# faker
python-dotenv==1.0.1
# via -r requirements/_test.in
Expand Down Expand Up @@ -263,6 +270,7 @@ urllib3==2.2.3
# -c requirements/../../../requirements/constraints.txt
# -c requirements/_aiohttp.txt
# -c requirements/_base.txt
# botocore
# docker
# requests
yarl==1.12.1
Expand Down
6 changes: 4 additions & 2 deletions packages/service-library/requirements/_tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ packaging==24.1
# build
pathspec==0.12.1
# via black
pip==24.2
# via pip-tools
pip==24.3.1
# via
# -c requirements/_test.txt
# pip-tools
pip-tools==7.4.1
# via -r requirements/../../../requirements/devenv.txt
platformdirs==4.3.6
Expand Down
50 changes: 42 additions & 8 deletions packages/service-library/src/servicelib/aiohttp/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,39 @@
from opentelemetry.instrumentation.aiohttp_server import (
middleware as aiohttp_server_opentelemetry_middleware, # pylint:disable=no-name-in-module
)
from opentelemetry.instrumentation.aiopg import ( # pylint:disable=no-name-in-module
AiopgInstrumentor,
)
from opentelemetry.instrumentation.requests import RequestsInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from servicelib.logging_utils import log_context
from settings_library.tracing import TracingSettings

_logger = logging.getLogger(__name__)
try:
from opentelemetry.instrumentation.botocore import ( # type: ignore[import-not-found]
BotocoreInstrumentor,
)

HAS_BOTOCORE = True
except ImportError:
HAS_BOTOCORE = False
try:
from opentelemetry.instrumentation.aiopg import AiopgInstrumentor

HAS_AIOPG = True
except ImportError:
HAS_AIOPG = False
try:
from opentelemetry.instrumentation.requests import RequestsInstrumentor

HAS_REQUESTS = True
except ImportError:
HAS_REQUESTS = False


def setup_tracing(
app: web.Application,
tracing_settings: TracingSettings,
service_name: str,
instrument_aiopg: bool = False, # noqa: FBT001, FBT002
) -> None:
"""
Sets up this service for a distributed tracing system (opentelemetry)
Expand Down Expand Up @@ -91,6 +107,24 @@ def setup_tracing(

# Instrument aiohttp client
AioHttpClientInstrumentor().instrument()
if instrument_aiopg:
AiopgInstrumentor().instrument()
RequestsInstrumentor().instrument()
if HAS_AIOPG:
with log_context(
_logger,
logging.INFO,
msg="Attempting to add aio-pg opentelemetry autoinstrumentation...",
):
AiopgInstrumentor().instrument()
if HAS_BOTOCORE:
with log_context(
_logger,
logging.INFO,
msg="Attempting to add botocore opentelemetry autoinstrumentation...",
):
BotocoreInstrumentor().instrument()
if HAS_REQUESTS:
with log_context(
_logger,
logging.INFO,
msg="Attempting to add requests opentelemetry autoinstrumentation...",
):
RequestsInstrumentor().instrument()
Loading

0 comments on commit cd20a2c

Please sign in to comment.