Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
feat(build): run precompile
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasBa committed Nov 4, 2022
1 parent 1242caa commit 537d743
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ jobs:
with:
name: ${{ matrix.os }}.${{ matrix.node-version }}.${{ matrix.host }}.${{ github.sha }}
path: |
${{ github.workspace }}/binaries/*.node
${{ github.workspace }}/binaries
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

coverage/
lib/
binaries/

node_modules/
memory_db
Expand Down
Binary file removed binaries/sentry_cpu_profiler-v93-darwin-arm64.node
Binary file not shown.
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
"README.md",
"package.json",
"package-lock.json",
"binaries"
"binaries",
"scripts/binaries.js",
"scripts/check-build.js",
"scripts/copu-target.js"
],
"scripts": {
"postinstall": "node scripts/check-build.js",
"clean": "rm -rf ./lib && rm -rf build",
"lint": "eslint ./src --ext .ts",
"build": "npm run build:bindings && npm run build:lib",
Expand Down Expand Up @@ -62,6 +66,7 @@
"@types/express": "^4.17.14",
"@types/jest": "^29.0.0",
"@types/node": "^18.0.2",
"@types/node-abi": "^3.0.0",
"@typescript-eslint/eslint-plugin": "^5.36.2",
"@typescript-eslint/parser": "^5.36.2",
"autocannon": "^7.9.0",
Expand Down
14 changes: 14 additions & 0 deletions scripts/binaries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-env node */
const os = require('os');
const path = require('path');
const abi = require('node-abi');

function getModuleName() {
return `sentry_cpu_profiler-v${abi.getAbi(process.versions.node, 'node')}-${os.platform()}-${os.arch()}.node`;
}

const source = path.join(__dirname, '..', 'build', 'Release', 'sentry_cpu_profiler.node');
const target = path.join(__dirname, '..', 'binaries', getModuleName());

module.exports.target = target;
module.exports.source = source;
44 changes: 44 additions & 0 deletions scripts/check-build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* eslint-env node */
const cp = require('child_process');
const os = require('os');

const { target } = require('./binaries');

function recompileFromSource() {
try {
console.log('@sentry/profiling-node: Precompiled binary not found, compiling from source...');
cp.execSync(`npm run build:configure --arch=${os.arch()}`);
cp.execSync(`npm run build:bindings`);
cp.execSync('node scripts/copy-target.js');
return true;
} catch (e) {
console.error(
'@sentry/profiling-node: Failed to build from source, please report this a bug at https://github.com/getsentry/profiling-node/issues/new?assignees=&labels=Type%3A+Bug&template=bug.yml'
);
return false;
}
}

try {
require(target);
console.log('@sentry/profiling-node: Precompiled binary found, skipping build from source.');
} catch (e) {
// Check for node version missmatch
if (/was compiled against a different Node.js/.test(e.message)) {
const success = recompileFromSource();
if (success) {
process.exit(0);
}
}
// Not sure if this could even happen, but just in case it somehow does,
// we can provide a better experience than just crashing with cannot find module message.
if (/Cannot find module/.test(e.message)) {
const success = recompileFromSource();
if (success) {
process.exit(0);
}
}

// re-throw so we dont end up swallowing errors
throw e;
}
12 changes: 9 additions & 3 deletions src/cpu_profiler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// @ts-expect-error this screams because it cannot resolve the module?
import profiler from './../build/Release/sentry_cpu_profiler.node';
import os from 'os';
import path from 'path';
import abi from 'node-abi';
import { threadId } from 'worker_threads';

export function importCppBindingsModule(): PrivateV8CpuProfilerBindings {
const name = `sentry_cpu_profiler-v${abi.getAbi(process.versions.node, 'node')}-${os.platform()}-${os.arch()}.node`;
return require(path.join(__dirname, '..', 'binaries', name));
}

interface Sample {
stack_id: number;
thread_id: string;
Expand Down Expand Up @@ -44,7 +50,7 @@ interface V8CpuProfilerBindings {
stopProfiling(name: string): RawThreadCpuProfile | null;
}

const privateBindings: PrivateV8CpuProfilerBindings = profiler;
const privateBindings: PrivateV8CpuProfilerBindings = importCppBindingsModule();
const CpuProfilerBindings: V8CpuProfilerBindings = {
startProfiling(name: string) {
return privateBindings.startProfiling(name);
Expand Down
4 changes: 2 additions & 2 deletions src/hubextensions.hub.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Sentry from '@sentry/node';
import '@sentry/tracing'; // this has a addExtensionMethods side effect
import { ProfilingIntegration } from './index'; // this has a addExtensionMethods side effect
import { importCppBindingsModule } from './cpu_profiler';

Sentry.init({
dsn: 'https://[email protected]/6625302',
Expand All @@ -11,8 +12,7 @@ Sentry.init({
integrations: [new ProfilingIntegration()]
});

// @ts-expect-error file extension errors
import profiler from './../build/Release/sentry_cpu_profiler';
const profiler = importCppBindingsModule();

describe('hubextensions', () => {
beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/hubextensions.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { BaseTransportOptions, ClientOptions, Hub, Transaction, TransactionMetadata } from '@sentry/types';

import { __PRIVATE__wrapStartTransactionWithProfiling } from './hubextensions';
import { importCppBindingsModule } from './cpu_profiler';

// @ts-expect-error file extension errors
import profiler from './../build/Release/sentry_cpu_profiler';
const profiler = importCppBindingsModule();

function makeTransactionMock(): Transaction {
return {
Expand Down

0 comments on commit 537d743

Please sign in to comment.