Skip to content
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

chore: move botocore utils to utils_botocore #10749

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions ddtrace/_trace/trace_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

from ddtrace._trace.span import Span
from ddtrace._trace.utils import extract_DD_context_from_messages
from ddtrace._trace.utils import set_botocore_patched_api_call_span_tags as set_patched_api_call_span_tags
from ddtrace._trace.utils import set_botocore_response_metadata_tags
from ddtrace._trace.utils_botocore.span_tags import (
set_botocore_patched_api_call_span_tags as set_patched_api_call_span_tags,
)
from ddtrace._trace.utils_botocore.span_tags import set_botocore_response_metadata_tags
from ddtrace.constants import _ANALYTICS_SAMPLE_RATE_KEY
from ddtrace.constants import SPAN_KIND
from ddtrace.constants import SPAN_MEASURED_KEY
Expand Down
67 changes: 0 additions & 67 deletions ddtrace/_trace/utils.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,8 @@
from typing import Any
from typing import Callable
from typing import Dict
from typing import Optional

from ddtrace import Span
from ddtrace import config
from ddtrace.constants import _ANALYTICS_SAMPLE_RATE_KEY
from ddtrace.constants import SPAN_KIND
from ddtrace.constants import SPAN_MEASURED_KEY
from ddtrace.ext import SpanKind
from ddtrace.ext import aws
from ddtrace.ext import http
from ddtrace.internal.constants import COMPONENT
from ddtrace.internal.utils.formats import deep_getattr
from ddtrace.propagation.http import HTTPPropagator


def set_botocore_patched_api_call_span_tags(span: Span, instance, args, params, endpoint_name, operation):
span.set_tag_str(COMPONENT, config.botocore.integration_name)
# set span.kind to the type of request being performed
span.set_tag_str(SPAN_KIND, SpanKind.CLIENT)
span.set_tag(SPAN_MEASURED_KEY)

if args:
# DEV: join is the fastest way of concatenating strings that is compatible
# across Python versions (see
# https://stackoverflow.com/questions/1316887/what-is-the-most-efficient-string-concatenation-method-in-python)
span.resource = ".".join((endpoint_name, operation.lower()))
span.set_tag("aws_service", endpoint_name)

if params and not config.botocore["tag_no_params"]:
aws._add_api_param_span_tags(span, endpoint_name, params)

else:
span.resource = endpoint_name

region_name = deep_getattr(instance, "meta.region_name")

span.set_tag_str("aws.agent", "botocore")
if operation is not None:
span.set_tag_str("aws.operation", operation)
if region_name is not None:
span.set_tag_str("aws.region", region_name)
span.set_tag_str("region", region_name)

# set analytics sample rate
span.set_tag(_ANALYTICS_SAMPLE_RATE_KEY, config.botocore.get_analytics_sample_rate())


def set_botocore_response_metadata_tags(
span: Span, result: Dict[str, Any], is_error_code_fn: Optional[Callable] = None
) -> None:
if not result or not result.get("ResponseMetadata"):
return
response_meta = result["ResponseMetadata"]

if "HTTPStatusCode" in response_meta:
status_code = response_meta["HTTPStatusCode"]
span.set_tag(http.STATUS_CODE, status_code)

# Mark this span as an error if requested
if is_error_code_fn is not None and is_error_code_fn(int(status_code)):
span.error = 1

if "RetryAttempts" in response_meta:
span.set_tag("retry_attempts", response_meta["RetryAttempts"])

if "RequestId" in response_meta:
span.set_tag_str("aws.requestid", response_meta["RequestId"])


def extract_DD_context_from_messages(messages, extract_from_message: Callable):
ctx = None
if len(messages) >= 1:
Expand Down
69 changes: 69 additions & 0 deletions ddtrace/_trace/utils_botocore/span_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from typing import Any
from typing import Callable
from typing import Dict
from typing import Optional

from ddtrace import Span
from ddtrace import config
from ddtrace.constants import _ANALYTICS_SAMPLE_RATE_KEY
from ddtrace.constants import SPAN_KIND
from ddtrace.constants import SPAN_MEASURED_KEY
from ddtrace.ext import SpanKind
from ddtrace.ext import aws
from ddtrace.ext import http
from ddtrace.internal.constants import COMPONENT
from ddtrace.internal.utils.formats import deep_getattr


def set_botocore_patched_api_call_span_tags(span: Span, instance, args, params, endpoint_name, operation):
span.set_tag_str(COMPONENT, config.botocore.integration_name)
# set span.kind to the type of request being performed
span.set_tag_str(SPAN_KIND, SpanKind.CLIENT)
span.set_tag(SPAN_MEASURED_KEY)

if args:
# DEV: join is the fastest way of concatenating strings that is compatible
# across Python versions (see
# https://stackoverflow.com/questions/1316887/what-is-the-most-efficient-string-concatenation-method-in-python)
span.resource = ".".join((endpoint_name, operation.lower()))
span.set_tag("aws_service", endpoint_name)

if params and not config.botocore["tag_no_params"]:
aws._add_api_param_span_tags(span, endpoint_name, params)

else:
span.resource = endpoint_name

region_name = deep_getattr(instance, "meta.region_name")

span.set_tag_str("aws.agent", "botocore")
if operation is not None:
span.set_tag_str("aws.operation", operation)
if region_name is not None:
span.set_tag_str("aws.region", region_name)
span.set_tag_str("region", region_name)

# set analytics sample rate
span.set_tag(_ANALYTICS_SAMPLE_RATE_KEY, config.botocore.get_analytics_sample_rate())


def set_botocore_response_metadata_tags(
span: Span, result: Dict[str, Any], is_error_code_fn: Optional[Callable] = None
) -> None:
if not result or not result.get("ResponseMetadata"):
return
response_meta = result["ResponseMetadata"]

if "HTTPStatusCode" in response_meta:
status_code = response_meta["HTTPStatusCode"]
span.set_tag(http.STATUS_CODE, status_code)

# Mark this span as an error if requested
if is_error_code_fn is not None and is_error_code_fn(int(status_code)):
span.error = 1

if "RetryAttempts" in response_meta:
span.set_tag("retry_attempts", response_meta["RetryAttempts"])

if "RequestId" in response_meta:
span.set_tag_str("aws.requestid", response_meta["RequestId"])
Loading