Skip to content

Commit

Permalink
fix: pyroscope endpoints (#2613)
Browse files Browse the repository at this point in the history
  • Loading branch information
utsabc authored Sep 14, 2023
1 parent f44a951 commit 7d268f0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 43 deletions.
18 changes: 18 additions & 0 deletions src/controllers/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,22 @@ export default class MiscController {
ctx.status = 200;
return ctx;
}

public static async getCPUProfile(ctx: Context) {
const { seconds } = ctx.query;
let secondsData = 10;
// if seconds is not null and is not array then parseInt
if (seconds && !Array.isArray(seconds)) {
secondsData = parseInt(seconds, 10);
}
ctx.body = await MiscService.getCPUProfile(secondsData);
ctx.status = 200;
return ctx;
}

public static async getHeapProfile(ctx: Context) {
ctx.body = await MiscService.getHeapProfile();
ctx.status = 200;
return ctx;
}
}
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ import cluster from './util/cluster';
import { router } from './legacy/router';
import { testRouter } from './testRouter';
import { metricsRouter } from './routes/metricsRouter';
import { addStatMiddleware, addRequestSizeMiddleware, addPyroscopeMiddleware } from './middleware';
import { addStatMiddleware, addRequestSizeMiddleware, initPyroscope } from './middleware';
import { logProcessInfo } from './util/utils';
import { applicationRoutes, addSwaggerRoutes } from './routes';
import { RedisDB } from './util/redis/redisConnector';

dotenv.config();
const clusterEnabled = process.env.CLUSTER_ENABLED !== 'false';
const useUpdatedRoutes = process.env.ENABLE_NEW_ROUTES !== 'false';
const port = parseInt(process.env.PORT || '9090', 10);
const port = parseInt(process.env.PORT ?? '9090', 10);
const metricsPort = parseInt(process.env.METRICS_PORT || '9091', 10);

initPyroscope();

const app = new Koa();
addStatMiddleware(app);

Expand All @@ -30,7 +32,6 @@ app.use(
jsonLimit: '200mb',
}),
);
addPyroscopeMiddleware(app);
addRequestSizeMiddleware(app);
addSwaggerRoutes(app);

Expand Down
51 changes: 12 additions & 39 deletions src/middleware.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,19 @@
const Pyroscope = require('@pyroscope/nodejs');
const stats = require('./util/stats');
const logger = require('./logger');

Pyroscope.init({
appName: 'rudder-transformer',
});

async function handlerCpu(ctx) {
try {
const p = await Pyroscope.collectCpu(Number(ctx.query.seconds));
ctx.body = p;
ctx.status = 200;
} catch (e) {
logger.error(e);
ctx.status = 500;
}
}

async function handlerHeap(ctx) {
try {
const p = await Pyroscope.collectHeap();
ctx.body = p;
ctx.status = 200;
} catch (e) {
logger.error(e);
ctx.status = 500;
}
function initPyroscope() {
Pyroscope.init({
appName: 'rudder-transformer',
});
Pyroscope.startHeapCollecting();
}

function pyroscopeMiddleware() {
Pyroscope.startHeapCollecting();
return (ctx, next) => {
if (ctx.method === 'GET' && ctx.path === '/debug/pprof/profile') {
return handlerCpu(ctx).then(() => next());
}
if (ctx.method === 'GET' && ctx.path === '/debug/pprof/heap') {
return handlerHeap(ctx).then(() => next());
}
return next();
};
function getCPUProfile(seconds) {
return Pyroscope.collectCpu(seconds);
}

function addPyroscopeMiddleware(app) {
app.use(pyroscopeMiddleware());
function getHeapProfile() {
return Pyroscope.collectHeap();
}

function durationMiddleware() {
Expand Down Expand Up @@ -90,5 +61,7 @@ function addRequestSizeMiddleware(app) {
module.exports = {
addStatMiddleware,
addRequestSizeMiddleware,
addPyroscopeMiddleware,
getHeapProfile,
getCPUProfile,
initPyroscope,
};
2 changes: 2 additions & 0 deletions src/routes/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ router.get('/transformerBuildVersion', MiscController.buildVersion); // depricia
router.get('/buildVersion', MiscController.buildVersion);
router.get('/version', MiscController.version);
router.get('/features', MiscController.features);
router.get('/debug/pprof/profile', MiscController.getCPUProfile);
router.get('/debug/pprof/heap', MiscController.getHeapProfile);

export const miscRoutes = router.routes();
11 changes: 10 additions & 1 deletion src/services/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import { Context } from 'koa';
import { DestHandlerMap } from '../constants/destinationCanonicalNames';
import { Metadata } from '../types';
import { getCPUProfile, getHeapProfile, } from '../middleware';

export default class MiscService {
public static getDestHandler(dest: string, version: string) {
Expand Down Expand Up @@ -32,7 +33,7 @@ export default class MiscService {

public static getMetaTags(metadata: Metadata) {
if (!metadata) {
return {}
return {};
}
return {
sourceType: metadata.sourceType,
Expand Down Expand Up @@ -62,4 +63,12 @@ export default class MiscService {
const obj = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../features.json'), 'utf8'));
return JSON.stringify(obj);
}

public static async getCPUProfile(seconds: number) {
return getCPUProfile(seconds);
}

public static async getHeapProfile() {
return getHeapProfile()
}
}

0 comments on commit 7d268f0

Please sign in to comment.