Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ test('sends a streamed span envelope with correct spans for a manually started s
[SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_ID]: { type: 'string', value: segmentSpanId },
[SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_NAME]: { type: 'string', value: 'test-span' },
[SEMANTIC_ATTRIBUTE_SENTRY_RELEASE]: { type: 'string', value: '1.0.0' },
'process.runtime.engine.name': { type: 'string', value: 'v8' },
'process.runtime.engine.version': { type: 'string', value: expect.any(String) },
'app.start_time': { type: 'string', value: expect.any(String) },
'device.processor_count': { type: 'integer', value: expect.any(Number) },
},
name: 'test-span',
is_segment: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as Sentry from '@sentry/node';
import { loggingTransport } from '@sentry-internal/node-integration-tests';

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
tracesSampleRate: 1.0,
traceLifecycle: 'stream',
transport: loggingTransport,
});

Sentry.startSpan({ name: 'test-span' }, () => {
// noop
});

void Sentry.flush();
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { afterAll, expect, test } from 'vitest';
import { cleanupChildProcesses, createRunner } from '../../utils/runner';

afterAll(() => {
cleanupChildProcesses();
});

test('nodeContextIntegration sets context attributes on segment spans', async () => {
await createRunner(__dirname, 'scenario.ts')
.expect({
span: container => {
const segmentSpan = container.items.find(s => !!s.is_segment);
expect(segmentSpan).toBeDefined();

const attrs = segmentSpan!.attributes!;

expect(attrs['app.start_time']).toEqual({ type: 'string', value: expect.any(String) });
expect(attrs['device.processor_count']).toEqual({ type: 'integer', value: expect.any(Number) });
expect(attrs['process.runtime.engine.name']).toEqual({ type: 'string', value: 'v8' });
expect(attrs['process.runtime.engine.version']).toEqual({ type: 'string', value: expect.any(String) });
},
})
.start()
.completed();
});
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ test('sends a streamed span envelope with correct spans for a manually started s
[SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_ID]: { type: 'string', value: segmentSpanId },
[SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_NAME]: { type: 'string', value: 'test-span' },
[SEMANTIC_ATTRIBUTE_SENTRY_RELEASE]: { type: 'string', value: '1.0.0' },
'process.runtime.engine.name': { type: 'string', value: 'v8' },
'process.runtime.engine.version': { type: 'string', value: expect.any(String) },
'app.start_time': { type: 'string', value: expect.any(String) },
'device.processor_count': { type: 'integer', value: expect.any(Number) },
},
name: 'test-span',
is_segment: true,
Expand Down
48 changes: 45 additions & 3 deletions packages/node-core/src/integrations/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
IntegrationFn,
OsContext,
} from '@sentry/core';
import { defineIntegration } from '@sentry/core';
import { defineIntegration, safeSetSpanJSONAttributes } from '@sentry/core';

