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

PoC: Add request_hook to all loaded instr libraries #123

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
40 changes: 40 additions & 0 deletions solarwinds_apm/distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@

"""Module to configure OpenTelemetry to work with SolarWinds backend"""

import logging
from os import environ
from pkg_resources import EntryPoint
from types import MappingProxyType

from opentelemetry.environment_variables import (
OTEL_PROPAGATORS,
OTEL_TRACES_EXPORTER,
)
from opentelemetry.instrumentation.distro import BaseDistro
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor

from solarwinds_apm.apm_constants import (
INTL_SWO_DEFAULT_PROPAGATORS,
INTL_SWO_DEFAULT_TRACES_EXPORTER,
)


logger = logging.getLogger(__name__)

class SolarWindsDistro(BaseDistro):
"""OpenTelemetry Distro for SolarWinds reporting environment"""

Expand All @@ -31,3 +37,37 @@ def _configure(self, **kwargs):
environ.setdefault(
OTEL_PROPAGATORS, ",".join(INTL_SWO_DEFAULT_PROPAGATORS)
)

def load_instrumentor( # pylint: disable=no-self-use
self, entry_point: EntryPoint, **kwargs
):
"""
Override of BaseDistro method that loads and instrument() of all installed OTel instrumentation libraries.
"""
def request_hook(span, request):
"""
request_hook for instrumentation libraries that use them.
Edits attributes of spans created from receiving requests.

See also Django usage
https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/__init__.py#L124-L141
"""
logger.debug("request_hook received span:")
logger.debug("type %s", type(span))
logger.debug("name %s", span.name)
logger.debug("kind %s", span.kind)
logger.debug("attributes %s", span.attributes)
logger.debug("resource %s", span.resource)
logger.debug("request_hook received Django request:")
logger.debug("path %s", request.path)
logger.debug("headers %s", request.headers)

new_attributes = {"request-hook-foo": "some-bar-value"}
for attr_k, attr_v in span.attributes.items():
new_attributes[attr_k] = attr_v
span.set_attributes(MappingProxyType(new_attributes))
logger.debug("Updated span attributes is %s", span.attributes)

kwargs.update({"request_hook": request_hook})
instrumentor: BaseInstrumentor = entry_point.load()
instrumentor().instrument(**kwargs)