Skip to content

Commit

Permalink
Merge branch 'main' into support-lambda-entities
Browse files Browse the repository at this point in the history
  • Loading branch information
TimPansino authored Oct 24, 2024
2 parents 9033706 + 7d6da2c commit f9aa807
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
44 changes: 24 additions & 20 deletions newrelic/bootstrap/sitecustomize.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import os
import sys
import time

from importlib.machinery import PathFinder

# Define some debug logging routines to help sort out things when this
Expand Down Expand Up @@ -108,12 +107,14 @@ def del_sys_path_entry(path):
python_prefix_matches = expected_python_prefix == actual_python_prefix
python_version_matches = expected_python_version == actual_python_version
k8s_operator_enabled = os.environ.get("NEW_RELIC_K8S_OPERATOR_ENABLED", "off").lower() in ("on", "true", "1")
azure_operator_enabled = os.environ.get("NEW_RELIC_AZURE_OPERATOR_ENABLED", "off").lower() in ("on", "true", "1")

log_message("python_prefix_matches = %r", python_prefix_matches)
log_message("python_version_matches = %r", python_version_matches)
log_message("k8s_operator_enabled = %r", k8s_operator_enabled)
log_message("azure_operator_enabled = %r", azure_operator_enabled)

if k8s_operator_enabled or (python_prefix_matches and python_version_matches):
if k8s_operator_enabled or azure_operator_enabled or (python_prefix_matches and python_version_matches):
# We also need to skip agent initialisation if neither the license
# key or config file environment variables are set. We do this as
# some people like to use a common startup script which always uses
Expand All @@ -129,24 +130,10 @@ def del_sys_path_entry(path):
log_message("initialize_agent = %r", initialize_agent)

if initialize_agent:
if not k8s_operator_enabled:
# When installed as an egg with buildout, the root directory for
# packages is not listed in sys.path and scripts instead set it
# after Python has started up. This will cause importing of
# 'newrelic' module to fail. What we do is see if the root
# directory where the package is held is in sys.path and if not
# insert it. For good measure we remove it after having imported
# 'newrelic' module to reduce chance that will cause any issues.
# If it is a buildout created script, it will replace the whole
# sys.path again later anyway.
root_directory = os.path.dirname(os.path.dirname(boot_directory))
log_message("root_directory = %r", root_directory)

new_relic_path = root_directory
do_insert_path = root_directory not in sys.path
else:
# When installed with the kubernetes operator, we need to attempt
# to find a distribution from our initcontainer that matches the
if k8s_operator_enabled or azure_operator_enabled:
# When installed with either the kubernetes operator or the
# azure operator functionality enabled, we need to attempt to
# find a distribution from our initcontainer that matches the
# current environment. For wheels, this is platform dependent and we
# rely on pip to identify the correct wheel to use. If no suitable
# wheel can be found, we will fall back to the sdist and disable
Expand All @@ -155,12 +142,29 @@ def del_sys_path_entry(path):
# the 'newrelic' module later and use our APIs in their code.
try:
sys.path.insert(0, boot_directory)
# Will use the same file for k8s as well as Azure since the functionality
# will remain the same. File may be renamed in the near future.
from newrelic_k8s_operator import find_supported_newrelic_distribution
finally:
del_sys_path_entry(boot_directory)

new_relic_path = find_supported_newrelic_distribution()
do_insert_path = True
else:
# When installed as an egg with buildout, the root directory for
# packages is not listed in sys.path and scripts instead set it
# after Python has started up. This will cause importing of
# 'newrelic' module to fail. What we do is see if the root
# directory where the package is held is in sys.path and if not
# insert it. For good measure we remove it after having imported
# 'newrelic' module to reduce chance that will cause any issues.
# If it is a buildout created script, it will replace the whole
# sys.path again later anyway.
root_directory = os.path.dirname(os.path.dirname(boot_directory))
log_message("root_directory = %r", root_directory)

new_relic_path = root_directory
do_insert_path = root_directory not in sys.path

# Now that the appropriate location of the module has been identified,
# either by the kubernetes operator or this script, we are ready to import
Expand Down
1 change: 1 addition & 0 deletions newrelic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ def _process_configuration(section):
_process_setting(section, "ai_monitoring.record_content.enabled", "getboolean", None)
_process_setting(section, "ai_monitoring.streaming.enabled", "getboolean", None)
_process_setting(section, "k8s_operator.enabled", "getboolean", None)
_process_setting(section, "azure_operator.enabled", "getboolean", None)
_process_setting(section, "package_reporting.enabled", "getboolean", None)


