From c28e9f46712c5affe94a88ee33e5a25d6be9d981 Mon Sep 17 00:00:00 2001 From: flynn Date: Sat, 7 Nov 2020 13:25:41 -0500 Subject: [PATCH] licenses and refactor span wrapper --- .../instrumentation/sklearn/__init__.py | 51 ++++++++++--------- .../instrumentation/sklearn/version.py | 14 +++++ .../tests/fixtures.py | 14 +++++ .../tests/test_sklearn.py | 14 +++++ 4 files changed, 68 insertions(+), 25 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-sklearn/src/opentelemetry/instrumentation/sklearn/__init__.py b/instrumentation/opentelemetry-instrumentation-sklearn/src/opentelemetry/instrumentation/sklearn/__init__.py index d7ac330d1e..750c5f1f02 100644 --- a/instrumentation/opentelemetry-instrumentation-sklearn/src/opentelemetry/instrumentation/sklearn/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-sklearn/src/opentelemetry/instrumentation/sklearn/__init__.py @@ -80,7 +80,7 @@ logger = logging.getLogger(__name__) -def implement_spans( +def implement_span_estimator( func: Callable, estimator: Union[BaseEstimator, Type[BaseEstimator]], attributes: Attributes = None, @@ -100,13 +100,26 @@ def implement_spans( else: name = estimator.__class__.__name__ logger.debug("Instrumenting: %s.%s", name, func.__name__) - attributes = attributes or {} + name = "{cls}.{func}".format(cls=name, func=func.__name__) + return implement_span_function(func, name, attributes) + + +def implement_span_function(func: Callable, name: str, attributes: Attributes): + """Wrap the function with a span. + Args: + func: A callable to be wrapped in a span + name: The name of the span + attributes: Attributes to apply to the span + + Returns: + The passed function wrapped in a span. + """ @wraps(func) def wrapper(*args, **kwargs): with get_tracer(__name__, __version__).start_as_current_span( - name="{cls}.{func}".format(cls=name, func=func.__name__), + name=name ) as span: if span.is_recording(): for key, val in attributes.items(): @@ -116,7 +129,7 @@ def wrapper(*args, **kwargs): return wrapper -def implement_spans_delegator( +def implement_span_delegator( obj: _IffHasAttrDescriptor, attributes: Attributes = None ): """Wrap the descriptor's fn with a span. @@ -129,26 +142,14 @@ def implement_spans_delegator( if hasattr(obj, "_otel_original_fn"): logger.debug("Already instrumented: %s", obj.fn.__qualname__) return - - attributes = attributes or {} - - def implement_spans_fn(func: Callable): - @wraps(func) - def wrapper(*args, **kwargs): - with get_tracer(__name__, __version__).start_as_current_span( - name=func.__qualname__ - ) as span: - if span.is_recording(): - for key, val in attributes.items(): - span.set_attribute(key, val) - return func(*args, **kwargs) - - return wrapper - logger.debug("Instrumenting: %s", obj.fn.__qualname__) - + attributes = attributes or {} setattr(obj, "_otel_original_fn", getattr(obj, "fn")) - setattr(obj, "fn", implement_spans_fn(obj.fn)) + setattr( + obj, + "fn", + implement_span_function(obj.fn, obj.fn.__qualname__, attributes) + ) def get_delegator( @@ -595,7 +596,7 @@ def _instrument_class_method( method_name, ) elif delegator is not None: - implement_spans_delegator(delegator) + implement_span_delegator(delegator) else: setattr( estimator, @@ -605,7 +606,7 @@ def _instrument_class_method( setattr( estimator, method_name, - implement_spans(class_attr, estimator, attributes), + implement_span_estimator(class_attr, estimator, attributes), ) def _unwrap_function(self, function): @@ -655,7 +656,7 @@ def _instrument_instance_method( setattr( estimator, method_name, - implement_spans(method, estimator, attributes), + implement_span_estimator(method, estimator, attributes), ) def _instrument_estimator_attribute( diff --git a/instrumentation/opentelemetry-instrumentation-sklearn/src/opentelemetry/instrumentation/sklearn/version.py b/instrumentation/opentelemetry-instrumentation-sklearn/src/opentelemetry/instrumentation/sklearn/version.py index 4c4951dcda..bb32120c79 100644 --- a/instrumentation/opentelemetry-instrumentation-sklearn/src/opentelemetry/instrumentation/sklearn/version.py +++ b/instrumentation/opentelemetry-instrumentation-sklearn/src/opentelemetry/instrumentation/sklearn/version.py @@ -1 +1,15 @@ +# Copyright 2020, OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + __version__ = "0.16.dev0" diff --git a/instrumentation/opentelemetry-instrumentation-sklearn/tests/fixtures.py b/instrumentation/opentelemetry-instrumentation-sklearn/tests/fixtures.py index 4f9b8aa47e..cf26c0fcf2 100644 --- a/instrumentation/opentelemetry-instrumentation-sklearn/tests/fixtures.py +++ b/instrumentation/opentelemetry-instrumentation-sklearn/tests/fixtures.py @@ -1,3 +1,17 @@ +# Copyright 2020, OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import numpy as np from sklearn.datasets import load_iris from sklearn.decomposition import PCA, TruncatedSVD diff --git a/instrumentation/opentelemetry-instrumentation-sklearn/tests/test_sklearn.py b/instrumentation/opentelemetry-instrumentation-sklearn/tests/test_sklearn.py index df454b6fa9..ad4d032280 100644 --- a/instrumentation/opentelemetry-instrumentation-sklearn/tests/test_sklearn.py +++ b/instrumentation/opentelemetry-instrumentation-sklearn/tests/test_sklearn.py @@ -1,3 +1,17 @@ +# Copyright 2020, OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from sklearn.ensemble import RandomForestClassifier from opentelemetry.instrumentation.sklearn import (