80
80
logger = logging .getLogger (__name__ )
81
81
82
82
83
- def implement_spans (
83
+ def implement_span_estimator (
84
84
func : Callable ,
85
85
estimator : Union [BaseEstimator , Type [BaseEstimator ]],
86
86
attributes : Attributes = None ,
@@ -100,13 +100,27 @@ def implement_spans(
100
100
else :
101
101
name = estimator .__class__ .__name__
102
102
logger .debug ("Instrumenting: %s.%s" , name , func .__name__ )
103
-
104
103
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
+ """
105
119
106
120
@wraps (func )
107
121
def wrapper (* args , ** kwargs ):
108
122
with get_tracer (__name__ , __version__ ).start_as_current_span (
109
- name = "{cls}.{func}" . format ( cls = name , func = func . __name__ ),
123
+ name = name
110
124
) as span :
111
125
if span .is_recording ():
112
126
for key , val in attributes .items ():
@@ -116,7 +130,7 @@ def wrapper(*args, **kwargs):
116
130
return wrapper
117
131
118
132
119
- def implement_spans_delegator (
133
+ def implement_span_delegator (
120
134
obj : _IffHasAttrDescriptor , attributes : Attributes = None
121
135
):
122
136
"""Wrap the descriptor's fn with a span.
@@ -129,26 +143,14 @@ def implement_spans_delegator(
129
143
if hasattr (obj , "_otel_original_fn" ):
130
144
logger .debug ("Already instrumented: %s" , obj .fn .__qualname__ )
131
145
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
-
148
146
logger .debug ("Instrumenting: %s" , obj .fn .__qualname__ )
149
-
147
+ attributes = attributes or {}
150
148
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
+ )
152
154
153
155
154
156
def get_delegator (
@@ -595,7 +597,7 @@ def _instrument_class_method(
595
597
method_name ,
596
598
)
597
599
elif delegator is not None :
598
- implement_spans_delegator (delegator )
600
+ implement_span_delegator (delegator )
599
601
else :
600
602
setattr (
601
603
estimator ,
@@ -605,7 +607,7 @@ def _instrument_class_method(
605
607
setattr (
606
608
estimator ,
607
609
method_name ,
608
- implement_spans (class_attr , estimator , attributes ),
610
+ implement_span_estimator (class_attr , estimator , attributes ),
609
611
)
610
612
611
613
def _unwrap_function (self , function ):
@@ -655,7 +657,7 @@ def _instrument_instance_method(
655
657
setattr (
656
658
estimator ,
657
659
method_name ,
658
- implement_spans (method , estimator , attributes ),
660
+ implement_span_estimator (method , estimator , attributes ),
659
661
)
660
662
661
663
def _instrument_estimator_attribute (
0 commit comments