-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathinstrumentSqlStorage.ts
More file actions
51 lines (45 loc) · 1.74 KB
/
Copy pathinstrumentSqlStorage.ts
File metadata and controls
51 lines (45 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import type { SqlStorage, SqlStorageCursor, SqlStorageValue } from '@cloudflare/workers-types';
import { _INTERNAL_sanitizeSqlQuery, addBreadcrumb, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, startSpan } from '@sentry/core';
/**
* Instruments the Durable Object SqlStorage `exec` method with Sentry spans.
*
* @param sql - The SqlStorage instance to instrument
* @returns An instrumented SqlStorage instance
*/
export function instrumentSqlStorage(sql: SqlStorage): SqlStorage {
return new Proxy(sql, {
get(target, prop, _receiver) {
const original = Reflect.get(target, prop, target);
if (prop !== 'exec' || typeof original !== 'function') {
return original;
}
return function (this: unknown, ...args: unknown[]) {
const [query, ...bindings] = args as [string, ...unknown[]];
const sanitizedQuery = _INTERNAL_sanitizeSqlQuery(query);
return startSpan(
{
op: 'db.query',
name: sanitizedQuery,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.db.cloudflare.durable_object.sql',
'db.system.name': 'cloudflare-durable-object-sql',
'db.operation.name': 'exec',
'db.query.text': sanitizedQuery,
'cloudflare.durable_object.query.bindings': bindings.length,
},
},
() => {
const cursor: SqlStorageCursor<Record<string, SqlStorageValue>> = (
original as (...a: unknown[]) => SqlStorageCursor<Record<string, SqlStorageValue>>
).apply(target, args);
addBreadcrumb({
category: 'query',
message: sanitizedQuery,
});
return cursor;
},
);
};
},
});
}