Skip to content
Merged
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
14 changes: 12 additions & 2 deletions packages/node/src/integrations/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,20 @@ function getAppContext(): AppContext {
return { app_start_time, app_memory };
}

function getDeviceContext(deviceOpt: DeviceContextOptions | true): DeviceContext {
/**
* Gets device information from os
*/
export function getDeviceContext(deviceOpt: DeviceContextOptions | true): DeviceContext {
const device: DeviceContext = {};

device.boot_time = new Date(Date.now() - os.uptime() * 1000).toISOString();
// os.uptime or its return value seem to be undefined in certain environments (e.g. Azure functions).
// Hence, we only set boot time, if we get a valid uptime value.
// @see https://github.com/getsentry/sentry-javascript/issues/5856
const uptime = os.uptime && os.uptime();
if (typeof uptime === 'number') {
device.boot_time = new Date(Date.now() - uptime * 1000).toISOString();
}

device.arch = os.arch();

if (deviceOpt === true || deviceOpt.memory) {
Expand Down
22 changes: 22 additions & 0 deletions packages/node/test/integrations/context.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as os from 'os';

import { getDeviceContext } from '../../src/integrations/context';

describe('Context', () => {
describe('getDeviceContext', () => {
afterAll(() => {
jest.clearAllMocks();
});

it('returns boot time if os.uptime is defined and returns a valid uptime', () => {
const deviceCtx = getDeviceContext({});
expect(deviceCtx.boot_time).toEqual(expect.any(String));
});

it('returns no boot time if os.uptime() returns undefined', () => {
jest.spyOn(os, 'uptime').mockReturnValue(undefined as unknown as number);
const deviceCtx = getDeviceContext({});
expect(deviceCtx.boot_time).toBeUndefined();
});
});
});