Skip to content

Commit b5f8973

Browse files
committedNov 7, 2020
licenses and refactor span wrapper
1 parent 369dba4 commit b5f8973

File tree

4 files changed

+69
-25
lines changed

4 files changed

+69
-25
lines changed
 

‎instrumentation/opentelemetry-instrumentation-sklearn/src/opentelemetry/instrumentation/sklearn/__init__.py

+27-25
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
logger = logging.getLogger(__name__)
8181

8282

83-
def implement_spans(
83+
def implement_span_estimator(
8484
func: Callable,
8585
estimator: Union[BaseEstimator, Type[BaseEstimator]],
8686
attributes: Attributes = None,
@@ -100,13 +100,27 @@ def implement_spans(
100100
else:
101101
name = estimator.__class__.__name__
102102
logger.debug("Instrumenting: %s.%s", name, func.__name__)
103-
104103
attributes = attributes or {}
104+
name = "{cls}.{func}".format(cls=name, func=func.__name__)
105+
return implement_span_function(func, name, attributes)
106+
107+
108+
def implement_span_function(func: Callable, name: str, attributes: Attributes):
109+
"""Wrap the function with a span.
110+
111+
Args:
112+
func: A callable to be wrapped in a span
113+
name: The name of the span
114+
attributes: Attributes to apply to the span
115+
116+
Returns:
117+
The passed function wrapped in a span.
118+
"""
105119

106120
@wraps(func)
107121
def wrapper(*args, **kwargs):
108122
with get_tracer(__name__, __version__).start_as_current_span(
109-
name="{cls}.{func}".format(cls=name, func=func.__name__),
123+
name=name
110124
) as span:
111125
if span.is_recording():
112126
for key, val in attributes.items():
@@ -116,7 +130,7 @@ def wrapper(*args, **kwargs):
116130
return wrapper
117131

118132

119-
def implement_spans_delegator(
133+
def implement_span_delegator(
120134
obj: _IffHasAttrDescriptor, attributes: Attributes = None
121135
):
122136
"""Wrap the descriptor's fn with a span.
@@ -129,26 +143,14 @@ def implement_spans_delegator(
129143
if hasattr(obj, "_otel_original_fn"):
130144
logger.debug("Already instrumented: %s", obj.fn.__qualname__)
131145
return
132-
133-
attributes = attributes or {}
134-
135-
def implement_spans_fn(func: Callable):
136-
@wraps(func)
137-
def wrapper(*args, **kwargs):
138-
with get_tracer(__name__, __version__).start_as_current_span(
139-
name=func.__qualname__
140-
) as span:
141-
if span.is_recording():
142-
for key, val in attributes.items():
143-
span.set_attribute(key, val)
144-
return func(*args, **kwargs)
145-
146-
return wrapper
147-
148146
logger.debug("Instrumenting: %s", obj.fn.__qualname__)
149-
147+
attributes = attributes or {}
150148
setattr(obj, "_otel_original_fn", getattr(obj, "fn"))
151-
setattr(obj, "fn", implement_spans_fn(obj.fn))
149+
setattr(
150+
obj,
151+
"fn",
152+
implement_span_function(obj.fn, obj.fn.__qualname__, attributes),
153+
)
152154

153155

154156
def get_delegator(
@@ -595,7 +597,7 @@ def _instrument_class_method(
595597
method_name,
596598
)
597599
elif delegator is not None:
598-
implement_spans_delegator(delegator)
600+
implement_span_delegator(delegator)
599601
else:
600602
setattr(
601603
estimator,
@@ -605,7 +607,7 @@ def _instrument_class_method(
605607
setattr(
606608
estimator,
607609
method_name,
608-
implement_spans(class_attr, estimator, attributes),
610+
implement_span_estimator(class_attr, estimator, attributes),
609611
)
610612

611613
def _unwrap_function(self, function):
@@ -655,7 +657,7 @@ def _instrument_instance_method(
655657
setattr(
656658
estimator,
657659
method_name,
658-
implement_spans(method, estimator, attributes),
660+
implement_span_estimator(method, estimator, attributes),
659661
)
660662

661663
def _instrument_estimator_attribute(
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1+
# Copyright 2020, OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
__version__ = "0.16.dev0"

‎instrumentation/opentelemetry-instrumentation-sklearn/tests/fixtures.py

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright 2020, OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
import numpy as np
216
from sklearn.datasets import load_iris
317
from sklearn.decomposition import PCA, TruncatedSVD

‎instrumentation/opentelemetry-instrumentation-sklearn/tests/test_sklearn.py

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright 2020, OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
from sklearn.ensemble import RandomForestClassifier
216

317
from opentelemetry.instrumentation.sklearn import (

0 commit comments

Comments
 (0)
Please sign in to comment.