Skip to content

patch upstream ec2 and eks resource detectors #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
# SPDX-License-Identifier: Apache-2.0
# Modifications Copyright The OpenTelemetry Authors. Licensed under the Apache License 2.0 License.
import importlib
import ssl
from urllib.request import Request, urlopen

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

_apply_resource_detector_patches()


def _apply_botocore_instrumentation_patches() -> None:
"""Botocore instrumentation patches
Expand All @@ -31,6 +37,38 @@ def _apply_botocore_instrumentation_patches() -> None:
_apply_botocore_sqs_patch()


# The OpenTelemetry Authors code
def _apply_resource_detector_patches() -> None:
"""AWS Resource Detector patches for getting the following unreleased change (as of v2.0.1) in the upstream:
https://github.com/open-telemetry/opentelemetry-python-contrib/commit/a5ec3f7f55494cb80b4b53c652e31c465b8d5e80
"""

def patch_ec2_aws_http_request(method, path, headers):
with urlopen(
Request("http://169.254.169.254" + path, headers=headers, method=method),
timeout=5,
) as response:
return response.read().decode("utf-8")

def patch_eks_aws_http_request(method, path, cred_value):
with urlopen(
Request(
"https://kubernetes.default.svc" + path,
headers={"Authorization": cred_value},
method=method,
),
timeout=5,
context=ssl.create_default_context(cafile="/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"),
) as response:
return response.read().decode("utf-8")

ec2_resource._aws_http_request = patch_ec2_aws_http_request
eks_resource._aws_http_request = patch_eks_aws_http_request


# END The OpenTelemetry Authors code


def _apply_botocore_kinesis_patch() -> None:
"""Botocore instrumentation patch for Kinesis

Expand Down