@@ -15,7 +15,13 @@ import type {
1515 IntegrationFn ,
1616 OsContext ,
1717} from '@sentry/core' ;
18- import { debug , defineIntegration , getGlobalScope } from '@sentry/core' ;
18+ import {
19+ debug ,
20+ defineIntegration ,
21+ getCapturedScopesOnSpan ,
22+ getGlobalScope ,
23+ INTERNAL_getSegmentSpan ,
24+ } from '@sentry/core' ;
1925
2026export const readFileAsync = promisify ( readFile ) ;
2127export const readDirAsync = promisify ( readdir ) ;
@@ -107,8 +113,8 @@ const _nodeContextIntegration = ((options: ContextOptions = {}) => {
107113
108114 return {
109115 name : INTEGRATION_NAME ,
110- setupOnce ( ) {
111- console . log ( 'xx setupOnce' ) ;
116+ setup ( client ) {
117+ // first set all contexts on the global scope
112118 _getContexts ( )
113119 . then ( updatedContext => {
114120 const globalScope = getGlobalScope ( ) ;
@@ -120,17 +126,29 @@ const _nodeContextIntegration = ((options: ContextOptions = {}) => {
120126 device : { ...updatedContext . device , ...previousContexts ?. device } ,
121127 culture : { ...updatedContext . culture , ...previousContexts ?. culture } ,
122128 cloud_resource : { ...updatedContext . cloud_resource , ...previousContexts ?. cloud_resource } ,
129+ runtime : { name : 'node' , version : global . process . version , ...previousContexts ?. runtime } ,
123130 } ;
124131
125132 Object . keys ( contexts ) . forEach ( key => {
126133 globalScope . setContext ( key , contexts [ key as keyof Event [ 'contexts' ] ] ) ;
127134 } ) ;
128-
129- console . log ( 'xx set contexts to global scope' , contexts ) ;
130135 } )
131136 . catch ( ( ) => {
132137 debug . warn ( `[${ INTEGRATION_NAME } ] Failed to get contexts from Node` ) ;
133138 } ) ;
139+
140+ client . on ( 'spanEnd' , span => {
141+ if ( INTERNAL_getSegmentSpan ( span ) !== span ) {
142+ return ;
143+ }
144+ const currentScopeOfSpan = getCapturedScopesOnSpan ( span ) . scope ;
145+ if ( currentScopeOfSpan ) {
146+ const updatedContext = _updateContext ( getGlobalScope ( ) . getScopeData ( ) . contexts ) ;
147+ Object . keys ( updatedContext ) . forEach ( key => {
148+ currentScopeOfSpan . setContext ( key , updatedContext [ key as keyof Event [ 'contexts' ] ] ?? null ) ;
149+ } ) ;
150+ }
151+ } ) ;
134152 } ,
135153 // TODO (span-streaming): we probably need to apply this to spans via a hook IF we decide to apply contexts to (segment) spans
136154 processEvent ( event ) {
0 commit comments