Skip to content

Commit 37f85bf

Browse files
authored
instrumentation/aws-lambda: don't print warnings outside of AWS Lambda (#3183)
If we are not running inside AWS Lambda don't print warnings on missing OTel lambda extension layer. The instrumentation is installed by the OTel k8s operator and so this warning may confuse users.
1 parent 20413ef commit 37f85bf

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,11 @@ def _instrument(self, **kwargs):
431431
the context is extracted from the HTTP headers of an API Gateway
432432
request.
433433
"""
434+
435+
# Don't try if we are not running on AWS Lambda
436+
if "AWS_LAMBDA_FUNCTION_NAME" not in os.environ:
437+
return
438+
434439
lambda_handler = os.environ.get(ORIG_HANDLER, os.environ.get(_HANDLER))
435440
if not lambda_handler:
436441
logger.warning(

instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
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
1416
import os
1517
from dataclasses import dataclass
1618
from 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

Comments
 (0)