11
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
+
15
+ import logging
14
16
import os
15
17
from dataclasses import dataclass
16
18
from importlib import import_module , reload
@@ -124,7 +126,10 @@ def setUp(self):
124
126
super ().setUp ()
125
127
self .common_env_patch = mock .patch .dict (
126
128
"os.environ" ,
127
- {_HANDLER : "tests.mocks.lambda_function.handler" },
129
+ {
130
+ _HANDLER : "tests.mocks.lambda_function.handler" ,
131
+ "AWS_LAMBDA_FUNCTION_NAME" : "mylambda" ,
132
+ },
128
133
)
129
134
self .common_env_patch .start ()
130
135
@@ -466,12 +471,14 @@ def test_lambda_handles_handler_exception(self):
466
471
467
472
exc_env_patch .stop ()
468
473
469
- def test_lambda_handles_should_do_nothing_when_environment_variables_not_present (
470
- self ,
474
+ @mock .patch ("opentelemetry.instrumentation.aws_lambda.logger" )
475
+ def test_lambda_handles_should_do_nothing_when_aws_lambda_environment_variables_not_present (
476
+ self , logger_mock
471
477
):
472
478
exc_env_patch = mock .patch .dict (
473
479
"os.environ" ,
474
- {_HANDLER : "" },
480
+ {_HANDLER : "tests.mocks.lambda_function.handler" },
481
+ clear = True ,
475
482
)
476
483
exc_env_patch .start ()
477
484
AwsLambdaInstrumentor ().instrument ()
@@ -480,6 +487,29 @@ def test_lambda_handles_should_do_nothing_when_environment_variables_not_present
480
487
self .assertEqual (len (spans ), 0 )
481
488
exc_env_patch .stop ()
482
489
490
+ logger_mock .warnings .assert_not_called ()
491
+
492
+ def test_lambda_handles_should_warn_when_handler_environment_variable_not_present (
493
+ self ,
494
+ ):
495
+ exc_env_patch = mock .patch .dict (
496
+ "os.environ" ,
497
+ {"AWS_LAMBDA_FUNCTION_NAME" : "mylambda" },
498
+ clear = True ,
499
+ )
500
+ exc_env_patch .start ()
501
+ with self .assertLogs (level = logging .WARNING ) as warning :
502
+ AwsLambdaInstrumentor ().instrument ()
503
+ self .assertEqual (len (warning .records ), 1 )
504
+ self .assertIn (
505
+ "This instrumentation requires the OpenTelemetry Lambda extension installed" ,
506
+ warning .records [0 ].message ,
507
+ )
508
+
509
+ spans = self .memory_exporter .get_finished_spans ()
510
+ self .assertEqual (len (spans ), 0 )
511
+ exc_env_patch .stop ()
512
+
483
513
def test_uninstrument (self ):
484
514
AwsLambdaInstrumentor ().instrument ()
485
515
0 commit comments