@@ -102,6 +102,7 @@ import Policy = google.iam.v1.Policy;
102102import FieldMask = google . protobuf . FieldMask ;
103103import IDatabase = google . spanner . admin . database . v1 . IDatabase ;
104104import snakeCase = require( 'lodash.snakecase' ) ;
105+ import { getActiveOrNoopSpan } from './instrument' ;
105106
106107export type GetDatabaseRolesCallback = RequestCallback <
107108 IDatabaseRole ,
@@ -824,10 +825,17 @@ class Database extends common.GrpcServiceObject {
824825 const transaction = this . batchTransaction ( { session : session ! } , options ) ;
825826 this . _releaseOnEnd ( session ! , transaction ) ;
826827 transaction . begin ( ( err , resp ) => {
828+ const span = getActiveOrNoopSpan ( ) ;
827829 if ( err ) {
830+ if ( isSessionNotFoundError ( err ) ) {
831+ span . addEvent ( 'No session available' , {
832+ 'session.id' : session ?. id ,
833+ } ) ;
834+ }
828835 callback ! ( err , null , resp ! ) ;
829836 return ;
830837 }
838+ span . addEvent ( 'Using Session' , { 'session.id' : session ?. id } ) ;
831839 callback ! ( null , transaction , resp ! ) ;
832840 } ) ;
833841 } ) ;
@@ -2031,12 +2039,17 @@ class Database extends common.GrpcServiceObject {
20312039 const snapshot = session ! . snapshot ( options , this . queryOptions_ ) ;
20322040
20332041 snapshot . begin ( err => {
2042+ const span = getActiveOrNoopSpan ( ) ;
20342043 if ( err ) {
20352044 if ( isSessionNotFoundError ( err ) ) {
2045+ span . addEvent ( 'No session available' , {
2046+ 'session.id' : session ?. id ,
2047+ } ) ;
20362048 session ! . lastError = err ;
20372049 this . pool_ . release ( session ! ) ;
20382050 this . getSnapshot ( options , callback ! ) ;
20392051 } else {
2052+ span . addEvent ( 'Using Session' , { 'session.id' : session ?. id } ) ;
20402053 this . pool_ . release ( session ! ) ;
20412054 callback ! ( err ) ;
20422055 }
@@ -2121,7 +2134,14 @@ class Database extends common.GrpcServiceObject {
21212134 transaction ! . excludeTxnFromChangeStreams ( ) ;
21222135 }
21232136 if ( ! err ) {
2137+ const span = getActiveOrNoopSpan ( ) ;
2138+ span . addEvent ( 'Using Session' , { 'session.id' : session ?. id } ) ;
21242139 this . _releaseOnEnd ( session ! , transaction ! ) ;
2140+ } else if ( isSessionNotFoundError ( err as grpc . ServiceError ) ) {
2141+ const span = getActiveOrNoopSpan ( ) ;
2142+ span . addEvent ( 'No session available' , {
2143+ 'session.id' : session ?. id ,
2144+ } ) ;
21252145 }
21262146 cb ! ( err as grpc . ServiceError | null , transaction ) ;
21272147 } ) ;
@@ -2348,6 +2368,8 @@ class Database extends common.GrpcServiceObject {
23482368 callback ! ( err as ServiceError , null ) ;
23492369 return ;
23502370 }
2371+ const span = getActiveOrNoopSpan ( ) ;
2372+ span . addEvent ( 'Using Session' , { 'session.id' : session ?. id } ) ;
23512373 config . reqOpts . session = session ! . formattedName_ ;
23522374 this . request < Session > ( config , ( err , ...args ) => {
23532375 pool . release ( session ! ) ;
@@ -2389,10 +2411,17 @@ class Database extends common.GrpcServiceObject {
23892411 }
23902412 waitForSessionStream . on ( 'reading' , ( ) => {
23912413 pool . getSession ( ( err , session_ ) => {
2414+ const span = getActiveOrNoopSpan ( ) ;
23922415 if ( err ) {
2416+ if ( isSessionNotFoundError ( err as grpc . ServiceError ) ) {
2417+ span . addEvent ( 'No session available' , {
2418+ 'session.id' : session ?. id ,
2419+ } ) ;
2420+ }
23932421 destroyStream ( err as ServiceError ) ;
23942422 return ;
23952423 }
2424+ span . addEvent ( 'Using Session' , { 'session.id' : session_ ?. id } ) ;
23962425 session = session_ ! ;
23972426 config . reqOpts . session = session ! . formattedName_ ;
23982427 requestStream = self . requestStream ( config ) ;
@@ -2924,13 +2953,17 @@ class Database extends common.GrpcServiceObject {
29242953 options ?: TimestampBounds
29252954 ) : PartialResultStream {
29262955 const proxyStream : Transform = through . obj ( ) ;
2956+ const span = getActiveOrNoopSpan ( ) ;
29272957
29282958 this . pool_ . getSession ( ( err , session ) => {
29292959 if ( err ) {
29302960 proxyStream . destroy ( err ) ;
29312961 return ;
29322962 }
29332963
2964+ const span = getActiveOrNoopSpan ( ) ;
2965+ span . addEvent ( 'Using Session' , { 'session.id' : session ?. id } ) ;
2966+
29342967 const snapshot = session ! . snapshot ( options , this . queryOptions_ ) ;
29352968
29362969 this . _releaseOnEnd ( session ! , snapshot ) ;
@@ -2951,6 +2984,9 @@ class Database extends common.GrpcServiceObject {
29512984 if ( session ) {
29522985 session . lastError = err as grpc . ServiceError ;
29532986 }
2987+ span . addEvent ( 'No session available' , {
2988+ 'session.id' : session ?. id ,
2989+ } ) ;
29542990 // Remove the current data stream from the end user stream.
29552991 dataStream . unpipe ( proxyStream ) ;
29562992 dataStream . removeListener ( 'end' , endListener ) ;
@@ -3081,7 +3117,11 @@ class Database extends common.GrpcServiceObject {
30813117 : { } ;
30823118
30833119 this . pool_ . getSession ( ( err , session ?, transaction ?) => {
3120+ const span = getActiveOrNoopSpan ( ) ;
30843121 if ( err && isSessionNotFoundError ( err as grpc . ServiceError ) ) {
3122+ span . addEvent ( 'No session available' , {
3123+ 'session.id' : session ?. id ,
3124+ } ) ;
30853125 this . runTransaction ( options , runFn ! ) ;
30863126 return ;
30873127 }
@@ -3105,10 +3145,17 @@ class Database extends common.GrpcServiceObject {
31053145 ) ;
31063146
31073147 runner . run ( ) . then ( release , err => {
3148+ const span = getActiveOrNoopSpan ( ) ;
31083149 if ( isSessionNotFoundError ( err ) ) {
3150+ span . addEvent ( 'No session available' , {
3151+ 'session.id' : session ?. id ,
3152+ } ) ;
31093153 release ( ) ;
31103154 this . runTransaction ( options , runFn ! ) ;
31113155 } else {
3156+ if ( ! err ) {
3157+ span . addEvent ( 'Using Session' , { 'session.id' : session ! . id } ) ;
3158+ }
31123159 setImmediate ( runFn ! , err ) ;
31133160 release ( ) ;
31143161 }
@@ -3195,7 +3242,9 @@ class Database extends common.GrpcServiceObject {
31953242 ? ( optionsOrRunFn as RunTransactionOptions )
31963243 : { } ;
31973244
3245+ let sessionId = '' ;
31983246 const getSession = this . pool_ . getSession . bind ( this . pool_ ) ;
3247+ const span = getActiveOrNoopSpan ( ) ;
31993248 // Loop to retry 'Session not found' errors.
32003249 // (and yes, we like while (true) more than for (;;) here)
32013250 // eslint-disable-next-line no-constant-condition
@@ -3212,6 +3261,8 @@ class Database extends common.GrpcServiceObject {
32123261 if ( options . excludeTxnFromChangeStreams ) {
32133262 transaction . excludeTxnFromChangeStreams ( ) ;
32143263 }
3264+ sessionId = session ?. id ;
3265+ span . addEvent ( 'Using Session' , { 'session.id' : sessionId } ) ;
32153266 const runner = new AsyncTransactionRunner < T > (
32163267 session ,
32173268 transaction ,
@@ -3226,6 +3277,9 @@ class Database extends common.GrpcServiceObject {
32263277 }
32273278 } catch ( e ) {
32283279 if ( ! isSessionNotFoundError ( e as ServiceError ) ) {
3280+ span . addEvent ( 'No session available' , {
3281+ 'session.id' : sessionId ,
3282+ } ) ;
32293283 throw e ;
32303284 }
32313285 }
@@ -3301,6 +3355,10 @@ class Database extends common.GrpcServiceObject {
33013355 proxyStream . destroy ( err ) ;
33023356 return ;
33033357 }
3358+
3359+ const span = getActiveOrNoopSpan ( ) ;
3360+ span . addEvent ( 'Using Session' , { 'session.id' : session ?. id } ) ;
3361+
33043362 const gaxOpts = extend ( true , { } , options ?. gaxOptions ) ;
33053363 const reqOpts = Object . assign (
33063364 { } as spannerClient . spanner . v1 . BatchWriteRequest ,
@@ -3332,6 +3390,7 @@ class Database extends common.GrpcServiceObject {
33323390 if ( session ) {
33333391 session . lastError = err as grpc . ServiceError ;
33343392 }
3393+ span . addEvent ( 'No session available' , { 'session.id' : session ?. id } ) ;
33353394 // Remove the current data stream from the end user stream.
33363395 dataStream . unpipe ( proxyStream ) ;
33373396 dataStream . end ( ) ;
@@ -3424,14 +3483,19 @@ class Database extends common.GrpcServiceObject {
34243483 ? ( optionsOrCallback as CallOptions )
34253484 : { } ;
34263485 this . pool_ . getSession ( ( err , session ?, transaction ?) => {
3486+ const span = getActiveOrNoopSpan ( ) ;
34273487 if ( err && isSessionNotFoundError ( err as grpc . ServiceError ) ) {
3488+ span . addEvent ( 'No session available' , {
3489+ 'session.id' : session ?. id ,
3490+ } ) ;
34283491 this . writeAtLeastOnce ( mutations , options , cb ! ) ;
34293492 return ;
34303493 }
34313494 if ( err ) {
34323495 cb ! ( err as grpc . ServiceError ) ;
34333496 return ;
34343497 }
3498+ span . addEvent ( 'Using Session' , { 'session.id' : session ?. id } ) ;
34353499 this . _releaseOnEnd ( session ! , transaction ! ) ;
34363500 transaction ?. setQueuedMutations ( mutations . proto ( ) ) ;
34373501 return transaction ?. commit ( options , cb ! ) ;
0 commit comments