-
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 s3 instrumentation and project refactor
Signed-off-by: Cagri Yonca <[email protected]>
- Loading branch information
1 parent
712bc3d
commit e69acaf
Showing
5 changed files
with
177 additions
and
292 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# (c) Copyright IBM Corp. 2021 | ||
# (c) Copyright Instana Inc. 2020 | ||
|
||
from typing import TYPE_CHECKING, Any, Callable, Dict, Sequence, Type | ||
|
||
from instana.span_context import SpanContext | ||
|
||
if TYPE_CHECKING: | ||
from botocore.client import BaseClient | ||
|
||
try: | ||
import wrapt | ||
|
||
from instana.log import logger | ||
from instana.singletons import tracer | ||
from instana.util.traceutils import ( | ||
get_tracer_tuple, | ||
tracing_is_off, | ||
) | ||
|
||
operations = { | ||
"upload_file": "UploadFile", | ||
"upload_fileobj": "UploadFileObj", | ||
"download_file": "DownloadFile", | ||
"download_fileobj": "DownloadFileObj", | ||
} | ||
|
||
def collect_s3_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("s3", span_context=parent_context) as span: | ||
try: | ||
span.set_attribute("s3.op", args[0]) | ||
if "Bucket" in args[1].keys(): | ||
span.set_attribute("s3.bucket", args[1]["Bucket"]) | ||
except Exception as exc: | ||
span.record_exception(exc) | ||
logger.debug( | ||
"collect_dynamodb_attributes: collect error", exc_info=True | ||
) | ||
|
||
def collect_s3_injected_attributes( | ||
wrapped: Callable[..., object], | ||
instance: Type["BaseClient"], | ||
args: Sequence[object], | ||
kwargs: Dict[str, Any], | ||
) -> Callable[..., object]: | ||
# 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 | ||
|
||
with tracer.start_as_current_span("s3", span_context=parent_context) as span: | ||
try: | ||
span.set_attribute("s3.op", operations[wrapped.__name__]) | ||
if wrapped.__name__ in ["download_file", "download_fileobj"]: | ||
span.set_attribute("s3.bucket", args[0]) | ||
else: | ||
span.set_attribute("s3.bucket", args[1]) | ||
return wrapped(*args, **kwargs) | ||
except Exception as exc: | ||
span.record_exception(exc) | ||
logger.debug( | ||
"s3_inject_method_with_instana: collect error", exc_info=True | ||
) | ||
|
||
for method in [ | ||
"upload_file", | ||
"upload_fileobj", | ||
"download_file", | ||
"download_fileobj", | ||
]: | ||
wrapt.wrap_function_wrapper( | ||
"boto3.s3.inject", method, collect_s3_injected_attributes | ||
) | ||
|
||
logger.debug("Instrumenting s3") | ||
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 |
---|---|---|
|
@@ -50,6 +50,7 @@ | |
"redis", | ||
"rpc-client", | ||
"sqlalchemy", | ||
"s3", | ||
"tornado-client", | ||
"urllib3", | ||
"pymongo", | ||
|
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.