1111# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212# See the License for the specific language governing permissions and
1313# limitations under the License.
14+
15+ import logging
1416import os
1517from dataclasses import dataclass
1618from importlib import import_module , reload
@@ -124,7 +126,10 @@ def setUp(self):
124126 super ().setUp ()
125127 self .common_env_patch = mock .patch .dict (
126128 "os.environ" ,
127- {_HANDLER : "tests.mocks.lambda_function.handler" },
129+ {
130+ _HANDLER : "tests.mocks.lambda_function.handler" ,
131+ "AWS_LAMBDA_FUNCTION_NAME" : "mylambda" ,
132+ },
128133 )
129134 self .common_env_patch .start ()
130135
@@ -466,12 +471,14 @@ def test_lambda_handles_handler_exception(self):
466471
467472 exc_env_patch .stop ()
468473
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
471477 ):
472478 exc_env_patch = mock .patch .dict (
473479 "os.environ" ,
474- {_HANDLER : "" },
480+ {_HANDLER : "tests.mocks.lambda_function.handler" },
481+ clear = True ,
475482 )
476483 exc_env_patch .start ()
477484 AwsLambdaInstrumentor ().instrument ()
@@ -480,6 +487,29 @@ def test_lambda_handles_should_do_nothing_when_environment_variables_not_present
480487 self .assertEqual (len (spans ), 0 )
481488 exc_env_patch .stop ()
482489
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+
483513 def test_uninstrument (self ):
484514 AwsLambdaInstrumentor ().instrument ()
485515
0 commit comments