@@ -251,11 +251,9 @@ def exists(self):
251251 span ,
252252 ),
253253 )
254- if span :
255- span .set_attribute ("session_found" , True )
254+ span .set_attribute ("session_found" , True )
256255 except NotFound :
257- if span :
258- span .set_attribute ("session_found" , False )
256+ span .set_attribute ("session_found" , False )
259257 return False
260258
261259 return True
@@ -317,18 +315,21 @@ def ping(self):
317315 """
318316 if self ._session_id is None :
319317 raise ValueError ("Session ID not set by back-end" )
318+
320319 database = self ._database
321320 api = database .spanner_api
322- request = ExecuteSqlRequest (session = self .name , sql = "SELECT 1" )
323- api .execute_sql (
324- request = request ,
325- metadata = database .metadata_with_request_id (
326- database ._next_nth_request ,
327- 1 ,
328- _metadata_with_prefix (database .name ),
329- ),
330- )
331- self ._last_use_time = datetime .now ()
321+
322+ with trace_call ("CloudSpanner.Session.ping" , self ) as span :
323+ request = ExecuteSqlRequest (session = self .name , sql = "SELECT 1" )
324+ api .execute_sql (
325+ request = request ,
326+ metadata = database .metadata_with_request_id (
327+ database ._next_nth_request ,
328+ 1 ,
329+ _metadata_with_prefix (database .name ),
330+ span ,
331+ ),
332+ )
332333
333334 def snapshot (self , ** kw ):
334335 """Create a snapshot to perform a set of reads with shared staleness.
@@ -566,20 +567,18 @@ def run_in_transaction(self, func, *args, **kw):
566567
567568 except Aborted as exc :
568569 previous_transaction_id = txn ._transaction_id
569- if span :
570- delay_seconds = _get_retry_delay (
571- exc .errors [0 ],
572- attempts ,
573- default_retry_delay = default_retry_delay ,
574- )
575- attributes = dict (delay_seconds = delay_seconds , cause = str (exc ))
576- attributes .update (span_attributes )
577- add_span_event (
578- span ,
579- "Transaction was aborted in user operation, retrying" ,
580- attributes ,
581- )
582-
570+ delay_seconds = _get_retry_delay (
571+ exc .errors [0 ],
572+ attempts ,
573+ default_retry_delay = default_retry_delay ,
574+ )
575+ attributes = dict (delay_seconds = delay_seconds , cause = str (exc ))
576+ attributes .update (span_attributes )
577+ add_span_event (
578+ span ,
579+ "Transaction was aborted in user operation, retrying" ,
580+ attributes ,
581+ )
583582 _delay_until_retry (
584583 exc , deadline , attempts , default_retry_delay = default_retry_delay
585584 )
@@ -611,20 +610,18 @@ def run_in_transaction(self, func, *args, **kw):
611610
612611 except Aborted as exc :
613612 previous_transaction_id = txn ._transaction_id
614- if span :
615- delay_seconds = _get_retry_delay (
616- exc .errors [0 ],
617- attempts ,
618- default_retry_delay = default_retry_delay ,
619- )
620- attributes = dict (delay_seconds = delay_seconds )
621- attributes .update (span_attributes )
622- add_span_event (
623- span ,
624- "Transaction was aborted during commit, retrying" ,
625- attributes ,
626- )
627-
613+ delay_seconds = _get_retry_delay (
614+ exc .errors [0 ],
615+ attempts ,
616+ default_retry_delay = default_retry_delay ,
617+ )
618+ attributes = dict (delay_seconds = delay_seconds )
619+ attributes .update (span_attributes )
620+ add_span_event (
621+ span ,
622+ "Transaction was aborted during commit, retrying" ,
623+ attributes ,
624+ )
628625 _delay_until_retry (
629626 exc , deadline , attempts , default_retry_delay = default_retry_delay
630627 )
0 commit comments