Skip to content

Commit

Permalink
Merge branch 'main' of github.com:grafana/pyroscope-nodejs into remov…
Browse files Browse the repository at this point in the history
…e-package-lock.json
  • Loading branch information
bryanhuhta committed Sep 10, 2024
2 parents a7449db + ebe94df commit 852d1be
Show file tree
Hide file tree
Showing 30 changed files with 694 additions and 691 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ module.exports = {
'prettier/prettier': 'error',
},
plugins: ['@typescript-eslint', 'prettier'],
}
};
4 changes: 2 additions & 2 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
semi: false,
semi: true,
singleQuote: true,
trailingComma: 'es5',
arrowParens: 'always',
printWidth: 80,
}
};
2 changes: 1 addition & 1 deletion CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @grafana/pyroscope-nodejs
* @grafana/pyroscope-nodejs @grafana/pyroscope-team
6 changes: 3 additions & 3 deletions config/fileTransformer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// source: https://jestjs.io/docs/code-transformation#examples

import 'path'
import 'path';

module.exports = {
process(src, filename) {
return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';'
return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';';
},
}
};
4 changes: 2 additions & 2 deletions config/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'path'
import 'path';

module.exports = {
mode: 'production',
Expand Down Expand Up @@ -27,4 +27,4 @@ module.exports = {
resolve: {
extensions: ['.ts', '.js', '.tsx', '.jsx'],
},
}
};
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ module.exports = {
},
},
extensionsToTreatAsEsm: ['.ts'],
}
};
20 changes: 10 additions & 10 deletions src/environment.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export interface Environment {
adhocServerAddress: string | undefined
appName: string | undefined
authToken: string | undefined
flushIntervalMs: number | undefined
heapSamplingIntervalBytes: number | undefined
heapStackDepth: number | undefined
serverAddress: string | undefined
wallSamplingDurationMs: number | undefined
wallSamplingIntervalMicros: number | undefined
wallCollectCpuTime: boolean | undefined
adhocServerAddress: string | undefined;
appName: string | undefined;
authToken: string | undefined;
flushIntervalMs: number | undefined;
heapSamplingIntervalBytes: number | undefined;
heapStackDepth: number | undefined;
serverAddress: string | undefined;
wallSamplingDurationMs: number | undefined;
wallSamplingIntervalMicros: number | undefined;
wallCollectCpuTime: boolean | undefined;
}
78 changes: 39 additions & 39 deletions src/express/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
import debug from 'debug'
import { NextFunction, Request, Response, RequestHandler } from 'express'
import { Profile } from 'pprof-format'
import { Profiler } from '../profilers/profiler'
import { PyroscopeProfiler } from '../profilers/pyroscope-profiler'
import { WallProfilerStartArgs } from '../profilers/wall-profiler'
import { getProfiler } from '../utils/pyroscope-profiler'
import { encode } from '@datadog/pprof'
import { HeapProfilerStartArgs } from '../profilers/heap-profiler'

const log = debug('pyroscope')
import debug from 'debug';
import { NextFunction, Request, Response, RequestHandler } from 'express';
import { Profile } from 'pprof-format';
import { Profiler } from '../profilers/profiler';
import { PyroscopeProfiler } from '../profilers/pyroscope-profiler';
import { WallProfilerStartArgs } from '../profilers/wall-profiler';
import { getProfiler } from '../utils/pyroscope-profiler';
import { encode } from '@datadog/pprof';
import { HeapProfilerStartArgs } from '../profilers/heap-profiler';

const log = debug('pyroscope');

async function collectProfile<TStartArgs>(
profiler: Profiler<TStartArgs>
): Promise<Buffer> {
const profile: Profile = profiler.profile().profile
const profile: Profile = profiler.profile().profile;

profiler.stop()
profiler.stop();

return encode(profile)
return encode(profile);
}

async function collectProfileAfterMs<TStartArgs>(
profiler: Profiler<TStartArgs>,
args: TStartArgs,
delayMs: number
): Promise<Buffer> {
profiler.start(args)
profiler.start(args);

if (delayMs === 0) {
return collectProfile(profiler)
return collectProfile(profiler);
}

return new Promise(
(resolve: (buffer: Buffer | PromiseLike<Buffer>) => void) => {
setTimeout(() => {
resolve(collectProfile(profiler))
}, delayMs)
resolve(collectProfile(profiler));
}, delayMs);
}
)
);
}

function collectHeap(): Promise<Buffer> {
const profiler: PyroscopeProfiler = getProfiler()
const profiler: PyroscopeProfiler = getProfiler();

const heapProfilerArgs: HeapProfilerStartArgs =
profiler.heapProfiler.startArgs
profiler.heapProfiler.startArgs;
const heapProfiler: Profiler<HeapProfilerStartArgs> =
profiler.heapProfiler.profiler
profiler.heapProfiler.profiler;

return collectProfileAfterMs(heapProfiler, heapProfilerArgs, 0)
return collectProfileAfterMs(heapProfiler, heapProfilerArgs, 0);
}

function collectWall(ms: number): Promise<Buffer> {
const profiler: PyroscopeProfiler = getProfiler()
const profiler: PyroscopeProfiler = getProfiler();

const wallProfilerArgs: WallProfilerStartArgs =
profiler.wallProfiler.startArgs
profiler.wallProfiler.startArgs;
const wallProfiler: Profiler<WallProfilerStartArgs> =
profiler.wallProfiler.profiler
profiler.wallProfiler.profiler;

return collectProfileAfterMs(wallProfiler, wallProfilerArgs, ms)
return collectProfileAfterMs(wallProfiler, wallProfilerArgs, ms);
}

