Skip to content

Commit 7d6da2c

Browse files
lrafeeimergify[bot]umaannamalaiTimPansinohmstepanek
authored
Azure init container setting flag (#1223)
* Initial commit for azure sidecar * Merge changes from main * Change Azure init container env var name * Pin gRPC version supported in Python 3.7 * Update tox.ini Co-authored-by: Uma Annamalai <[email protected]> * Clean up tox runners Co-authored-by: Uma Annamalai <[email protected]> Co-authored-by: Timothy Pansino <[email protected]> Co-authored-by: Hannah Stepanek <[email protected]> --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Uma Annamalai <[email protected]> Co-authored-by: Uma Annamalai <[email protected]> Co-authored-by: Timothy Pansino <[email protected]> Co-authored-by: Hannah Stepanek <[email protected]> Co-authored-by: Timothy Pansino <[email protected]>
1 parent b2d8cc5 commit 7d6da2c

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

newrelic/bootstrap/sitecustomize.py

+24-20
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import os
1616
import sys
1717
import time
18-
1918
from importlib.machinery import PathFinder
2019

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

112112
log_message("python_prefix_matches = %r", python_prefix_matches)
113113
log_message("python_version_matches = %r", python_version_matches)
114114
log_message("k8s_operator_enabled = %r", k8s_operator_enabled)
115+
log_message("azure_operator_enabled = %r", azure_operator_enabled)
115116

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

131132
if initialize_agent:
132-
if not k8s_operator_enabled:
133-
# When installed as an egg with buildout, the root directory for
134-
# packages is not listed in sys.path and scripts instead set it
135-
# after Python has started up. This will cause importing of
136-
# 'newrelic' module to fail. What we do is see if the root
137-
# directory where the package is held is in sys.path and if not
138-
# insert it. For good measure we remove it after having imported
139-
# 'newrelic' module to reduce chance that will cause any issues.
140-
# If it is a buildout created script, it will replace the whole
141-
# sys.path again later anyway.
142-
root_directory = os.path.dirname(os.path.dirname(boot_directory))
143-
log_message("root_directory = %r", root_directory)
144-
145-
new_relic_path = root_directory
146-
do_insert_path = root_directory not in sys.path
147-
else:
148-
# When installed with the kubernetes operator, we need to attempt
149-
# to find a distribution from our initcontainer that matches the
133+
if k8s_operator_enabled or azure_operator_enabled:
134+
# When installed with either the kubernetes operator or the
135+
# azure operator functionality enabled, we need to attempt to
136+
# find a distribution from our initcontainer that matches the
150137
# current environment. For wheels, this is platform dependent and we
151138
# rely on pip to identify the correct wheel to use. If no suitable
152139
# wheel can be found, we will fall back to the sdist and disable
@@ -155,12 +142,29 @@ def del_sys_path_entry(path):
155142
# the 'newrelic' module later and use our APIs in their code.
156143
try:
157144
sys.path.insert(0, boot_directory)
145+
# Will use the same file for k8s as well as Azure since the functionality
146+
# will remain the same. File may be renamed in the near future.
158147
from newrelic_k8s_operator import find_supported_newrelic_distribution
159148
finally:
160149
del_sys_path_entry(boot_directory)
161150

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

165169
# Now that the appropriate location of the module has been identified,
166170
# either by the kubernetes operator or this script, we are ready to import

newrelic/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ def _process_configuration(section):
554554
_process_setting(section, "ai_monitoring.record_content.enabled", "getboolean", None)
555555
_process_setting(section, "ai_monitoring.streaming.enabled", "getboolean", None)
556556
_process_setting(section, "k8s_operator.enabled", "getboolean", None)
557+
_process_setting(section, "azure_operator.enabled", "getboolean", None)
557558
_process_setting(section, "package_reporting.enabled", "getboolean", None)
558559

559560

newrelic/core/config.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
from newrelic.core.attribute import MAX_ATTRIBUTE_LENGTH
3535
from newrelic.core.attribute_filter import AttributeFilter
3636

37-
3837
try:
3938
import grpc
4039

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

8483

85-
class Settings():
84+
class Settings:
8685
nested = False
8786

8887
def __repr__(self):
@@ -162,6 +161,10 @@ class K8sOperatorSettings(Settings):
162161
pass
163162

164163

164+
class AzureOperatorSettings(Settings):
165+
pass
166+
167+
165168
class PackageReportingSettings(Settings):
166169
pass
167170

@@ -431,6 +434,7 @@ class EventHarvestConfigHarvestLimitSettings(Settings):
431434
_settings.ai_monitoring.streaming = AIMonitoringStreamingSettings()
432435
_settings.ai_monitoring.record_content = AIMonitoringRecordContentSettings()
433436
_settings.k8s_operator = K8sOperatorSettings()
437+
_settings.azure_operator = AzureOperatorSettings()
434438
_settings.package_reporting = PackageReportingSettings()
435439
_settings.attributes = AttributesSettings()
436440
_settings.browser_monitoring = BrowserMonitorSettings()
@@ -957,6 +961,7 @@ def default_otlp_host(host):
957961
)
958962
_settings.ai_monitoring._llm_token_count_callback = None
959963
_settings.k8s_operator.enabled = _environ_as_bool("NEW_RELIC_K8S_OPERATOR_ENABLED", default=False)
964+
_settings.azure_operator.enabled = _environ_as_bool("NEW_RELIC_AZURE_OPERATOR_ENABLED", default=False)
960965
_settings.package_reporting.enabled = _environ_as_bool("NEW_RELIC_PACKAGE_REPORTING_ENABLED", default=True)
961966
_settings.ml_insights_events.enabled = _environ_as_bool("NEW_RELIC_ML_INSIGHTS_EVENTS_ENABLED", default=False)
962967

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

1094-
if (
1095-
not isinstance(value, str)
1096-
and not isinstance(value, float)
1097-
and not isinstance(value, int)
1098-
):
1099+
if not isinstance(value, str) and not isinstance(value, float) and not isinstance(value, int):
10991100
settings[key] = repr(value)
11001101

11011102
return settings

0 commit comments

Comments
 (0)