Skip to content

Commit 07633a3

Browse files
committed
more contexts
1 parent 8602881 commit 07633a3

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

packages/core/src/spans/captureSpan.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,36 @@ function getFinalScopeData(isolationScope: Scope | undefined, scope: Scope | und
126126
return finalScopeData;
127127
}
128128

129+
// TODO: This should likely live in the Context integration since most of this data is only avialable in server runtime contexts
129130
function contextsToAttributes(contexts: Contexts): RawAttributes<Record<string, unknown>> {
130131
return {
132+
// os context
131133
'os.build_id': contexts.os?.build,
132134
'os.name': contexts.os?.name,
133135
'os.version': contexts.os?.version,
134136
// TODO: Add to Sentry SemConv
135137
'os.kernel_version': contexts.os?.kernel_version,
138+
139+
// runtime context
140+
// TODO: Add to Sentry SemConv
141+
'runtime.name': contexts.runtime?.name,
142+
// TODO: Add to Sentry SemConv
143+
'runtime.version': contexts.runtime?.version,
144+
145+
// TODO: All of them need to be added to Sentry SemConv (except family and model)
146+
...(contexts.app
147+
? Object.fromEntries(Object.entries(contexts.app).map(([key, value]) => [`app.${key}`, value]))
148+
: {}),
149+
...(contexts.device
150+
? Object.fromEntries(Object.entries(contexts.device).map(([key, value]) => [`device.${key}`, value]))
151+
: {}),
152+
...(contexts.culture
153+
? Object.fromEntries(Object.entries(contexts.culture).map(([key, value]) => [`culture.${key}`, value]))
154+
: {}),
155+
...(contexts.cloud_resource
156+
? Object.fromEntries(
157+
Object.entries(contexts.cloud_resource).map(([key, value]) => [`cloud_resource.${key}`, value]),
158+
)
159+
: {}),
136160
};
137161
}

packages/node-core/src/integrations/context.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2026
export const readFileAsync = promisify(readFile);
2127
export 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

Comments
 (0)