Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node): Only set DeviceContext.boot_time if os.uptime() is valid #5859

Merged
merged 3 commits into from Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/node/src/integrations/context.ts
Expand Up @@ -189,10 +189,18 @@ 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();
device.boot_time = uptime !== undefined ? new Date(Date.now() - uptime * 1000).toISOString() : undefined;
Lms24 marked this conversation as resolved.
Show resolved Hide resolved

device.arch = os.arch();

if (deviceOpt === true || deviceOpt.memory) {
Expand Down
21 changes: 21 additions & 0 deletions packages/node/test/integrations/context.test.ts
@@ -0,0 +1,21 @@
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();
});
});
});