@@ -23,8 +23,9 @@ def test_update_opentelemetry_status_import_error(self):
23
23
# Patch the built-in import mechanism
24
24
with patch ('builtins.__import__' , side_effect = ImportError ):
25
25
self .dispatcher .update_opentelemetry_status ()
26
- # Verify that otel_libs_available is set to False due to ImportError
27
- self .assertFalse (self .dispatcher ._azure_monitor_available )
26
+ # Verify that context variables are None due to ImportError
27
+ self .assertIsNone (self .dispatcher ._context_api )
28
+ self .assertIsNone (self .dispatcher ._trace_context_propagator )
28
29
29
30
@patch ('builtins.__import__' )
30
31
def test_update_opentelemetry_status_success (
@@ -54,12 +55,12 @@ def test_initialize_azure_monitor_import_error(
54
55
with patch ('builtins.__import__' , side_effect = ImportError ):
55
56
self .dispatcher .initialize_azure_monitor ()
56
57
mock_update_ot .assert_called_once ()
57
- # Verify that otel_libs_available is set to False due to ImportError
58
+ # Verify that azure_monitor_available is set to False due to ImportError
58
59
self .assertFalse (self .dispatcher ._azure_monitor_available )
59
60
60
- @patch .dict (os .environ , {'PYTHON_ENABLE_OPENTELEMETRY ' : 'true' })
61
+ @patch .dict (os .environ , {'PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY ' : 'true' })
61
62
@patch ('builtins.__import__' )
62
- def test_init_request_otel_capability_enabled_app_setting (
63
+ def test_init_request_initialize_azure_monitor_enabled_app_setting (
63
64
self ,
64
65
mock_imports ,
65
66
):
@@ -78,13 +79,15 @@ def test_init_request_otel_capability_enabled_app_setting(
78
79
self .assertEqual (init_response .worker_init_response .result .status ,
79
80
protos .StatusResult .Success )
80
81
82
+ # Verify azure_monitor_available is set to True
83
+ self .assertTrue (self .dispatcher ._azure_monitor_available )
81
84
# Verify that WorkerOpenTelemetryEnabled capability is set to _TRUE
82
85
capabilities = init_response .worker_init_response .capabilities
83
86
self .assertIn ("WorkerOpenTelemetryEnabled" , capabilities )
84
87
self .assertEqual (capabilities ["WorkerOpenTelemetryEnabled" ], "true" )
85
88
86
89
@patch ("azure_functions_worker.dispatcher.Dispatcher.initialize_azure_monitor" )
87
- def test_init_request_otel_capability_disabled_app_setting (
90
+ def test_init_request_initialize_azure_monitor_default_app_setting (
88
91
self ,
89
92
mock_initialize_azmon ,
90
93
):
@@ -103,8 +106,113 @@ def test_init_request_otel_capability_disabled_app_setting(
103
106
protos .StatusResult .Success )
104
107
105
108
# Azure monitor initialized not called
109
+ # Since default behavior is not enabled
106
110
mock_initialize_azmon .assert_not_called ()
107
111
112
+ # Verify azure_monitor_available is set to False
113
+ self .assertFalse (self .dispatcher ._azure_monitor_available )
114
+ # Verify that WorkerOpenTelemetryEnabled capability is not set
115
+ capabilities = init_response .worker_init_response .capabilities
116
+ self .assertNotIn ("WorkerOpenTelemetryEnabled" , capabilities )
117
+
118
+ @patch .dict (os .environ , {'PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY' : 'false' })
119
+ @patch ("azure_functions_worker.dispatcher.Dispatcher.initialize_azure_monitor" )
120
+ def test_init_request_initialize_azure_monitor_disabled_app_setting (
121
+ self ,
122
+ mock_initialize_azmon ,
123
+ ):
124
+
125
+ init_request = protos .StreamingMessage (
126
+ worker_init_request = protos .WorkerInitRequest (
127
+ host_version = "2.3.4" ,
128
+ function_app_directory = str (FUNCTION_APP_DIRECTORY )
129
+ )
130
+ )
131
+
132
+ init_response = self .loop .run_until_complete (
133
+ self .dispatcher ._handle__worker_init_request (init_request ))
134
+
135
+ self .assertEqual (init_response .worker_init_response .result .status ,
136
+ protos .StatusResult .Success )
137
+
138
+ # Azure monitor initialized not called
139
+ mock_initialize_azmon .assert_not_called ()
140
+
141
+ # Verify azure_monitor_available is set to False
142
+ self .assertFalse (self .dispatcher ._azure_monitor_available )
143
+ # Verify that WorkerOpenTelemetryEnabled capability is not set
144
+ capabilities = init_response .worker_init_response .capabilities
145
+ self .assertNotIn ("WorkerOpenTelemetryEnabled" , capabilities )
146
+
147
+ @patch .dict (os .environ , {'PYTHON_ENABLE_OPENTELEMETRY' : 'true' })
148
+ def test_init_request_enable_opentelemetry_enabled_app_setting (
149
+ self ,
150
+ ):
151
+
152
+ init_request = protos .StreamingMessage (
153
+ worker_init_request = protos .WorkerInitRequest (
154
+ host_version = "2.3.4" ,
155
+ function_app_directory = str (FUNCTION_APP_DIRECTORY )
156
+ )
157
+ )
158
+
159
+ init_response = self .loop .run_until_complete (
160
+ self .dispatcher ._handle__worker_init_request (init_request ))
161
+
162
+ self .assertEqual (init_response .worker_init_response .result .status ,
163
+ protos .StatusResult .Success )
164
+
165
+ # Verify otel_libs_available is set to True
166
+ self .assertTrue (self .dispatcher ._otel_libs_available )
167
+ # Verify that WorkerOpenTelemetryEnabled capability is set to _TRUE
168
+ capabilities = init_response .worker_init_response .capabilities
169
+ self .assertIn ("WorkerOpenTelemetryEnabled" , capabilities )
170
+ self .assertEqual (capabilities ["WorkerOpenTelemetryEnabled" ], "true" )
171
+
172
+ @patch .dict (os .environ , {'PYTHON_ENABLE_OPENTELEMETRY' : 'false' })
173
+ def test_init_request_enable_opentelemetry_default_app_setting (
174
+ self ,
175
+ ):
176
+
177
+ init_request = protos .StreamingMessage (
178
+ worker_init_request = protos .WorkerInitRequest (
179
+ host_version = "2.3.4" ,
180
+ function_app_directory = str (FUNCTION_APP_DIRECTORY )
181
+ )
182
+ )
183
+
184
+ init_response = self .loop .run_until_complete (
185
+ self .dispatcher ._handle__worker_init_request (init_request ))
186
+
187
+ self .assertEqual (init_response .worker_init_response .result .status ,
188
+ protos .StatusResult .Success )
189
+
190
+ # Verify otel_libs_available is set to False by default
191
+ self .assertFalse (self .dispatcher ._otel_libs_available )
192
+ # Verify that WorkerOpenTelemetryEnabled capability is not set
193
+ capabilities = init_response .worker_init_response .capabilities
194
+ self .assertNotIn ("WorkerOpenTelemetryEnabled" , capabilities )
195
+
196
+ @patch .dict (os .environ , {'PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY' : 'false' })
197
+ def test_init_request_enable_azure_monitor_disabled_app_setting (
198
+ self ,
199
+ ):
200
+
201
+ init_request = protos .StreamingMessage (
202
+ worker_init_request = protos .WorkerInitRequest (
203
+ host_version = "2.3.4" ,
204
+ function_app_directory = str (FUNCTION_APP_DIRECTORY )
205
+ )
206
+ )
207
+
208
+ init_response = self .loop .run_until_complete (
209
+ self .dispatcher ._handle__worker_init_request (init_request ))
210
+
211
+ self .assertEqual (init_response .worker_init_response .result .status ,
212
+ protos .StatusResult .Success )
213
+
214
+ # Verify otel_libs_available is set to False by default
215
+ self .assertFalse (self .dispatcher ._otel_libs_available )
108
216
# Verify that WorkerOpenTelemetryEnabled capability is not set
109
217
capabilities = init_response .worker_init_response .capabilities
110
218
self .assertNotIn ("WorkerOpenTelemetryEnabled" , capabilities )
0 commit comments