-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ft: adding dynamodb instrumentation and project refactor
Signed-off-by: Cagri Yonca <[email protected]>
- Loading branch information
1 parent
6e678a6
commit 7e7b53c
Showing
18 changed files
with
780 additions
and
563 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# (c) Copyright IBM Corp. 2025 | ||
|
||
from typing import TYPE_CHECKING, Any, Callable, Dict, Sequence, Tuple, Type | ||
|
||
from instana.instrumentation.aws.dynamodb import collect_dynamodb_attributes | ||
from instana.instrumentation.aws.s3 import collect_s3_attributes | ||
|
||
if TYPE_CHECKING: | ||
from botocore.auth import SigV4Auth | ||
from botocore.client import BaseClient | ||
|
||
from instana.span.span import InstanaSpan | ||
|
||
try: | ||
import json | ||
|
||
import wrapt | ||
|
||
from instana.log import logger | ||
from instana.propagators.format import Format | ||
from instana.singletons import tracer | ||
from instana.span.span import get_current_span | ||
from instana.util.traceutils import ( | ||
extract_custom_headers, | ||
get_tracer_tuple, | ||
tracing_is_off, | ||
) | ||
|
||
def lambda_inject_context(payload: Dict[str, Any], span: "InstanaSpan") -> None: | ||
""" | ||
When boto3 lambda client 'Invoke' is called, we want to inject the tracing context. | ||
boto3/botocore has specific requirements: | ||
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/lambda.html#Lambda.Client.invoke | ||
""" | ||
try: | ||
invoke_payload = payload.get("Payload", {}) | ||
|
||
if not isinstance(invoke_payload, dict): | ||
invoke_payload = json.loads(invoke_payload) | ||
|
||
tracer.inject(span.context, Format.HTTP_HEADERS, invoke_payload) | ||
payload["Payload"] = json.dumps(invoke_payload) | ||
except Exception: | ||
logger.debug("non-fatal lambda_inject_context: ", exc_info=True) | ||
|
||
@wrapt.patch_function_wrapper("botocore.auth", "SigV4Auth.add_auth") | ||
def emit_add_auth_with_instana( | ||
wrapped: Callable[..., None], | ||
instance: "SigV4Auth", | ||
args: Tuple[object], | ||
kwargs: Dict[str, Any], | ||
) -> Callable[..., None]: | ||
current_span = get_current_span() | ||
if not tracing_is_off() and current_span and current_span.is_recording(): | ||
extract_custom_headers(current_span, args[0].headers) | ||
return wrapped(*args, **kwargs) | ||
|
||
@wrapt.patch_function_wrapper("botocore.client", "BaseClient._make_api_call") | ||
def make_api_call_with_instana( | ||
wrapped: Callable[..., Dict[str, Any]], | ||
instance: Type["BaseClient"], | ||
args: Sequence[Dict[str, Any]], | ||
kwargs: Dict[str, Any], | ||
) -> Dict[str, Any]: | ||
# If we're not tracing, just return | ||
if tracing_is_off(): | ||
return wrapped(*args, **kwargs) | ||
|
||
tracer, parent_span, _ = get_tracer_tuple() | ||
|
||
parent_context = parent_span.get_span_context() if parent_span else None | ||
|
||
try: | ||
if instance: | ||
if instance.meta.service_model.service_name == "dynamodb": | ||
collect_dynamodb_attributes( | ||
wrapped, instance, args, kwargs, parent_context | ||
) | ||
elif instance.meta.service_model.service_name == "s3": | ||
collect_s3_attributes( | ||
wrapped, instance, args, kwargs, parent_context | ||
) | ||
except Exception: | ||
logger.debug("make_api_call_with_instana: collect error", exc_info=True) | ||
else: | ||
return wrapped(*args, **kwargs) | ||
|
||
except ImportError: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# (c) Copyright IBM Corp. 2025 | ||
|
||
from typing import TYPE_CHECKING, Any, Callable, Dict, Sequence, Type | ||
|
||
if TYPE_CHECKING: | ||
from botocore.client import BaseClient | ||
|
||
from instana.log import logger | ||
from instana.singletons import tracer | ||
from instana.span_context import SpanContext | ||
|
||
try: | ||
|
||
def collect_dynamodb_attributes( | ||
wrapped: Callable[..., Dict[str, Any]], | ||
instance: Type["BaseClient"], | ||
args: Sequence[Dict[str, Any]], | ||
kwargs: Dict[str, Any], | ||
parent_context: SpanContext, | ||
) -> None: | ||
with tracer.start_as_current_span( | ||
"dynamodb", span_context=parent_context | ||
) as span: | ||
try: | ||
span.set_attribute("dynamodb.op", args[0]) | ||
span.set_attribute( | ||
"dynamodb.region", instance._client_config.region_name | ||
) | ||
if "TableName" in args[1].keys(): | ||
span.set_attribute("dynamodb.table", args[1]["TableName"]) | ||
except Exception as exc: | ||
span.record_exception(exc) | ||
logger.debug( | ||
"collect_dynamodb_attributes: collect error", exc_info=True | ||
) | ||
|
||
logger.debug("Instrumenting DynamoDB") | ||
|
||
except ImportError: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.