Skip to content

Commit

Permalink
chore: resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
aashishmalik committed Sep 14, 2023
2 parents 10256b1 + 7d268f0 commit dd207d0
Show file tree
Hide file tree
Showing 22 changed files with 154 additions and 147 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
65 changes: 21 additions & 44 deletions src/middleware.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,19 @@
import { init, collectCpu, collectHeap, startHeapCollecting } from '@pyroscope/nodejs';
import { timing, histogram } from './util/stats';
import { error } from './logger';
const Pyroscope = require('@pyroscope/nodejs');
const stats = require('./util/stats');

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

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

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

function pyroscopeMiddleware() {
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 All @@ -56,7 +27,7 @@ function durationMiddleware() {
code: ctx.status,
route: ctx.request.url,
};
timing('http_request_duration', startTime, labels);
stats.timing('http_request_duration', startTime, labels);
};
}

Expand All @@ -71,11 +42,11 @@ function requestSizeMiddleware() {
};

const inputLength = ctx.request?.body ? Buffer.byteLength(JSON.stringify(ctx.request.body)) : 0;
histogram('http_request_size', inputLength, labels);
stats.histogram('http_request_size', inputLength, labels);
const outputLength = ctx.response?.body
? Buffer.byteLength(JSON.stringify(ctx.response.body))
: 0;
histogram('http_response_size', outputLength, labels);
stats.histogram('http_response_size', outputLength, labels);
};
}

Expand All @@ -87,4 +58,10 @@ function addRequestSizeMiddleware(app) {
app.use(requestSizeMiddleware());
}

export { addStatMiddleware, addRequestSizeMiddleware, addPyroscopeMiddleware };
module.exports = {
addStatMiddleware,
addRequestSizeMiddleware,
getHeapProfile,
getCPUProfile,
initPyroscope,
};
4 changes: 3 additions & 1 deletion src/routes/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ 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);

const miscRoutes = router.routes();
export default miscRoutes;
export default miscRoutes;
9 changes: 9 additions & 0 deletions src/services/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,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 @@ -64,4 +65,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();

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

View check run for this annotation

Codecov / codecov/patch

src/services/misc.ts#L74

Added line #L74 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { getMappingConfig } = require('../../util');

const BASE_ENDPOINT = 'https://googleads.googleapis.com/v13/customers';
const BASE_ENDPOINT = 'https://googleads.googleapis.com/v14/customers';