function profileExpressHandler(
Expand All @@ -71,25 +71,25 @@ function profileExpressHandler(
res: Response
// next: NextFunction
): Promise<void> => {
log(`Fetching ${profileKind} Profile`)
log(`Fetching ${profileKind} Profile`);
try {
const profileBuffer = await useCaseHandler(req)
res.status(200).send(profileBuffer)
const profileBuffer = await useCaseHandler(req);
res.status(200).send(profileBuffer);
} catch (error: unknown) {
log(`Error collecting ${profileKind}`, error)
res.sendStatus(500)
log(`Error collecting ${profileKind}`, error);
res.sendStatus(500);
}
}
};
}

const heapHandler: RequestHandler = profileExpressHandler('Heap', () =>
collectHeap()
)
);

const wallHandler: RequestHandler = profileExpressHandler(
'Wall',
(req: Request) => collectWall(1000 * Number(req.query.seconds))
)
);

export default function expressMiddleware(): (
req: Request,
Expand All @@ -99,12 +99,12 @@ export default function expressMiddleware(): (
return (req: Request, res: Response, next: NextFunction) => {
if (req.method === 'GET') {
if (req.path === '/debug/pprof/heap') {
return heapHandler(req, res, next)
return heapHandler(req, res, next);
}
if (req.path === '/debug/pprof/profile') {
return wallHandler(req, res, next)
return wallHandler(req, res, next);
}
}
next()
}
next();
};
}
64 changes: 32 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,94 +1,94 @@
import 'regenerator-runtime/runtime'
import 'regenerator-runtime/runtime';

import expressMiddleware from './express/middleware'
import { PyroscopeProfiler } from './profilers/pyroscope-profiler'
import expressMiddleware from './express/middleware';
import { PyroscopeProfiler } from './profilers/pyroscope-profiler';
import {
PyroscopeConfig,
PyroscopeHeapConfig,
PyroscopeWallConfig,
} from './pyroscope-config'
import { checkPyroscopeConfig } from './utils/check-pyroscope-config'
import { getProfiler, setProfiler } from './utils/pyroscope-profiler'
import { processConfig } from './utils/process-config'
import { getEnv } from './utils/get-env'
import { setLogger as datadogSetLogger } from '@datadog/pprof'
import { setLogger as ourSetLogger, Logger } from './logger'
import { SourceMapper } from './sourcemapper'
} from './pyroscope-config';
import { checkPyroscopeConfig } from './utils/check-pyroscope-config';
import { getProfiler, setProfiler } from './utils/pyroscope-profiler';
import { processConfig } from './utils/process-config';
import { getEnv } from './utils/get-env';
import { setLogger as datadogSetLogger } from '@datadog/pprof';
import { setLogger as ourSetLogger, Logger } from './logger';
import { SourceMapper } from './sourcemapper';

export function init(config: PyroscopeConfig = {}): void {
checkPyroscopeConfig(config)
checkPyroscopeConfig(config);

const processedConfig: PyroscopeConfig = processConfig(config, getEnv())
const processedConfig: PyroscopeConfig = processConfig(config, getEnv());

setProfiler(new PyroscopeProfiler(processedConfig))
setProfiler(new PyroscopeProfiler(processedConfig));
}

// deprecated: please use getLabels
function getWallLabels(): Record<string, number | string> {
return getLabels()
return getLabels();
}

// deprecated: please use setLabels
function setWallLabels(labels: Record<string, number | string>): void {
return setLabels(labels)
return setLabels(labels);
}

function getLabels(): Record<string, number | string> {
return getProfiler().wallProfiler.profiler.getLabels()
return getProfiler().wallProfiler.profiler.getLabels();
}

function setLabels(labels: Record<string, number | string>): void {
getProfiler().wallProfiler.profiler.setLabels(labels)
getProfiler().wallProfiler.profiler.setLabels(labels);
}

export function wrapWithLabels(
lbls: Record<string, string | number>,
fn: () => void,
...args: unknown[]
): void {
getProfiler().wallProfiler.profiler.wrapWithLabels(lbls, fn, ...args)
getProfiler().wallProfiler.profiler.wrapWithLabels(lbls, fn, ...args);
}

function startWallProfiling(): void {
getProfiler().wallProfiler.start()
getProfiler().wallProfiler.start();
}

// here for backwards compatibility
function startCpuProfiling(): void {
getProfiler().wallProfiler.start()
getProfiler().wallProfiler.start();
}

async function stopWallProfiling(): Promise<void> {
await getProfiler().wallProfiler.stop()
await getProfiler().wallProfiler.stop();
}

// here for backwards compatibility
async function stopCpuProfiling(): Promise<void> {
await getProfiler().wallProfiler.stop()
await getProfiler().wallProfiler.stop();
}

function startHeapProfiling(): void {
getProfiler().heapProfiler.start()
getProfiler().heapProfiler.start();
}

async function stopHeapProfiling(): Promise<void> {
await getProfiler().heapProfiler.stop()
await getProfiler().heapProfiler.stop();
}

export function start(): void {
startWallProfiling()
startHeapProfiling()
startWallProfiling();
startHeapProfiling();
}

export async function stop(): Promise<void> {
await Promise.all([stopWallProfiling(), stopHeapProfiling()])
await Promise.all([stopWallProfiling(), stopHeapProfiling()]);
}

export { PyroscopeConfig, PyroscopeHeapConfig, PyroscopeWallConfig }
export { PyroscopeConfig, PyroscopeHeapConfig, PyroscopeWallConfig };

function setLogger(logger: Logger): void {
datadogSetLogger(logger)
ourSetLogger(logger)
datadogSetLogger(logger);
ourSetLogger(logger);
}

export default {
Expand All @@ -109,4 +109,4 @@ export default {
stopWallProfiling,
stopCpuProfiling,
setLogger,
}
};
Loading

0 comments on commit 852d1be

Please sign in to comment.