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

[WIP/PoC] NH-75342 Set logging handler based on log_correlation #356

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions solarwinds_apm/apm_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@
INTL_SWO_SUPPORT_EMAIL,
INTL_SWO_TRACECONTEXT_PROPAGATOR,
)
from solarwinds_apm.apm_logging import _stream_handler
from solarwinds_apm.certs.ao_issuer_ca import get_public_cert

logger = logging.getLogger(__name__)
# This startup component must always log
if not logger.hasHandlers():
logger.addHandler(_stream_handler)


class OboeTracingMode:
Expand Down
21 changes: 16 additions & 5 deletions solarwinds_apm/apm_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
import os
from logging.handlers import RotatingFileHandler

from opentelemetry.instrumentation.logging.environment_variables import (
OTEL_PYTHON_LOG_CORRELATION,
)


class ApmLoggingType:
"""Mapping of supported solarwinds_apm library log types"""
Expand Down Expand Up @@ -142,6 +146,9 @@ def _get_logger():
- When _get_logger is invoked, SW_APM_DEBUG_LEVEL is checked and the logging level will be set to the
value provided by the variable. If an invalid value has been provided, the logging level will not be changed.
(2) By invoking set_sw_log_level

Special case: if the environment variable OTEL_PYTHON_LOG_CORRELATION is set, then APM Python
will not set up its own stream handling.
"""

# create base logger for solarwinds_apm package
Expand All @@ -163,11 +170,15 @@ def _get_logger():

_logger.setLevel(ApmLoggingLevel.logging_map[log_level])

if log_level != ApmLoggingLevel.debug_levels["OBOE_DEBUG_DISABLE"]:
_logger.addHandler(_stream_handler)
else:
_logger.propagate = False
_logger.addHandler(logging.NullHandler())
upstream_log_correlation = (
os.environ.get(OTEL_PYTHON_LOG_CORRELATION, "false").lower() == "true"
)
if not upstream_log_correlation:
if log_level != ApmLoggingLevel.debug_levels["OBOE_DEBUG_DISABLE"]:
_logger.addHandler(_stream_handler)
else:
_logger.propagate = False
_logger.addHandler(logging.NullHandler())

return _logger

Expand Down
4 changes: 4 additions & 0 deletions solarwinds_apm/configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
INTL_SWO_SUPPORT_EMAIL,
)
from solarwinds_apm.apm_fwkv_manager import SolarWindsFrameworkKvManager
from solarwinds_apm.apm_logging import _stream_handler
from solarwinds_apm.apm_oboe_codes import OboeReporterCode
from solarwinds_apm.apm_txname_manager import SolarWindsTxnNameManager
from solarwinds_apm.response_propagator import (
Expand All @@ -73,6 +74,9 @@

solarwinds_apm_logger = apm_logging.logger
logger = logging.getLogger(__name__)
# This startup component must always log
if not logger.hasHandlers():
logger.addHandler(_stream_handler)


class SolarWindsConfigurator(_OTelSDKConfigurator):
Expand Down
4 changes: 4 additions & 0 deletions solarwinds_apm/distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
INTL_SWO_DEFAULT_PROPAGATORS,
INTL_SWO_DEFAULT_TRACES_EXPORTER,
)
from solarwinds_apm.apm_logging import _stream_handler
from solarwinds_apm.version import __version__ as apm_version

_EXPORTER_BY_OTLP_PROTOCOL = {
Expand All @@ -41,6 +42,9 @@
}

logger = logging.getLogger(__name__)
# This startup component must always log
if not logger.hasHandlers():
logger.addHandler(_stream_handler)


class SolarWindsDistro(BaseDistro):
Expand Down