Skip to content

Commit

Permalink
licenses and refactor span wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
crflynn committed Nov 7, 2020
1 parent 369dba4 commit b5f8973
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -100,13 +100,27 @@ 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():
Expand All @@ -116,7 +130,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.
Expand All @@ -129,26 +143,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(
Expand Down Expand Up @@ -595,7 +597,7 @@ def _instrument_class_method(
method_name,
)
elif delegator is not None:
implement_spans_delegator(delegator)
implement_span_delegator(delegator)
else:
setattr(
estimator,
Expand All @@ -605,7 +607,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):
Expand Down Expand Up @@ -655,7 +657,7 @@ def _instrument_instance_method(
setattr(
estimator,
method_name,
implement_spans(method, estimator, attributes),
implement_span_estimator(method, estimator, attributes),
)

def _instrument_estimator_attribute(
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down

0 comments on commit b5f8973

Please sign in to comment.