1515import os
1616import sys
1717import time
18-
1918from 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):
108107python_prefix_matches = expected_python_prefix == actual_python_prefix
109108python_version_matches = expected_python_version == actual_python_version
110109k8s_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
112112log_message ("python_prefix_matches = %r" , python_prefix_matches )
113113log_message ("python_version_matches = %r" , python_version_matches )
114114log_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
0 commit comments