@@ -8,6 +8,7 @@ import { OnUncaughtException, OnUnhandledRejection } from '@sentry/integration-n
8
8
import { ConsoleBreadcrumbs , HTTPBreadcrumbs } from '@sentry/integration-node-breadcrumbs' ;
9
9
import { HTTPTransport } from '@sentry/transport-http' ;
10
10
import { ClientLike , Integration } from '@sentry/types' ;
11
+ import { sync as readPkgUp } from 'read-pkg-up' ;
11
12
12
13
import { NodeClient , NodeOptions } from './client' ;
13
14
@@ -21,26 +22,33 @@ export function init(options: NodeOptions = {}): ClientLike {
21
22
}
22
23
23
24
export function initClient ( options : NodeOptions = { } ) : ClientLike {
24
- options . dsn = options . dsn ?? process . env . SENTRY_DSN ;
25
- options . release = options . release ?? process . env . SENTRY_RELEASE ;
26
- options . environment = options . environment ?? process . env . SENTRY_ENVIRONMENT ;
25
+ const opts : NodeOptions = {
26
+ dsn : process . env . SENTRY_DSN ,
27
+ release : process . env . SENTRY_RELEASE ,
28
+ environment : process . env . SENTRY_ENVIRONMENT ,
29
+ transport : HTTPTransport ,
30
+ defaultIntegrations : true ,
31
+ discoverIntegrations : true ,
32
+ ...options ,
33
+ _internal : {
34
+ defaultIntegrations :
35
+ options . defaultIntegrations === false ? [ ] : options . _internal ?. defaultIntegrations || getDefaultIntegrations ( ) ,
36
+ discoveredIntegrations :
37
+ options . discoverIntegrations === false
38
+ ? [ ]
39
+ : options . _internal ?. discoveredIntegrations || discoverIntegrations ( ) ,
40
+ ...options . _internal ,
41
+ } ,
42
+ } ;
27
43
28
- if ( options . tracesSampleRate === undefined && process . env . SENTRY_TRACES_SAMPLE_RATE ) {
44
+ if ( ! ( ' tracesSampleRate' in opts ) && process . env . SENTRY_TRACES_SAMPLE_RATE ) {
29
45
const tracesSampleRate = parseFloat ( process . env . SENTRY_TRACES_SAMPLE_RATE ) ;
30
46
if ( isFinite ( tracesSampleRate ) ) {
31
- options . tracesSampleRate = tracesSampleRate ;
47
+ opts . tracesSampleRate = tracesSampleRate ;
32
48
}
33
49
}
34
50
35
- options . transport = options . transport ?? HTTPTransport ;
36
-
37
- options . _internal = options . _internal || { } ;
38
- options . _internal . defaultIntegrations = options . defaultIntegrations
39
- ? options . _internal . defaultIntegrations || getDefaultIntegrations ( )
40
- : [ ] ;
41
- options . _internal . discoveredIntegrations = options . discoverIntegrations ? discoverIntegrations ( ) : [ ] ;
42
-
43
- return new NodeClient ( options ) ;
51
+ return new NodeClient ( opts ) ;
44
52
}
45
53
46
54
export const getDefaultIntegrations = ( ) : Integration [ ] => [
@@ -54,5 +62,25 @@ export const getDefaultIntegrations = (): Integration[] => [
54
62
] ;
55
63
56
64
function discoverIntegrations ( ) : Integration [ ] {
57
- return [ ] ;
65
+ const pkg = readPkgUp ( ) ;
66
+
67
+ if ( ! pkg ) {
68
+ return [ ] ;
69
+ }
70
+
71
+ return Object . keys ( {
72
+ ...pkg . packageJson . dependencies ,
73
+ ...pkg . packageJson . devDependencies ,
74
+ } )
75
+ . filter ( name => {
76
+ return / ^ @ s e n t r y \/ i n t e g r a t i o n - ( c o m m o n | n o d e ) - [ a - z ] / . test ( name ) ;
77
+ } )
78
+ . map ( name => {
79
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
80
+ const mod = require ( name ) ;
81
+ return Object . values ( mod ) as { new ( ) : Integration } [ ] ;
82
+ } )
83
+ . reduce ( ( acc , integrations ) => {
84
+ return acc . concat ( integrations . map ( Integration => new Integration ( ) ) ) ;
85
+ } , [ ] as Integration [ ] ) ;
58
86
}
0 commit comments