Skip to content

Commit 4d6a80c

Browse files
committed
Add tracing to EdgeDB queries
1 parent b58da6b commit 4d6a80c

File tree

1 file changed

+52
-42
lines changed

1 file changed

+52
-42
lines changed

src/core/edgedb/edgedb.service.ts

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Injectable, Optional } from '@nestjs/common';
33
import { $, ConstraintViolationError, EdgeDBError, Executor } from 'edgedb';
44
import { QueryArgs } from 'edgedb/dist/ifaces';
55
import { retry, RetryOptions } from '~/common/retry';
6+
import { TracingService } from '~/core/tracing';
67
import { jestSkipFileInExceptionSource } from '../exception';
78
import { TypedEdgeQL } from './edgeql';
89
import { enhanceConstraintError } from './errors';
@@ -17,6 +18,7 @@ export class EdgeDB {
1718
private readonly client: Client,
1819
private readonly transactionContext: TransactionContext,
1920
private readonly optionsContext: OptionsContext,
21+
private readonly tracing: TracingService,
2022
@Optional() private readonly childOptions: ApplyOptions[] = [],
2123
@Optional() private childExecutor?: Executor,
2224
) {}
@@ -26,6 +28,7 @@ export class EdgeDB {
2628
this.client,
2729
this.transactionContext,
2830
this.optionsContext,
31+
this.tracing,
2932
[...this.childOptions],
3033
this.childExecutor,
3134
);
@@ -104,55 +107,62 @@ export class EdgeDB {
104107

105108
async run(query: any, args?: any) {
106109
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);
112126
}
113-
const exeMethod = cardinalityToExecutorMethod[found.cardinality];
114127

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+
}
123132

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+
}
128137

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+at(?: async)? EdgeDB\.run.+$\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+at .+src[/|\\]core[/|\\]edgedb[/|\\].+\.repository\..+$\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+at(?: async)? EdgeDB\.run.+$\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+at .+src[/|\\]core[/|\\]edgedb[/|\\].+\.repository\..+$\n/gm,
155+
);
156+
}
148157

149-
if (e instanceof ConstraintViolationError) {
150-
throw enhanceConstraintError(e);
158+
if (e instanceof ConstraintViolationError) {
159+
throw enhanceConstraintError(e);
160+
}
161+
throw e;
151162
}
152-
throw e;
153-
}
154163

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+
});
156166
}
157167
}
158168

0 commit comments

Comments
 (0)