Skip to content

Commit f561efe

Browse files
committed
PYTHON-4574 - FaaS detection logic mistakenly identifies EKS as AWS Lambda
1 parent 2895e84 commit f561efe

File tree

3 files changed

+55
-29
lines changed

3 files changed

+55
-29
lines changed

pymongo/pool_options.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -179,37 +179,39 @@ def _getenv_int(key: str) -> Optional[int]:
179179
def _metadata_env() -> dict[str, Any]:
180180
env: dict[str, Any] = {}
181181
container = get_container_env_info()
182+
# Don't populate FaaS metadata if a container is present.
182183
if container:
183184
env["container"] = container
184-
# Skip if multiple (or no) envs are matched.
185-
if (_is_lambda(), _is_azure_func(), _is_gcp_func(), _is_vercel()).count(True) != 1:
186-
return env
187-
if _is_lambda():
188-
env["name"] = "aws.lambda"
189-
region = os.getenv("AWS_REGION")
190-
if region:
191-
env["region"] = region
192-
memory_mb = _getenv_int("AWS_LAMBDA_FUNCTION_MEMORY_SIZE")
193-
if memory_mb is not None:
194-
env["memory_mb"] = memory_mb
195-
elif _is_azure_func():
196-
env["name"] = "azure.func"
197-
elif _is_gcp_func():
198-
env["name"] = "gcp.func"
199-
region = os.getenv("FUNCTION_REGION")
200-
if region:
201-
env["region"] = region
202-
memory_mb = _getenv_int("FUNCTION_MEMORY_MB")
203-
if memory_mb is not None:
204-
env["memory_mb"] = memory_mb
205-
timeout_sec = _getenv_int("FUNCTION_TIMEOUT_SEC")
206-
if timeout_sec is not None:
207-
env["timeout_sec"] = timeout_sec
208-
elif _is_vercel():
209-
env["name"] = "vercel"
210-
region = os.getenv("VERCEL_REGION")
211-
if region:
212-
env["region"] = region
185+
else:
186+
# Skip if multiple (or no) envs are matched.
187+
if (_is_lambda(), _is_azure_func(), _is_gcp_func(), _is_vercel()).count(True) != 1:
188+
return env
189+
if _is_lambda():
190+
env["name"] = "aws.lambda"
191+
region = os.getenv("AWS_REGION")
192+
if region:
193+
env["region"] = region
194+
memory_mb = _getenv_int("AWS_LAMBDA_FUNCTION_MEMORY_SIZE")
195+
if memory_mb is not None:
196+
env["memory_mb"] = memory_mb
197+
elif _is_azure_func():
198+
env["name"] = "azure.func"
199+
elif _is_gcp_func():
200+
env["name"] = "gcp.func"
201+
region = os.getenv("FUNCTION_REGION")
202+
if region:
203+
env["region"] = region
204+
memory_mb = _getenv_int("FUNCTION_MEMORY_MB")
205+
if memory_mb is not None:
206+
env["memory_mb"] = memory_mb
207+
timeout_sec = _getenv_int("FUNCTION_TIMEOUT_SEC")
208+
if timeout_sec is not None:
209+
env["timeout_sec"] = timeout_sec
210+
elif _is_vercel():
211+
env["name"] = "vercel"
212+
region = os.getenv("VERCEL_REGION")
213+
if region:
214+
env["region"] = region
213215
return env
214216

215217

test/asynchronous/test_client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,18 @@ async def test_handshake_08_invalid_aws_ec2(self):
20192019
None,
20202020
)
20212021

2022+
async def test_handshake_09_container_with_provider(self):
2023+
# No FaaS metadata should be present.
2024+
await self._test_handshake(
2025+
{
2026+
ENV_VAR_K8S: "1",
2027+
"AWS_LAMBDA_RUNTIME_API": "1",
2028+
"AWS_REGION": "us-east-1",
2029+
"AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "256",
2030+
},
2031+
{"container": {"orchestrator": "kubernetes"}},
2032+
)
2033+
20222034
def test_dict_hints(self):
20232035
self.db.t.find(hint={"x": 1})
20242036

test/test_client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,6 +1977,18 @@ def test_handshake_08_invalid_aws_ec2(self):
19771977
None,
19781978
)
19791979

1980+
def test_handshake_09_container_with_provider(self):
1981+
# No FaaS metadata should be present.
1982+
self._test_handshake(
1983+
{
1984+
ENV_VAR_K8S: "1",
1985+
"AWS_LAMBDA_RUNTIME_API": "1",
1986+
"AWS_REGION": "us-east-1",
1987+
"AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "256",
1988+
},
1989+
{"container": {"orchestrator": "kubernetes"}},
1990+
)
1991+
19801992
def test_dict_hints(self):
19811993
self.db.t.find(hint={"x": 1})
19821994

0 commit comments

Comments
 (0)