6
6
import wrapt
7
7
8
8
import ddtrace
9
- from ddtrace .vendor .debtcollector import deprecate
10
9
11
10
from ..internal .logger import get_logger
12
11
@@ -32,25 +31,17 @@ class Pin(object):
32
31
>>> conn = sqlite.connect('/tmp/image.db')
33
32
"""
34
33
35
- __slots__ = ["tags" , "tracer " , "_target" , "_config" , "_initialized" ]
34
+ __slots__ = ["tags" , "_tracer " , "_target" , "_config" , "_initialized" ]
36
35
37
36
def __init__ (
38
37
self ,
39
38
service = None , # type: Optional[str]
40
39
tags = None , # type: Optional[Dict[str, str]]
41
- tracer = None ,
42
40
_config = None , # type: Optional[Dict[str, Any]]
43
41
):
44
42
# type: (...) -> None
45
- if tracer is not None and tracer is not ddtrace .tracer :
46
- deprecate (
47
- "Initializing ddtrace.trace.Pin with `tracer` argument is deprecated" ,
48
- message = "All Pin instances should use the global tracer instance" ,
49
- removal_version = "3.0.0" ,
50
- )
51
- tracer = tracer or ddtrace .tracer
52
43
self .tags = tags
53
- self .tracer = tracer
44
+ self ._tracer = ddtrace . tracer
54
45
self ._target = None # type: Optional[int]
55
46
# keep the configuration attribute internal because the
56
47
# public API to access it is not the Pin class
@@ -68,10 +59,14 @@ def service(self):
68
59
return self ._config ["service_name" ]
69
60
70
61
def __setattr__ (self , name , value ):
71
- if getattr (self , "_initialized" , False ) and name != "_target" :
62
+ if getattr (self , "_initialized" , False ) and name not in ( "_target" , "_tracer" ) :
72
63
raise AttributeError ("can't mutate a pin, use override() or clone() instead" )
73
64
super (Pin , self ).__setattr__ (name , value )
74
65
66
+ @property
67
+ def tracer (self ):
68
+ return self ._tracer
69
+
75
70
def __repr__ (self ):
76
71
return "Pin(service=%s, tags=%s, tracer=%s)" % (self .service , self .tags , self .tracer )
77
72
@@ -127,7 +122,6 @@ def override(
127
122
obj , # type: Any
128
123
service = None , # type: Optional[str]
129
124
tags = None , # type: Optional[Dict[str, str]]
130
- tracer = None ,
131
125
):
132
126
# type: (...) -> None
133
127
"""Override an object with the given attributes.
@@ -139,20 +133,32 @@ def override(
139
133
>>> # Override a pin for a specific connection
140
134
>>> Pin.override(conn, service='user-db')
141
135
"""
142
- if tracer is not None :
143
- deprecate (
144
- "Calling ddtrace.trace.Pin.override(...) with the `tracer` argument is deprecated" ,
145
- message = "All Pin instances should use the global tracer instance" ,
146
- removal_version = "3.0.0" ,
147
- )
136
+ Pin ._override (obj , service = service , tags = tags )
137
+
138
+ @classmethod
139
+ def _override (
140
+ cls ,
141
+ obj , # type: Any
142
+ service = None , # type: Optional[str]
143
+ tags = None , # type: Optional[Dict[str, str]]
144
+ tracer = None ,
145
+ ):
146
+ # type: (...) -> None
147
+ """
148
+ Internal method that allows overriding the global tracer in tests
149
+ """
148
150
if not obj :
149
151
return
150
152
151
153
pin = cls .get_from (obj )
152
154
if pin is None :
153
- Pin (service = service , tags = tags , tracer = tracer ). onto ( obj )
155
+ pin = Pin (service = service , tags = tags )
154
156
else :
155
- pin .clone (service = service , tags = tags , tracer = tracer ).onto (obj )
157
+ pin = pin .clone (service = service , tags = tags )
158
+
159
+ if tracer :
160
+ pin ._tracer = tracer
161
+ pin .onto (obj )
156
162
157
163
def enabled (self ):
158
164
# type: () -> bool
@@ -198,21 +204,22 @@ def clone(
198
204
self ,
199
205
service = None , # type: Optional[str]
200
206
tags = None , # type: Optional[Dict[str, str]]
201
- tracer = None ,
202
207
):
203
208
# type: (...) -> Pin
204
209
"""Return a clone of the pin with the given attributes replaced."""
210
+ return self ._clone (service = service , tags = tags )
211
+
212
+ def _clone (
213
+ self ,
214
+ service = None , # type: Optional[str]
215
+ tags = None , # type: Optional[Dict[str, str]]
216
+ tracer = None ,
217
+ ):
218
+ """Internal method that can clone the tracer from an existing Pin. This is used in tests"""
205
219
# do a shallow copy of Pin dicts
206
220
if not tags and self .tags :
207
221
tags = self .tags .copy ()
208
222
209
- if tracer is not None :
210
- deprecate (
211
- "Initializing ddtrace.trace.Pin with `tracer` argument is deprecated" ,
212
- message = "All Pin instances should use the global tracer instance" ,
213
- removal_version = "3.0.0" ,
214
- )
215
-
216
223
# we use a copy instead of a deepcopy because we expect configurations
217
224
# to have only a root level dictionary without nested objects. Using
218
225
# deepcopy introduces a big overhead:
@@ -221,9 +228,10 @@ def clone(
221
228
# deepcopy: 0.2787208557128906
222
229
config = self ._config .copy ()
223
230
224
- return Pin (
231
+ pin = Pin (
225
232
service = service or self .service ,
226
233
tags = tags ,
227
- tracer = tracer or self .tracer , # do not clone the Tracer
228
234
_config = config ,
229
235
)
236
+ pin ._tracer = tracer or self .tracer
237
+ return pin
0 commit comments