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 2 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,24 +2,30 @@
# 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
from opentelemetry.semconv.trace import SpanAttributes


def apply_instrumentation_patches() -> None:
"""Apply patches to upstream instrumentation libraries.
"""Apply patches to upstream libraries.

This method is invoked to apply changes to upstream instrumentation libraries, typically when changes to upstream
This method is invoked to apply changes to upstream libraries, typically when changes to upstream
are required on a timeline that cannot wait for upstream release. Generally speaking, patches should be short-term
local solutions that are comparable to long-term upstream solutions.

Where possible, automated testing should be run to catch upstream changes resulting in broken patches
"""
_apply_botocore_instrumentation_patches()

_apply_resource_detector_patches()


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


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


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

Expand Down