forked from getsentry/sentry-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextensions.ts
72 lines (64 loc) · 2.12 KB
/
extensions.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { addTracingExtensions, getMainCarrier } from '@sentry/core';
import type { Integration, IntegrationClass } from '@sentry/types';
import { dynamicRequire, isNodeEnv, loadModule } from '@sentry/utils';
import { registerErrorInstrumentation } from './errors';
/**
* @private
*/
function _autoloadDatabaseIntegrations(): void {
const carrier = getMainCarrier();
if (!carrier.__SENTRY__) {
return;
}
const packageToIntegrationMapping: Record<string, () => Integration> = {
mongodb() {
const integration = dynamicRequire(module, './integrations/node/mongo') as {
Mongo: IntegrationClass<Integration>;
};
return new integration.Mongo();
},
mongoose() {
const integration = dynamicRequire(module, './integrations/node/mongo') as {
Mongo: IntegrationClass<Integration>;
};
return new integration.Mongo({ mongoose: true });
},
mysql() {
const integration = dynamicRequire(module, './integrations/node/mysql') as {
Mysql: IntegrationClass<Integration>;
};
return new integration.Mysql();
},
pg() {
const integration = dynamicRequire(module, './integrations/node/postgres') as {
Postgres: IntegrationClass<Integration>;
};
return new integration.Postgres();
},
};
const mappedPackages = Object.keys(packageToIntegrationMapping)
.filter(moduleName => !!loadModule(moduleName))
.map(pkg => {
try {
return packageToIntegrationMapping[pkg]();
} catch (e) {
return undefined;
}
})
.filter(p => p) as Integration[];
if (mappedPackages.length > 0) {
carrier.__SENTRY__.integrations = [...(carrier.__SENTRY__.integrations || []), ...mappedPackages];
}
}
/**
* This patches the global object and injects the Tracing extensions methods
*/
export function addExtensionMethods(): void {
addTracingExtensions();
// Detect and automatically load specified integrations.
if (isNodeEnv()) {
_autoloadDatabaseIntegrations();
}
// If an error happens globally, we should make sure transaction status is set to error.
registerErrorInstrumentation();
}