Expand Down
15 changes: 8 additions & 7 deletions newrelic/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from newrelic.core.attribute import MAX_ATTRIBUTE_LENGTH
from newrelic.core.attribute_filter import AttributeFilter


try:
import grpc

Expand Down Expand Up @@ -82,7 +81,7 @@ def emit(self, record):
# sub categories we don't know about.


class Settings():
class Settings:
nested = False

def __repr__(self):
Expand Down Expand Up @@ -162,6 +161,10 @@ class K8sOperatorSettings(Settings):
pass


class AzureOperatorSettings(Settings):
pass


class PackageReportingSettings(Settings):
pass

Expand Down Expand Up @@ -431,6 +434,7 @@ class EventHarvestConfigHarvestLimitSettings(Settings):
_settings.ai_monitoring.streaming = AIMonitoringStreamingSettings()
_settings.ai_monitoring.record_content = AIMonitoringRecordContentSettings()
_settings.k8s_operator = K8sOperatorSettings()
_settings.azure_operator = AzureOperatorSettings()
_settings.package_reporting = PackageReportingSettings()
_settings.attributes = AttributesSettings()
_settings.browser_monitoring = BrowserMonitorSettings()
Expand Down Expand Up @@ -957,6 +961,7 @@ def default_otlp_host(host):
)
_settings.ai_monitoring._llm_token_count_callback = None
_settings.k8s_operator.enabled = _environ_as_bool("NEW_RELIC_K8S_OPERATOR_ENABLED", default=False)
_settings.azure_operator.enabled = _environ_as_bool("NEW_RELIC_AZURE_OPERATOR_ENABLED", default=False)
_settings.package_reporting.enabled = _environ_as_bool("NEW_RELIC_PACKAGE_REPORTING_ENABLED", default=True)
_settings.ml_insights_events.enabled = _environ_as_bool("NEW_RELIC_ML_INSIGHTS_EVENTS_ENABLED", default=False)

Expand Down Expand Up @@ -1091,11 +1096,7 @@ def global_settings_dump(settings_object=None, serializable=False):
if not isinstance(key, str):
del settings[key]

if (
not isinstance(value, str)
and not isinstance(value, float)
and not isinstance(value, int)
):
if not isinstance(value, str) and not isinstance(value, float) and not isinstance(value, int):
settings[key] = repr(value)

return settings
Expand Down
15 changes: 8 additions & 7 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ envlist =
elasticsearchserver07-datastore_elasticsearch-{py37,py38,py39,py310,py311,py312,py313,pypy310}-elasticsearch07,
elasticsearchserver08-datastore_elasticsearch-{py37,py38,py39,py310,py311,py312,py313,pypy310}-elasticsearch08,
firestore-datastore_firestore-{py37,py38,py39,py310,py311,py312,py313},
grpc-framework_grpc-{py37,py38,py39,py310,py311,py312,py313}-grpclatest,
grpc-framework_grpc-py37-grpc0167,
grpc-framework_grpc-{py38,py39,py310,py311,py312,py313}-grpclatest,
kafka-messagebroker_confluentkafka-py39-confluentkafka{0108,0107,0106},
kafka-messagebroker_confluentkafka-{py37,py38,py39,py310,py311,py312}-confluentkafkalatest,
;; Package not ready for Python 3.13 (missing wheel)
Expand Down Expand Up @@ -216,6 +217,9 @@ deps =
adapter_waitress-waitresslatest: waitress
agent_features: beautifulsoup4
agent_features: protobuf
agent_streaming-protobuf04: protobuf<5
agent_streaming-protobuf03: protobuf<4
agent_streaming-protobuf0319: protobuf<3.20
application_celery-celerylatest: celery[pytest]
application_celery-celery0504: celery[pytest]<5.5
application_celery-celery0503: celery[pytest]<5.4
Expand Down Expand Up @@ -343,12 +347,9 @@ deps =
framework_grpc-grpclatest: protobuf
framework_grpc-grpclatest: grpcio
framework_grpc-grpclatest: grpcio-tools
grpc0125: grpcio<1.26
grpc0125: grpcio-tools<1.26
grpc0125: protobuf<3.18.0
protobuf0319: protobuf<3.20
protobuf03: protobuf<4
protobuf04: protobuf<5
framework_grpc-grpc0167: grpcio<1.67
framework_grpc-grpc0167: grpcio-tools<1.67
framework_grpc-grpc0167: protobuf<5
framework_pyramid: routes
framework_pyramid-cornice: cornice!=5.0.0
framework_pyramid-Pyramidlatest: Pyramid
Expand Down

0 comments on commit f9aa807

Please sign in to comment.