Skip to content

Commit

Permalink
Merge branch 'main' into psycopg2-binary
Browse files Browse the repository at this point in the history
  • Loading branch information
emdneto authored Jan 21, 2025
2 parents dc168e9 + 37f85bf commit 335bdcb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,11 @@ def _instrument(self, **kwargs):
the context is extracted from the HTTP headers of an API Gateway
request.
"""

# Don't try if we are not running on AWS Lambda
if "AWS_LAMBDA_FUNCTION_NAME" not in os.environ:
return

lambda_handler = os.environ.get(ORIG_HANDLER, os.environ.get(_HANDLER))
if not lambda_handler:
logger.warning(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os
from dataclasses import dataclass
from importlib import import_module, reload
Expand Down Expand Up @@ -124,7 +126,10 @@ def setUp(self):
super().setUp()
self.common_env_patch = mock.patch.dict(
"os.environ",
{_HANDLER: "tests.mocks.lambda_function.handler"},
{
_HANDLER: "tests.mocks.lambda_function.handler",
"AWS_LAMBDA_FUNCTION_NAME": "mylambda",
},
)
self.common_env_patch.start()

Expand Down Expand Up @@ -466,12 +471,14 @@ def test_lambda_handles_handler_exception(self):

exc_env_patch.stop()

def test_lambda_handles_should_do_nothing_when_environment_variables_not_present(
self,
@mock.patch("opentelemetry.instrumentation.aws_lambda.logger")
def test_lambda_handles_should_do_nothing_when_aws_lambda_environment_variables_not_present(
self, logger_mock
):
exc_env_patch = mock.patch.dict(
"os.environ",
{_HANDLER: ""},
{_HANDLER: "tests.mocks.lambda_function.handler"},
clear=True,
)
exc_env_patch.start()
AwsLambdaInstrumentor().instrument()
Expand All @@ -480,6 +487,29 @@ def test_lambda_handles_should_do_nothing_when_environment_variables_not_present
self.assertEqual(len(spans), 0)
exc_env_patch.stop()

logger_mock.warnings.assert_not_called()

def test_lambda_handles_should_warn_when_handler_environment_variable_not_present(
self,
):
exc_env_patch = mock.patch.dict(
"os.environ",
{"AWS_LAMBDA_FUNCTION_NAME": "mylambda"},
clear=True,
)
exc_env_patch.start()
with self.assertLogs(level=logging.WARNING) as warning:
AwsLambdaInstrumentor().instrument()
self.assertEqual(len(warning.records), 1)
self.assertIn(
"This instrumentation requires the OpenTelemetry Lambda extension installed",
warning.records[0].message,
)

spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 0)
exc_env_patch.stop()

def test_uninstrument(self):
AwsLambdaInstrumentor().instrument()

Expand Down

0 comments on commit 335bdcb

Please sign in to comment.