@@ -8,6 +8,7 @@ import { OnUncaughtException, OnUnhandledRejection } from '@sentry/integration-n
88import { ConsoleBreadcrumbs , HTTPBreadcrumbs } from '@sentry/integration-node-breadcrumbs' ;
99import { HTTPTransport } from '@sentry/transport-http' ;
1010import { ClientLike , Integration } from '@sentry/types' ;
11+ import { sync as readPkgUp } from 'read-pkg-up' ;
1112
1213import { NodeClient , NodeOptions } from './client' ;
1314
@@ -21,26 +22,33 @@ export function init(options: NodeOptions = {}): ClientLike {
2122}
2223
2324export 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+ } ;
2743
28- if ( options . tracesSampleRate === undefined && process . env . SENTRY_TRACES_SAMPLE_RATE ) {
44+ if ( ! ( ' tracesSampleRate' in opts ) && process . env . SENTRY_TRACES_SAMPLE_RATE ) {
2945 const tracesSampleRate = parseFloat ( process . env . SENTRY_TRACES_SAMPLE_RATE ) ;
3046 if ( isFinite ( tracesSampleRate ) ) {
31- options . tracesSampleRate = tracesSampleRate ;
47+ opts . tracesSampleRate = tracesSampleRate ;
3248 }
3349 }
3450
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 ) ;
4452}
4553
4654export const getDefaultIntegrations = ( ) : Integration [ ] => [
@@ -54,5 +62,25 @@ export const getDefaultIntegrations = (): Integration[] => [
5462] ;
5563
5664function 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 [ ] ) ;
5886}
0 commit comments