-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathinstrumentDurableObjectStorage.ts
More file actions
50 lines (44 loc) · 1.75 KB
/
instrumentDurableObjectStorage.ts
File metadata and controls
50 lines (44 loc) · 1.75 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
import type { DurableObjectStorage } from '@cloudflare/workers-types';
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, startSpan } from '@sentry/core';
const STORAGE_METHODS_TO_INSTRUMENT = ['get', 'put', 'delete', 'list'] as const;
type StorageMethod = (typeof STORAGE_METHODS_TO_INSTRUMENT)[number];
/**
* Instruments DurableObjectStorage methods with Sentry spans.
*
* Wraps the following async methods:
* - get, put, delete, list (KV API)
*
* @param storage - The DurableObjectStorage instance to instrument
* @returns An instrumented DurableObjectStorage instance
*/
export function instrumentDurableObjectStorage(storage: DurableObjectStorage): DurableObjectStorage {
return new Proxy(storage, {
get(target, prop, receiver) {
const original = Reflect.get(target, prop, receiver);
if (typeof original !== 'function') {
return original;
}
const methodName = prop as string;
if (!STORAGE_METHODS_TO_INSTRUMENT.includes(methodName as StorageMethod)) {
return (original as (...args: unknown[]) => unknown).bind(target);
}
return function (this: unknown, ...args: unknown[]) {
return startSpan(
{
// Use underscore naming to match Cloudflare's native instrumentation (e.g., "durable_object_storage_get")
name: `durable_object_storage_${methodName}`,
op: 'db',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.db.cloudflare.durable_object',
'db.system.name': 'cloudflare.durable_object.storage',
'db.operation.name': methodName,
},
},
() => {
return (original as (...args: unknown[]) => unknown).apply(target, args);
},
);
};
},
});
}