@@ -25,6 +25,7 @@ import {
2525 CLOUD_RESOURCE_HEADER ,
2626 addLeaderAwareRoutingHeader ,
2727} from '../src/common' ;
28+ import { startTrace , setSpanError , traceConfig } from './instrument' ;
2829
2930export interface TransactionIdentifier {
3031 session : string | Session ;
@@ -136,20 +137,37 @@ class BatchTransaction extends Snapshot {
136137 delete reqOpts . gaxOptions ;
137138 delete reqOpts . types ;
138139
139- const headers : { [ k : string ] : string } = { } ;
140- if ( this . _getSpanner ( ) . routeToLeaderEnabled ) {
141- addLeaderAwareRoutingHeader ( headers ) ;
142- }
140+ const traceConfig : traceConfig = {
141+ sql : query ,
142+ opts : this . observabilityOptions ,
143+ } ;
144+ return startTrace (
145+ 'BatchTransaction.createQueryPartitions' ,
146+ traceConfig ,
147+ span => {
148+ const headers : { [ k : string ] : string } = { } ;
149+ if ( this . _getSpanner ( ) . routeToLeaderEnabled ) {
150+ addLeaderAwareRoutingHeader ( headers ) ;
151+ }
143152
144- this . createPartitions_ (
145- {
146- client : 'SpannerClient' ,
147- method : 'partitionQuery' ,
148- reqOpts,
149- gaxOpts : query . gaxOptions ,
150- headers : headers ,
151- } ,
152- callback
153+ this . createPartitions_ (
154+ {
155+ client : 'SpannerClient' ,
156+ method : 'partitionQuery' ,
157+ reqOpts,
158+ gaxOpts : query . gaxOptions ,
159+ headers : headers ,
160+ } ,
161+ ( err , partitions , resp ) => {
162+ if ( err ) {
163+ setSpanError ( span , err ) ;
164+ }
165+
166+ span . end ( ) ;
167+ callback ( err , partitions , resp ) ;
168+ }
169+ ) ;
170+ }
153171 ) ;
154172 }
155173 /**
@@ -163,38 +181,52 @@ class BatchTransaction extends Snapshot {
163181 * @param {function } callback Callback function.
164182 */
165183 createPartitions_ ( config , callback ) {
166- const query = extend ( { } , config . reqOpts , {
167- session : this . session . formattedName_ ,
168- transaction : { id : this . id } ,
169- } ) ;
170- config . reqOpts = extend ( { } , query ) ;
171- config . headers = {
172- [ CLOUD_RESOURCE_HEADER ] : ( this . session . parent as Database ) . formattedName_ ,
184+ const traceConfig : traceConfig = {
185+ opts : this . observabilityOptions ,
173186 } ;
174- delete query . partitionOptions ;
175- this . session . request ( config , ( err , resp ) => {
176- if ( err ) {
177- callback ( err , null , resp ) ;
178- return ;
179- }
180187
181- const partitions = resp . partitions . map ( partition => {
182- return extend ( { } , query , partition ) ;
183- } ) ;
188+ return startTrace (
189+ 'BatchTransaction.createPartitions_' ,
190+ traceConfig ,
191+ span => {
192+ const query = extend ( { } , config . reqOpts , {
193+ session : this . session . formattedName_ ,
194+ transaction : { id : this . id } ,
195+ } ) ;
196+ config . reqOpts = extend ( { } , query ) ;
197+ config . headers = {
198+ [ CLOUD_RESOURCE_HEADER ] : ( this . session . parent as Database )
199+ . formattedName_ ,
200+ } ;
201+ delete query . partitionOptions ;
202+ this . session . request ( config , ( err , resp ) => {
203+ if ( err ) {
204+ setSpanError ( span , err ) ;
205+ span . end ( ) ;
206+ callback ( err , null , resp ) ;
207+ return ;
208+ }
184209
185- if ( resp . transaction ) {
186- const { id, readTimestamp} = resp . transaction ;
210+ const partitions = resp . partitions . map ( partition => {
211+ return extend ( { } , query , partition ) ;
212+ } ) ;
187213
188- this . id = id ;
214+ if ( resp . transaction ) {
215+ const { id, readTimestamp} = resp . transaction ;
189216
190- if ( readTimestamp ) {
191- this . readTimestampProto = readTimestamp ;
192- this . readTimestamp = new PreciseDate ( readTimestamp ) ;
193- }
194- }
217+ this . id = id ;
218+
219+ if ( readTimestamp ) {
220+ this . readTimestampProto = readTimestamp ;
221+ this . readTimestamp = new PreciseDate ( readTimestamp ) ;
222+ }
223+ }
195224
196- callback ( null , partitions , resp ) ;
197- } ) ;
225+ span . end ( ) ;
226+ callback ( null , partitions , resp ) ;
227+ } ) ;
228+ }
229+ ) ;
198230 }
199231 /**
200232 * @typedef {object } ReadPartition
@@ -226,28 +258,45 @@ class BatchTransaction extends Snapshot {
226258 * @returns {Promise<CreateReadPartitionsResponse> }
227259 */
228260 createReadPartitions ( options , callback ) {
229- const reqOpts = Object . assign ( { } , options , {
230- keySet : Snapshot . encodeKeySet ( options ) ,
231- } ) ;
261+ const traceConfig : traceConfig = {
262+ opts : this . observabilityOptions ,
263+ } ;
232264
233- delete reqOpts . gaxOptions ;
234- delete reqOpts . keys ;
235- delete reqOpts . ranges ;
265+ return startTrace (
266+ 'BatchTransaction.createReadPartitions' ,
267+ traceConfig ,
268+ span => {
269+ const reqOpts = Object . assign ( { } , options , {
270+ keySet : Snapshot . encodeKeySet ( options ) ,
271+ } ) ;
236272
237- const headers : { [ k : string ] : string } = { } ;
238- if ( this . _getSpanner ( ) . routeToLeaderEnabled ) {
239- addLeaderAwareRoutingHeader ( headers ) ;
240- }
273+ delete reqOpts . gaxOptions ;
274+ delete reqOpts . keys ;
275+ delete reqOpts . ranges ;
241276
242- this . createPartitions_ (
243- {
244- client : 'SpannerClient' ,
245- method : 'partitionRead' ,
246- reqOpts,
247- gaxOpts : options . gaxOptions ,
248- headers : headers ,
249- } ,
250- callback
277+ const headers : { [ k : string ] : string } = { } ;
278+ if ( this . _getSpanner ( ) . routeToLeaderEnabled ) {
279+ addLeaderAwareRoutingHeader ( headers ) ;
280+ }
281+
282+ this . createPartitions_ (
283+ {
284+ client : 'SpannerClient' ,
285+ method : 'partitionRead' ,
286+ reqOpts,
287+ gaxOpts : options . gaxOptions ,
288+ headers : headers ,
289+ } ,
290+ ( err , partitions , resp ) => {
291+ if ( err ) {
292+ setSpanError ( span , err ) ;
293+ }
294+
295+ span . end ( ) ;
296+ callback ( err , partitions , resp ) ;
297+ }
298+ ) ;
299+ }
251300 ) ;
252301 }
253302 /**
@@ -322,6 +371,7 @@ class BatchTransaction extends Snapshot {
322371 * ```
323372 */
324373 executeStream ( partition ) {
374+ // TODO: Instrument the streams with Otel.
325375 if ( is . string ( partition . table ) ) {
326376 return this . createReadStream ( partition . table , partition ) ;
327377 }
0 commit comments