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

Move duplicated code to a dependency #942

Merged
merged 4 commits into from
Jul 28, 2020
Merged
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
1 change: 1 addition & 0 deletions eachdist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ sortfirst=
ext/opentelemetry-ext-wsgi
ext/opentelemetry-ext-dbapi
ext/opentelemetry-ext-asgi
ext/opentelemetry-ext-botocore
ext/*

[lintroots]
Expand Down
1 change: 1 addition & 0 deletions ext/opentelemetry-ext-boto/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ install_requires =
boto ~= 2.0
opentelemetry-api == 0.11.dev0
opentelemetry-instrumentation == 0.11.dev0
opentelemetry-ext-botocore == 0.11.dev0

[options.extras_require]
test =
Expand Down
55 changes: 2 additions & 53 deletions ext/opentelemetry-ext-boto/src/opentelemetry/ext/boto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@
from inspect import currentframe

from boto.connection import AWSAuthConnection, AWSQueryConnection
from wrapt import ObjectProxy, wrap_function_wrapper
from wrapt import wrap_function_wrapper

from opentelemetry.ext.boto.version import __version__
from opentelemetry.ext.botocore import add_span_arg_tags, unwrap
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.sdk.trace import Resource
from opentelemetry.trace import SpanKind, get_tracer
Expand Down Expand Up @@ -197,55 +198,3 @@ def _patched_auth_request(self, original_func, instance, args, kwargs):
args,
kwargs,
)


def truncate_arg_value(value, max_len=1024):
"""Truncate values which are bytes and greater than ``max_len``.
Useful for parameters like "Body" in ``put_object`` operations.
"""
if isinstance(value, bytes) and len(value) > max_len:
return b"..."

return value


def add_span_arg_tags(span, endpoint_name, args, args_names, args_traced):
if endpoint_name not in ["kms", "sts"]:
tags = dict(
(name, value)
for (name, value) in zip(args_names, args)
if name in args_traced
)
tags = flatten_dict(tags)
for key, value in {
k: truncate_arg_value(v)
for k, v in tags.items()
if k not in {"s3": ["params.Body"]}.get(endpoint_name, [])
}.items():
span.set_attribute(key, value)


def flatten_dict(dict_, sep=".", prefix=""):
"""
Returns a normalized dict of depth 1 with keys in order of embedding
"""
# adapted from https://stackoverflow.com/a/19647596
return (
{
prefix + sep + k if prefix else k: v
for kk, vv in dict_.items()
for k, v in flatten_dict(vv, sep, kk).items()
}
if isinstance(dict_, dict)
else {prefix: dict_}
)


def unwrap(obj, attr):
function = getattr(obj, attr, None)
if (
function
and isinstance(function, ObjectProxy)
and hasattr(function, "__wrapped__")
):
setattr(obj, attr, function.__wrapped__)
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ def truncate_arg_value(value, max_len=1024):

return value

def flatten_dict(dict_, sep=".", prefix=""):
"""
Returns a normalized dict of depth 1 with keys in order of embedding
"""
# adapted from https://stackoverflow.com/a/19647596
return (
{
prefix + sep + k if prefix else k: v
for kk, vv in dict_.items()
for k, v in flatten_dict(vv, sep, kk).items()
}
if isinstance(dict_, dict)
else {prefix: dict_}
)

if endpoint_name not in {"kms", "sts"}:
tags = dict(
(name, value)
Expand All @@ -175,22 +190,6 @@ def truncate_arg_value(value, max_len=1024):
span.set_attribute(key, value)


def flatten_dict(dict_, sep=".", prefix=""):
"""
Returns a normalized dict of depth 1 with keys in order of embedding
"""
# adapted from https://stackoverflow.com/a/19647596
return (
{
prefix + sep + k if prefix else k: v
for kk, vv in dict_.items()
for k, v in flatten_dict(vv, sep, kk).items()
}
if isinstance(dict_, dict)
else {prefix: dict_}
)


def deep_getattr(obj, attr_string, default=None):
"""
Returns the attribute of ``obj`` at the dotted path given by
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,11 @@ commands_pre =

asyncpg: pip install {toxinidir}/ext/opentelemetry-ext-asyncpg

boto: pip install {toxinidir}/ext/opentelemetry-ext-botocore[test]
boto: pip install {toxinidir}/ext/opentelemetry-ext-boto[test]

flask: pip install {toxinidir}/ext/opentelemetry-ext-flask[test]

botocore: pip install {toxinidir}/opentelemetry-instrumentation
botocore: pip install {toxinidir}/ext/opentelemetry-ext-botocore[test]

dbapi: pip install {toxinidir}/ext/opentelemetry-ext-dbapi[test]
Expand Down