@@ -3,6 +3,7 @@ import { Injectable, Optional } from '@nestjs/common';
3
3
import { $ , ConstraintViolationError , EdgeDBError , Executor } from 'edgedb' ;
4
4
import { QueryArgs } from 'edgedb/dist/ifaces' ;
5
5
import { retry , RetryOptions } from '~/common/retry' ;
6
+ import { TracingService } from '~/core/tracing' ;
6
7
import { jestSkipFileInExceptionSource } from '../exception' ;
7
8
import { TypedEdgeQL } from './edgeql' ;
8
9
import { enhanceConstraintError } from './errors' ;
@@ -17,6 +18,7 @@ export class EdgeDB {
17
18
private readonly client : Client ,
18
19
private readonly transactionContext : TransactionContext ,
19
20
private readonly optionsContext : OptionsContext ,
21
+ private readonly tracing : TracingService ,
20
22
@Optional ( ) private readonly childOptions : ApplyOptions [ ] = [ ] ,
21
23
@Optional ( ) private childExecutor ?: Executor ,
22
24
) { }
@@ -26,6 +28,7 @@ export class EdgeDB {
26
28
this . client ,
27
29
this . transactionContext ,
28
30
this . optionsContext ,
31
+ this . tracing ,
29
32
[ ...this . childOptions ] ,
30
33
this . childExecutor ,
31
34
) ;
@@ -104,55 +107,62 @@ export class EdgeDB {
104
107
105
108
async run ( query : any , args ?: any ) {
106
109
const executor = this . childExecutor ?? this . transactionContext . current ;
107
- try {
108
- if ( query instanceof TypedEdgeQL ) {
109
- const found = InlineQueryRuntimeMap . get ( query . query ) ;
110
- if ( ! found ) {
111
- throw new Error ( `Query was not found from inline query generation` ) ;
110
+ return await this . tracing . capture ( 'EdgeDB Query' , async ( segment ) => {
111
+ // Show this segment separately in the service map
112
+ segment . namespace = 'remote' ;
113
+ // Help ID the segment as being for a database
114
+ segment . sql = { } ;
115
+
116
+ try {
117
+ if ( query instanceof TypedEdgeQL ) {
118
+ const found = InlineQueryRuntimeMap . get ( query . query ) ;
119
+ if ( ! found ) {
120
+ throw new Error ( `Query was not found from inline query generation` ) ;
121
+ }
122
+ const exeMethod = cardinalityToExecutorMethod [ found . cardinality ] ;
123
+
124
+ // eslint-disable-next-line @typescript-eslint/return-await
125
+ return await executor [ exeMethod ] ( found . query , args ) ;
112
126
}
113
- const exeMethod = cardinalityToExecutorMethod [ found . cardinality ] ;
114
127
115
- // eslint-disable-next-line @typescript-eslint/return-await
116
- return await executor [ exeMethod ] ( found . query , args ) ;
117
- }
118
-
119
- if ( query . run ) {
120
- // eslint-disable-next-line @typescript-eslint/return-await
121
- return await query . run ( executor , args ) ;
122
- }
128
+ if ( query . run ) {
129
+ // eslint-disable-next-line @typescript-eslint/return-await
130
+ return await query . run ( executor , args ) ;
131
+ }
123
132
124
- if ( typeof query === 'function' ) {
125
- // eslint-disable-next-line @typescript-eslint/return-await
126
- return await query ( executor , args ) ;
127
- }
133
+ if ( typeof query === 'function' ) {
134
+ // eslint-disable-next-line @typescript-eslint/return-await
135
+ return await query ( executor , args ) ;
136
+ }
128
137
129
- // For REPL, as this is untyped and assumes many/empty cardinality
130
- if ( typeof query === 'string' ) {
131
- return await executor . query ( query , args ) ;
132
- }
133
- } catch ( e ) {
134
- // Ignore this call in stack trace. This puts the actual query as the first.
135
- e . stack = e . stack ! . replace ( / ^ \s + a t (?: a s y n c ) ? E d g e D B \. r u n .+ $ \n / m, '' ) ;
136
-
137
- // Don't present abstract repositories as the src block in jest reports
138
- // for DB execution errors.
139
- // There shouldn't be anything specific to there to be helpful.
140
- // This is a bit of a broad assumption though, so only do for jest and
141
- // keep the frame for actual use from users/devs.
142
- if ( e instanceof EdgeDBError ) {
143
- jestSkipFileInExceptionSource (
144
- e ,
145
- / ^ \s + a t .+ s r c [ / | \\ ] c o r e [ / | \\ ] e d g e d b [ / | \\ ] .+ \. r e p o s i t o r y \. .+ $ \n / gm,
146
- ) ;
147
- }
138
+ // For REPL, as this is untyped and assumes many/empty cardinality
139
+ if ( typeof query === 'string' ) {
140
+ return await executor . query ( query , args ) ;
141
+ }
142
+ } catch ( e ) {
143
+ // Ignore this call in stack trace. This puts the actual query as the first.
144
+ e . stack = e . stack ! . replace ( / ^ \s + a t (?: a s y n c ) ? E d g e D B \. r u n .+ $ \n / m, '' ) ;
145
+
146
+ // Don't present abstract repositories as the src block in jest reports
147
+ // for DB execution errors.
148
+ // There shouldn't be anything specific to there to be helpful.
149
+ // This is a bit of a broad assumption though, so only do for jest and
150
+ // keep the frame for actual use from users/devs.
151
+ if ( e instanceof EdgeDBError ) {
152
+ jestSkipFileInExceptionSource (
153
+ e ,
154
+ / ^ \s + a t .+ s r c [ / | \\ ] c o r e [ / | \\ ] e d g e d b [ / | \\ ] .+ \. r e p o s i t o r y \. .+ $ \n / gm,
155
+ ) ;
156
+ }
148
157
149
- if ( e instanceof ConstraintViolationError ) {
150
- throw enhanceConstraintError ( e ) ;
158
+ if ( e instanceof ConstraintViolationError ) {
159
+ throw enhanceConstraintError ( e ) ;
160
+ }
161
+ throw e ;
151
162
}
152
- throw e ;
153
- }
154
163
155
- throw new Error ( 'Could not figure out how to run given query' ) ;
164
+ throw new Error ( 'Could not figure out how to run given query' ) ;
165
+ } ) ;
156
166
}
157
167
}
158
168
0 commit comments