export const readFileAsync = promisify(readFile);
export const readDirAsync = promisify(readdir);
Expand Down Expand Up @@ -53,6 +53,45 @@ const _nodeContextIntegration = ((options: ContextOptions = {}) => {
...options,
};

const cachedSpanAttributes: Record<string, unknown> = {
'process.runtime.engine.name': 'v8',
'process.runtime.engine.version': process.versions.v8,
};

if (_options.app) {
// oxlint-disable-next-line sdk/no-unsafe-random-apis
cachedSpanAttributes['app.start_time'] = new Date(Date.now() - process.uptime() * 1000).toISOString();
}

if (_options.device) {
const deviceOpt = _options.device;
// Convention uses 'device.archs' (string[]), but array attributes are not yet serialized.
cachedSpanAttributes['device.archs'] = [os.arch()];
if (deviceOpt === true || (typeof deviceOpt === 'object' && deviceOpt.cpu)) {
const cpuInfo = os.cpus() as os.CpuInfo[] | undefined;
if (cpuInfo?.[0]) {
cachedSpanAttributes['device.processor_count'] = cpuInfo.length;
}
}
}

const osContextPromise = _options.os ? getOsContext() : undefined;

if (osContextPromise) {
osContextPromise
.then(osContext => {
if (osContext.name) {
cachedSpanAttributes['os.name'] = osContext.name;
}
if (osContext.version) {
cachedSpanAttributes['os.version'] = osContext.version;
}
})
.catch(() => {
// Ignore - os attributes will be undefined
});
}

/** Add contexts to the event. Caches the context so we only look it up once. */
async function addContext(event: Event): Promise<Event> {
if (cachedContext === undefined) {
Expand All @@ -78,8 +117,8 @@ const _nodeContextIntegration = ((options: ContextOptions = {}) => {
async function _getContexts(): Promise<Contexts> {
const contexts: Contexts = {};

if (_options.os) {
contexts.os = await getOsContext();
if (osContextPromise) {
contexts.os = await osContextPromise;
}

if (_options.app) {
Expand Down Expand Up @@ -110,6 +149,9 @@ const _nodeContextIntegration = ((options: ContextOptions = {}) => {
processEvent(event) {
return addContext(event);
},
processSegmentSpan(span) {
safeSetSpanJSONAttributes(span, cachedSpanAttributes);
},
};
}) satisfies IntegrationFn;

Expand Down
76 changes: 75 additions & 1 deletion packages/node-core/test/integrations/context.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as os from 'node:os';
import type { StreamedSpanJSON } from '@sentry/core';
import { afterAll, describe, expect, it, vi } from 'vitest';
import { getAppContext, getDeviceContext } from '../../src/integrations/context';
import { getAppContext, getDeviceContext, nodeContextIntegration } from '../../src/integrations/context';
import { conditionalTest } from '../helpers/conditional';

vi.mock('node:os', async () => {
Expand Down Expand Up @@ -53,4 +54,77 @@ describe('Context', () => {
expect(deviceCtx.boot_time).toBeUndefined();
});
});

describe('processSegmentSpan', () => {
it('sets context attributes on segment span', () => {
const integration = nodeContextIntegration();

const span: StreamedSpanJSON = {
trace_id: 'abc123',
span_id: 'def456',
name: 'test-span',
start_timestamp: Date.now(),
end_timestamp: Date.now(),
status: 'ok',
is_segment: true,
attributes: {},
};

integration.processSegmentSpan!(span, {} as any);

expect(span.attributes).toMatchObject({
'app.start_time': expect.any(String),
'device.archs': [os.arch()],
'device.processor_count': expect.any(Number),
'process.runtime.engine.name': 'v8',
'process.runtime.engine.version': process.versions.v8,
});
});

it('does not overwrite existing attributes', () => {
const integration = nodeContextIntegration();

const span: StreamedSpanJSON = {
trace_id: 'abc123',
span_id: 'def456',
name: 'test-span',
start_timestamp: Date.now(),
end_timestamp: Date.now(),
status: 'ok',
is_segment: true,
attributes: {
'process.runtime.engine.name': 'custom-engine',
},
};

integration.processSegmentSpan!(span, {} as any);

expect(span.attributes!['process.runtime.engine.name']).toBe('custom-engine');
});

it('respects disabled options', () => {
const integration = nodeContextIntegration({ app: false, device: false, os: false });

const span: StreamedSpanJSON = {
trace_id: 'abc123',
span_id: 'def456',
name: 'test-span',
start_timestamp: Date.now(),
end_timestamp: Date.now(),
status: 'ok',
is_segment: true,
attributes: {},
};

integration.processSegmentSpan!(span, {} as any);

expect(span.attributes).toMatchObject({
'process.runtime.engine.name': 'v8',
'process.runtime.engine.version': process.versions.v8,
});
expect(span.attributes!['app.start_time']).toBeUndefined();
expect(span.attributes!['device.archs']).toBeUndefined();
expect(span.attributes!['device.processor_count']).toBeUndefined();
});
});
});
Loading