const CONFIG_CATEGORIES = {
TRACK_CONFIG: { type: 'track', name: 'trackConfig' },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { getMappingConfig } = require('../../util');

const API_VERSION = 'v13';
const API_VERSION = 'v14';

const BASE_ENDPOINT = `https://googleads.googleapis.com/${API_VERSION}/customers/:customerId`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ describe('getExisitingUserIdentifier util tests', () => {
describe('getClickConversionPayloadAndEndpoint util tests', () => {
it('getClickConversionPayloadAndEndpoint flow check when default field identifier is present', () => {
let expectedOutput = {
endpoint: 'https://googleads.googleapis.com/v13/customers/9625812972:uploadClickConversions',
endpoint: 'https://googleads.googleapis.com/v14/customers/9625812972:uploadClickConversions',
payload: {
conversions: [
{
Expand All @@ -187,7 +187,7 @@ describe('getClickConversionPayloadAndEndpoint util tests', () => {
delete fittingPayload.traits.email;
delete fittingPayload.properties.email;
let expectedOutput = {
endpoint: 'https://googleads.googleapis.com/v13/customers/9625812972:uploadClickConversions',
endpoint: 'https://googleads.googleapis.com/v14/customers/9625812972:uploadClickConversions',
payload: {
conversions: [
{
Expand Down Expand Up @@ -215,7 +215,7 @@ describe('getClickConversionPayloadAndEndpoint util tests', () => {
delete fittingPayload.traits.phone;
delete fittingPayload.properties.email;
let expectedOutput = {
endpoint: 'https://googleads.googleapis.com/v13/customers/9625812972:uploadClickConversions',
endpoint: 'https://googleads.googleapis.com/v14/customers/9625812972:uploadClickConversions',
payload: {
conversions: [
{
Expand Down Expand Up @@ -251,7 +251,7 @@ describe('getClickConversionPayloadAndEndpoint util tests', () => {
},
];
let expectedOutput = {
endpoint: 'https://googleads.googleapis.com/v13/customers/9625812972:uploadClickConversions',
endpoint: 'https://googleads.googleapis.com/v14/customers/9625812972:uploadClickConversions',
payload: {
conversions: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { getMappingConfig } = require('../../util');

const BASE_ENDPOINT = 'https://googleads.googleapis.com/v13/customers';
const BASE_ENDPOINT = 'https://googleads.googleapis.com/v14/customers';
const CONFIG_CATEGORIES = {
AUDIENCE_LIST: { type: 'audienceList', name: 'offlineDataJobs' },
ADDRESSINFO: { type: 'addressInfo', name: 'addressInfo' },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"https://googleads.googleapis.com/v13/customers/11122233331/offlineUserDataJobs:create": {
"https://googleads.googleapis.com/v14/customers/11122233331/offlineUserDataJobs:create": {
"data": {
"resourceName": "customers/111-222-3333/offlineUserDataJobs/OFFLINE_USER_DATA_JOB_ID_FOR_ADD_FAILURE"
},
"status": 200
},
"https://googleads.googleapis.com/v13/customers/1112223333/offlineUserDataJobs:create": {
"https://googleads.googleapis.com/v14/customers/1112223333/offlineUserDataJobs:create": {
"data": {
"resourceName": "customers/111-222-3333/offlineUserDataJobs/OFFLINE_USER_DATA_JOB_ID"
},
"status": 200
},
"https://googleads.googleapis.com/v13/customers/customerid/offlineUserDataJobs:create": {
"https://googleads.googleapis.com/v14/customers/customerid/offlineUserDataJobs:create": {
"status": 401,
"data": {
"error": {
Expand All @@ -21,11 +21,11 @@
}
}
},
"https://googleads.googleapis.com/v13/customers/1112223333/offlineUserDataJobs/OFFLINE_USER_DATA_JOB_ID:addOperations": {
"https://googleads.googleapis.com/v14/customers/1112223333/offlineUserDataJobs/OFFLINE_USER_DATA_JOB_ID:addOperations": {
"status": 200,
"data": {}
},
"https://googleads.googleapis.com/v13/customers/11122233331/offlineUserDataJobs/OFFLINE_USER_DATA_JOB_ID_FOR_ADD_FAILURE:addOperations": {
"https://googleads.googleapis.com/v14/customers/11122233331/offlineUserDataJobs/OFFLINE_USER_DATA_JOB_ID_FOR_ADD_FAILURE:addOperations": {
"status": 400,
"data": {
"error": {
Expand All @@ -34,7 +34,7 @@
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.ads.googleads.v13.errors.GoogleAdsFailure",
"@type": "type.googleapis.com/google.ads.googleads.v14.errors.GoogleAdsFailure",
"errors": [
{
"errorCode": {
Expand Down Expand Up @@ -67,13 +67,13 @@
}
}
},
"https://googleads.googleapis.com/v13/customers/1112223333/offlineUserDataJobs/OFFLINE_USER_DATA_JOB_ID:run": {
"https://googleads.googleapis.com/v14/customers/1112223333/offlineUserDataJobs/OFFLINE_USER_DATA_JOB_ID:run": {
"status": 200,
"data": {
"name": "customers/111-222-3333/operations/abcd="
}
},
"https://googleads.googleapis.com/v13/customers/customerid/offlineUserDataJobs/OFFLINE_USER_DATA_JOB_ID_ADD_FAILURE:addOperations": {
"https://googleads.googleapis.com/v14/customers/customerid/offlineUserDataJobs/OFFLINE_USER_DATA_JOB_ID_ADD_FAILURE:addOperations": {
"status": 400,
"data": {
"error": {
Expand All @@ -82,7 +82,7 @@
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.ads.googleads.v13.errors.GoogleAdsFailure",
"@type": "type.googleapis.com/google.ads.googleads.v14.errors.GoogleAdsFailure",
"errors": [
{
"errorCode": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"https://googleads.googleapis.com/v13/customers/7693729833/offlineUserDataJobs:create": {
"https://googleads.googleapis.com/v14/customers/7693729833/offlineUserDataJobs:create": {
"status": 200,
"data": {
"resourceName": "customers/9249589672/offlineUserDataJobs/18025019461"
}
},
"https://googleads.googleapis.com/v13/customers/7693729833/offlineUserDataJobs/18025019461:addOperations": {
"https://googleads.googleapis.com/v14/customers/7693729833/offlineUserDataJobs/18025019461:addOperations": {
"status": 200
},
"https://googleads.googleapis.com/v13/customers/7693729833/offlineUserDataJobs/18025019461:run": {
"https://googleads.googleapis.com/v14/customers/7693729833/offlineUserDataJobs/18025019461:run": {
"status": 200
},
"https://googleads.googleapis.com/v13/customers/7693729834/offlineUserDataJobs:create": {
"https://googleads.googleapis.com/v14/customers/7693729834/offlineUserDataJobs:create": {
"status": 200,
"data": {
"resourceName": "customers/9249589672/offlineUserDataJobs/18025019462"
}
},
"https://googleads.googleapis.com/v13/customers/7693729834/offlineUserDataJobs/18025019462:addOperations": {
"https://googleads.googleapis.com/v14/customers/7693729834/offlineUserDataJobs/18025019462:addOperations": {
"response": {
"data": {
"error": {
Expand Down Expand Up @@ -60,7 +60,7 @@
"status": 400
}
},
"https://googleads.googleapis.com/v13/customers/1234567890/googleAds:searchStream": {
"https://googleads.googleapis.com/v14/customers/1234567890/googleAds:searchStream": {
"response": {
"data": [
{
Expand All @@ -74,11 +74,11 @@
"status": 401
}
},
"https://googleads.googleapis.com/v13/customers/1234567899/googleAds:searchStream": {
"https://googleads.googleapis.com/v14/customers/1234567899/googleAds:searchStream": {
"response": "",
"code": "ECONNREFUSED"
},
"https://googleads.googleapis.com/v13/customers/1234567891/googleAds:searchStream": {
"https://googleads.googleapis.com/v14/customers/1234567891/googleAds:searchStream": {
"data": [
{
"results": [
Expand All @@ -95,7 +95,7 @@
],
"status": 200
},
"https://googleads.googleapis.com/v13/customers/1234567891:uploadConversionAdjustments": {
"https://googleads.googleapis.com/v14/customers/1234567891:uploadConversionAdjustments": {
"data": [
{
"adjustmentType": "ENHANCEMENT",
Expand Down
Loading

0 comments on commit dd207d0

Please sign in to comment.