Skip to content

Commit 2ddee07

Browse files
authored
patch upstream ec2 and eks resource detectors (#91)
**Issue:** The upstream resource detector package is quite outdated since the last release of v2.0.1. And there is no plan for a new release for now. https://github.com/open-telemetry/opentelemetry-python-contrib/commits/main/sdk-extension/opentelemetry-sdk-extension-aws In our distro we want to consume this particular commit where the timeout for EC2 and EKS resource detectors were significantly shortened to 5 seconds. The current timeouts are quite long (1000s and 2000s respectively) and may cause application fail to start in certain cases. **Description of changes:** Using the existing mechanism of temporarily patching the upstream functions, I'm adding patches for the [`_aws_http_request`](https://github.com/open-telemetry/opentelemetry-python-contrib/blob/9e57fbad8e6bbc730c43b227d91bf24ed0876835/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/resource/ec2.py#L32) methods in the upstream. **Testing:** I couldn't come up with a way of writing unit tests to see if the patching works since the only change in the patched method is the timeout. Mocking the `urlopen()` to return a custom response from the patch didn't work as the mock would return the same for the original method too. I did test the change with a sample app where I added a log statement "Hello from the patch_ec2_aws_http_request" to the patch to ensure it was being applied. ``` (venv) ➜ adotDjangoTest opentelemetry-instrument --traces_exporter console --metrics_exporter none --service_name my_service python manage.py runserver --noreload 8000 Configuration of configurator not loaded because aws_configurator is set by OTEL_PYTHON_CONFIGURATOR Hello from the patch_ec2_aws_http_request AwsEcsResourceDetector failed: Missing ECS_CONTAINER_METADATA_URI therefore process is not on ECS. Failed to get k8s token: [Errno 2] No such file or directory: '/var/run/secrets/kubernetes.io/serviceaccount/token' AwsEksResourceDetector failed: [Errno 2] No such file or directory: '/var/run/secrets/kubernetes.io/serviceaccount/token' AwsEc2ResourceDetector failed: <urlopen error [Errno 65] No route to host> Performing system checks... System check identified no issues (0 silenced). You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them. March 01, 2024 - 17:20:36 Django version 5.0.2, using settings 'adotDjangoTest.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. ``` By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 8413501 commit 2ddee07

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/_instrumentation_patch.py

+38
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
# SPDX-License-Identifier: Apache-2.0
33
# Modifications Copyright The OpenTelemetry Authors. Licensed under the Apache License 2.0 License.
44
import importlib
5+
import ssl
6+
from urllib.request import Request, urlopen
57

8+
import opentelemetry.sdk.extension.aws.resource.ec2 as ec2_resource
9+
import opentelemetry.sdk.extension.aws.resource.eks as eks_resource
610
from opentelemetry.instrumentation.botocore.extensions import _KNOWN_EXTENSIONS
711
from opentelemetry.instrumentation.botocore.extensions.sqs import _SqsExtension
812
from opentelemetry.instrumentation.botocore.extensions.types import _AttributeMapT, _AwsSdkExtension
@@ -20,6 +24,8 @@ def apply_instrumentation_patches() -> None:
2024
"""
2125
_apply_botocore_instrumentation_patches()
2226

27+
_apply_resource_detector_patches()
28+
2329

2430
def _apply_botocore_instrumentation_patches() -> None:
2531
"""Botocore instrumentation patches
@@ -31,6 +37,38 @@ def _apply_botocore_instrumentation_patches() -> None:
3137
_apply_botocore_sqs_patch()
3238

3339

40+
# The OpenTelemetry Authors code
41+
def _apply_resource_detector_patches() -> None:
42+
"""AWS Resource Detector patches for getting the following unreleased change (as of v2.0.1) in the upstream:
43+
https://github.com/open-telemetry/opentelemetry-python-contrib/commit/a5ec3f7f55494cb80b4b53c652e31c465b8d5e80
44+
"""
45+
46+
def patch_ec2_aws_http_request(method, path, headers):
47+
with urlopen(
48+
Request("http://169.254.169.254" + path, headers=headers, method=method),
49+
timeout=5,
50+
) as response:
51+
return response.read().decode("utf-8")
52+
53+
def patch_eks_aws_http_request(method, path, cred_value):
54+
with urlopen(
55+
Request(
56+
"https://kubernetes.default.svc" + path,
57+
headers={"Authorization": cred_value},
58+
method=method,
59+
),
60+
timeout=5,
61+
context=ssl.create_default_context(cafile="/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"),
62+
) as response:
63+
return response.read().decode("utf-8")
64+
65+
ec2_resource._aws_http_request = patch_ec2_aws_http_request
66+
eks_resource._aws_http_request = patch_eks_aws_http_request
67+
68+
69+
# END The OpenTelemetry Authors code
70+
71+
3472
def _apply_botocore_kinesis_patch() -> None:
3573
"""Botocore instrumentation patch for Kinesis
3674

0 commit comments

Comments
 (0)