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

NH-32409 Fix failed ASGI version lookup logs #108

Merged
merged 3 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- SolarWinds c-lib 12.0.0, for gRPC upgrade ([#107](https://github.com/solarwindscloud/solarwinds-apm-python/pull/107))
- Bugfix: fix version lookup failures for instrumented ASGI libraries ([#108](https://github.com/solarwindscloud/solarwinds-apm-python/pull/108))

### Removed
- Drop centos7 support ([#107](https://github.com/solarwindscloud/solarwinds-apm-python/pull/107))
Expand Down
49 changes: 48 additions & 1 deletion solarwinds_apm/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from typing import Any

from opentelemetry.sdk.trace.export import SpanExporter
from opentelemetry.trace import SpanKind
from pkg_resources import get_distribution

from solarwinds_apm.apm_constants import (
Expand All @@ -36,6 +37,20 @@ class SolarWindsSpanExporter(SpanExporter):
Initialization requires a liboboe reporter.
"""

_ASGI_APP_IMPLEMENTATIONS = [
"fastapi", # based on starlette, so higher up
"starlette",
"channels",
"quart",
"sanic",
"rpc.py",
"a2wsgi",
]
_ASGI_SERVER_IMPLEMENTATIONS = [
"uvicorn",
"hypercorn",
"daphne",
]
_INTERNAL_TRANSACTION_NAME = "TransactionName"
_SW_SPAN_KIND = "sw.span_kind"

Expand Down Expand Up @@ -138,7 +153,7 @@ def _add_info_instrumentation_scope(self, span, evt) -> None:
INTL_SWO_OTEL_SCOPE_VERSION, span.instrumentation_scope.version
)

# pylint: disable=too-many-branches
# pylint: disable=too-many-branches,too-many-statements
def _add_info_instrumented_framework(self, span, evt) -> None:
"""Add info to span for which Python framework has been instrumented
with OTel. Based on instrumentation scope of the span, if present.
Expand All @@ -159,6 +174,38 @@ def _add_info_instrumented_framework(self, span, evt) -> None:
framework = "psutil"
elif framework == "tortoiseorm":
framework = "tortoise"
# asgi is implemented over multiple frameworks
# https://asgi.readthedocs.io/en/latest/implementations.html
# Use the first best guess framework for name and version
# TODO Increase precision
elif framework == "asgi":
if span.kind == SpanKind.SERVER:
for asgi_impl in self._ASGI_SERVER_IMPLEMENTATIONS:
try:
importlib.import_module(asgi_impl)
except (AttributeError, ImportError):
continue
else:
logger.debug(
"Setting %s as instrumented ASGI server framework span KV",
asgi_impl,
)
framework = asgi_impl
break
else:
# SpanKind.INTERNAL might be common for async
for asgi_impl in self._ASGI_APP_IMPLEMENTATIONS:
try:
importlib.import_module(asgi_impl)
except (AttributeError, ImportError):
continue
else:
logger.debug(
"Setting %s as instrumented ASGI application framework span KV",
asgi_impl,
)
framework = asgi_impl
break

instr_key = f"Python.{framework}.Version"
if "grpc_" in framework:
Expand Down