@@ -211,6 +211,10 @@ def begin_segment(self, name=None, traceid=None,
211
211
:param str traceid: trace id of the segment
212
212
:param int sampling: 0 means not sampled, 1 means sampled
213
213
"""
214
+ # Disable the recorder; return a generated dummy segment.
215
+ if not global_sdk_config .sdk_enabled ():
216
+ return DummySegment (global_sdk_config .DISABLED_ENTITY_NAME )
217
+
214
218
seg_name = name or self .service
215
219
if not seg_name :
216
220
raise SegmentNameMissingException ("Segment name is required." )
@@ -220,12 +224,6 @@ def begin_segment(self, name=None, traceid=None,
220
224
# depending on if centralized or local sampling rule takes effect.
221
225
decision = True
222
226
223
- # To disable the recorder, we set the sampling decision to always be false.
224
- # This way, when segments are generated, they become dummy segments and are ultimately never sent.
225
- # The call to self._sampler.should_trace() is never called either so the poller threads are never started.
226
- if not global_sdk_config .sdk_enabled ():
227
- sampling = 0
228
-
229
227
# we respect the input sampling decision
230
228
# regardless of recorder configuration.
231
229
if sampling == 0 :
@@ -251,8 +249,12 @@ def end_segment(self, end_time=None):
251
249
if it is ready to send. Ready means segment and
252
250
all its subsegments are closed.
253
251
254
- :param float end_time: segment compeletion in unix epoch in seconds.
252
+ :param float end_time: segment completion in unix epoch in seconds.
255
253
"""
254
+ # When the SDK is disabled we return
255
+ if not global_sdk_config .sdk_enabled ():
256
+ return
257
+
256
258
self .context .end_segment (end_time )
257
259
segment = self .current_segment ()
258
260
if segment and segment .ready_to_send ():
@@ -264,6 +266,7 @@ def current_segment(self):
264
266
this will make sure the segment returned is the one created by the
265
267
same thread.
266
268
"""
269
+
267
270
entity = self .get_trace_entity ()
268
271
if self ._is_subsegment (entity ):
269
272
return entity .parent_segment
@@ -280,6 +283,11 @@ def begin_subsegment(self, name, namespace='local'):
280
283
:param str name: the name of the subsegment.
281
284
:param str namespace: currently can only be 'local', 'remote', 'aws'.
282
285
"""
286
+ # Generating the parent dummy segment is necessary.
287
+ # We don't need to store anything in context. Assumption here
288
+ # is that we only work with recorder-level APIs.
289
+ if not global_sdk_config .sdk_enabled ():
290
+ return DummySubsegment (DummySegment (global_sdk_config .DISABLED_ENTITY_NAME ))
283
291
284
292
segment = self .current_segment ()
285
293
if not segment :
@@ -301,6 +309,9 @@ def current_subsegment(self):
301
309
this will make sure the subsegment returned is one created
302
310
by the same thread.
303
311
"""
312
+ if not global_sdk_config .sdk_enabled ():
313
+ return DummySubsegment (DummySegment (global_sdk_config .DISABLED_ENTITY_NAME ))
314
+
304
315
entity = self .get_trace_entity ()
305
316
if self ._is_subsegment (entity ):
306
317
return entity
@@ -314,6 +325,9 @@ def end_subsegment(self, end_time=None):
314
325
315
326
:param float end_time: subsegment compeletion in unix epoch in seconds.
316
327
"""
328
+ if not global_sdk_config .sdk_enabled ():
329
+ return
330
+
317
331
if not self .context .end_subsegment (end_time ):
318
332
return
319
333
@@ -333,6 +347,8 @@ def put_annotation(self, key, value):
333
347
:param object value: annotation value. Any type other than
334
348
string/number/bool will be dropped
335
349
"""
350
+ if not global_sdk_config .sdk_enabled ():
351
+ return
336
352
entity = self .get_trace_entity ()
337
353
if entity and entity .sampled :
338
354
entity .put_annotation (key , value )
@@ -348,6 +364,8 @@ def put_metadata(self, key, value, namespace='default'):
348
364
:param str key: metadata key under specified namespace
349
365
:param object value: any object that can be serialized into JSON string
350
366
"""
367
+ if not global_sdk_config .sdk_enabled ():
368
+ return
351
369
entity = self .get_trace_entity ()
352
370
if entity and entity .sampled :
353
371
entity .put_metadata (key , value , namespace )
@@ -357,6 +375,9 @@ def is_sampled(self):
357
375
Check if the current trace entity is sampled or not.
358
376
Return `False` if no active entity found.
359
377
"""
378
+ if not global_sdk_config .sdk_enabled ():
379
+ # Disabled SDK is never sampled
380
+ return False
360
381
entity = self .get_trace_entity ()
361
382
if entity :
362
383
return entity .sampled
@@ -404,16 +425,6 @@ def capture(self, name=None):
404
425
def record_subsegment (self , wrapped , instance , args , kwargs , name ,
405
426
namespace , meta_processor ):
406
427
407
- # In the case when the SDK is disabled, we ensure that a parent segment exists, because this is usually
408
- # handled by the middleware. We generate a dummy segment as the parent segment if one doesn't exist.
409
- # This is to allow potential segment method calls to not throw exceptions in the captured method.
410
- if not global_sdk_config .sdk_enabled ():
411
- try :
412
- self .current_segment ()
413
- except SegmentNotFoundException :
414
- segment = DummySegment (name )
415
- self .context .put_segment (segment )
416
-
417
428
subsegment = self .begin_subsegment (name , namespace )
418
429
419
430
exception = None
0 commit comments