Skip to content

Commit

Permalink
chore: fix sonar and lint issues (#2607)
Browse files Browse the repository at this point in the history
  • Loading branch information
krishna2020 authored and Sanjay-Veernala committed Sep 19, 2023
1 parent bc01bdf commit 9122dd2
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions src/middleware.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
const stats = require('./util/stats');
const Pyroscope = require('@pyroscope/nodejs');
const stats = require('./util/stats');
const logger = require('./logger');

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

function pyroscopeMiddleware(ctx, next) {
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());
}
next();
};
}

async function handlerCpu(ctx) {
try {
const p = await Pyroscope.collectCpu(Number(ctx.query.seconds));
ctx.body = p;
ctx.status = 200;
}
catch (e) {
console.log(e);
} catch (e) {
logger.error(e);
ctx.status = 500;
}
}
Expand All @@ -35,15 +22,27 @@ async function handlerHeap(ctx) {
const p = await Pyroscope.collectHeap();
ctx.body = p;
ctx.status = 200;
}
catch (e) {
console.log(e);
} catch (e) {
logger.error(e);
ctx.status = 500;
}
}

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 addPyroscopeMiddleware(app) {
app.use(pyroscopeMiddleware())
app.use(pyroscopeMiddleware());
}

function durationMiddleware() {
Expand Down

0 comments on commit 9122dd2

Please sign in to comment.