Skip to content

Commit

Permalink
fix: pyroscope endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
utsabc committed Sep 14, 2023
1 parent e98ea84 commit 55cc5aa
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;

Check warning on line 31 in src/controllers/misc.ts

View check run for this annotation

Codecov / codecov/patch

src/controllers/misc.ts#L29-L31

Added lines #L29 - L31 were not covered by tests
// if seconds is not null and is not array then parseInt
if (seconds && !Array.isArray(seconds)) {
secondsData = parseInt(seconds, 10);

Check warning on line 34 in src/controllers/misc.ts

View check run for this annotation

Codecov / codecov/patch

src/controllers/misc.ts#L34

Added line #L34 was not covered by tests
}
ctx.body = await MiscService.getCPUProfile(secondsData);
ctx.status = 200;
return ctx;

Check warning on line 38 in src/controllers/misc.ts

View check run for this annotation

Codecov / codecov/patch

src/controllers/misc.ts#L36-L38

Added lines #L36 - L38 were not covered by tests
}

public static async getHeapProfile(ctx: Context) {
ctx.body = await MiscService.getHeapProfile();
ctx.status = 200;
return ctx;

Check warning on line 44 in src/controllers/misc.ts

View check run for this annotation

Codecov / codecov/patch

src/controllers/misc.ts#L41-L44

Added lines #L41 - L44 were not covered by tests
}
}
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({

Check warning on line 5 in src/middleware.js

View check run for this annotation

Codecov / codecov/patch

src/middleware.js#L4-L5

Added lines #L4 - L5 were not covered by tests
appName: 'rudder-transformer',
});
Pyroscope.startHeapCollecting();

Check warning on line 8 in src/middleware.js

View check run for this annotation

Codecov / codecov/patch

src/middleware.js#L8

Added line #L8 was not covered by tests
}

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);

Check warning on line 12 in src/middleware.js

View check run for this annotation

Codecov / codecov/patch

src/middleware.js#L11-L12

Added lines #L11 - L12 were not covered by tests
}

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

Check warning on line 16 in src/middleware.js

View check run for this annotation

Codecov / codecov/patch

src/middleware.js#L15-L16

Added lines #L15 - L16 were not covered by tests
}

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);

Check warning on line 68 in src/services/misc.ts

View check run for this annotation

Codecov / codecov/patch

src/services/misc.ts#L67-L68

Added lines #L67 - L68 were not covered by tests
}

public static async getHeapProfile() {
return getHeapProfile()

Check warning on line 72 in src/services/misc.ts

View check run for this annotation

Codecov / codecov/patch

src/services/misc.ts#L71-L72

Added lines #L71 - L72 were not covered by tests
}
}

0 comments on commit 55cc5aa

Please sign in to comment.