mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-07-28 20:52:57 +08:00
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.
This commit is contained in:

committed by
GitHub

parent
20413ef7d7
commit
37f85bf8cc
@ -431,6 +431,11 @@ class AwsLambdaInstrumentor(BaseInstrumentor):
|
|||||||
the context is extracted from the HTTP headers of an API Gateway
|
the context is extracted from the HTTP headers of an API Gateway
|
||||||
request.
|
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))
|
lambda_handler = os.environ.get(ORIG_HANDLER, os.environ.get(_HANDLER))
|
||||||
if not lambda_handler:
|
if not lambda_handler:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from importlib import import_module, reload
|
from importlib import import_module, reload
|
||||||
@ -124,7 +126,10 @@ class TestAwsLambdaInstrumentorBase(TestBase):
|
|||||||
super().setUp()
|
super().setUp()
|
||||||
self.common_env_patch = mock.patch.dict(
|
self.common_env_patch = mock.patch.dict(
|
||||||
"os.environ",
|
"os.environ",
|
||||||
{_HANDLER: "tests.mocks.lambda_function.handler"},
|
{
|
||||||
|
_HANDLER: "tests.mocks.lambda_function.handler",
|
||||||
|
"AWS_LAMBDA_FUNCTION_NAME": "mylambda",
|
||||||
|
},
|
||||||
)
|
)
|
||||||
self.common_env_patch.start()
|
self.common_env_patch.start()
|
||||||
|
|
||||||
@ -466,15 +471,40 @@ class TestAwsLambdaInstrumentor(TestAwsLambdaInstrumentorBase):
|
|||||||
|
|
||||||
exc_env_patch.stop()
|
exc_env_patch.stop()
|
||||||
|
|
||||||
def test_lambda_handles_should_do_nothing_when_environment_variables_not_present(
|
@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: "tests.mocks.lambda_function.handler"},
|
||||||
|
clear=True,
|
||||||
|
)
|
||||||
|
exc_env_patch.start()
|
||||||
|
AwsLambdaInstrumentor().instrument()
|
||||||
|
|
||||||
|
spans = self.memory_exporter.get_finished_spans()
|
||||||
|
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,
|
self,
|
||||||
):
|
):
|
||||||
exc_env_patch = mock.patch.dict(
|
exc_env_patch = mock.patch.dict(
|
||||||
"os.environ",
|
"os.environ",
|
||||||
{_HANDLER: ""},
|
{"AWS_LAMBDA_FUNCTION_NAME": "mylambda"},
|
||||||
|
clear=True,
|
||||||
)
|
)
|
||||||
exc_env_patch.start()
|
exc_env_patch.start()
|
||||||
|
with self.assertLogs(level=logging.WARNING) as warning:
|
||||||
AwsLambdaInstrumentor().instrument()
|
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()
|
spans = self.memory_exporter.get_finished_spans()
|
||||||
self.assertEqual(len(spans), 0)
|
self.assertEqual(len(spans), 0)
|
||||||
|
Reference in New Issue
Block a user