Skip to content

Commit

Permalink
Merge branch 'develop' into chore.update-workflow-engine
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Sep 14, 2023
2 parents eeb6938 + e98ea84 commit 38e7806
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 29 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
39 changes: 39 additions & 0 deletions src/v0/destinations/mp/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,37 @@ const processRevenueEvents = (message, destination, revenueValue) => {
return responseBuilderSimple(payload, message, 'revenue', destination.Config);
};

/**
* This function is used to process the incremental properties
* ref :- https://developer.mixpanel.com/reference/profile-numerical-add
* @param {*} message
* @param {*} destination
* @param {*} propIncrements
* @returns
*/
const processIncrementalProperties = (message, destination, propIncrements) => {
const payload = {
$add: {},
$token: destination.Config.token,
$distinct_id: message.userId || message.anonymousId,
};

if (destination?.Config.identityMergeApi === 'simplified') {
payload.$distinct_id = message.userId || `$device:${message.anonymousId}`;
}

Object.keys(message.properties).forEach((prop) => {
const value = message.properties[prop];
if (value && propIncrements.includes(prop)) {
payload.$add[prop] = value;
}
});

return Object.keys(payload.$add).length > 0
? responseBuilderSimple(payload, message, 'incremental_properties', destination.Config)
: null;
};

const getEventValueForTrackEvent = (message, destination) => {
const mappedProperties = constructPayload(message, mPEventPropertiesConfigJson);
// This is to conform with SDKs sending timestamp component with messageId
Expand Down Expand Up @@ -178,6 +209,14 @@ const processTrack = (message, destination) => {
if (revenue) {
returnValue.push(processRevenueEvents(message, destination, revenue));
}

if (Array.isArray(destination.Config.propIncrements)) {
const propIncrements = destination.Config.propIncrements.map((item) => item.property);
const response = processIncrementalProperties(message, destination, propIncrements);
if (response) {
returnValue.push(response);
}
}
returnValue.push(getEventValueForTrackEvent(message, destination));
return returnValue;
};
Expand Down
5 changes: 1 addition & 4 deletions src/v0/destinations/mp/util.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const {
combineBatchRequestsWithSameJobIds,
combineBatchRequestsWithSameJobIds2,
} = require('./util');
const { combineBatchRequestsWithSameJobIds } = require('./util');

const destinationMock = {
Config: {
Expand Down
25 changes: 22 additions & 3 deletions test/__tests__/data/mp_input.json
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,18 @@
"apiKey": "dummyApiKey",
"token": "dummyApiKey",
"prefixProperties": true,
"useNativeSDK": false
"useNativeSDK": false,
"propIncrements": [
{
"property": "counter"
},
{
"property": "item_purchased"
},
{
"property": "number_of_logins"
}
]
},
"DestinationDefinition": {
"DisplayName": "Kiss Metrics",
Expand Down Expand Up @@ -493,7 +504,10 @@
"originalTimestamp": "2020-01-24T06:29:02.364Z",
"properties": {
"currency": "USD",
"revenue": 45.89
"revenue": 45.89,
"counter": 1,
"item_purchased": "2",
"number_of_logins": ""
},
"receivedAt": "2020-01-24T11:59:02.403+05:30",
"request_ip": "[::1]:53710",
Expand Down Expand Up @@ -582,7 +596,12 @@
"apiKey": "dummyApiKey",
"token": "dummyApiKey",
"prefixProperties": true,
"useNativeSDK": false
"useNativeSDK": false,
"propIncrements": [
{
"property": ""
}
]
},
"DestinationDefinition": {
"DisplayName": "Kiss Metrics",
Expand Down
20 changes: 19 additions & 1 deletion test/__tests__/data/mp_output.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@
"files": {},
"userId": "e6ab2c5e-2cda-44a9-a962-e2f67df78bca"
},
{
"version": "1",
"type": "REST",
"method": "POST",
"endpoint": "https://api.mixpanel.com/engage/",
"headers": {},
"params": {},
"body": {
"JSON": {},
"JSON_ARRAY": {
"batch": "[{\"$add\":{\"counter\":1,\"item_purchased\":\"2\"},\"$token\":\"dummyApiKey\",\"$distinct_id\":\"e6ab2c5e-2cda-44a9-a962-e2f67df78bca\"}]"
},
"XML": {},
"FORM": {}
},
"files": {},
"userId": "e6ab2c5e-2cda-44a9-a962-e2f67df78bca"
},
{
"version": "1",
"type": "REST",
Expand All @@ -138,7 +156,7 @@
"body": {
"JSON": {},
"JSON_ARRAY": {
"batch": "[{\"event\":\"test revenue MIXPANEL\",\"properties\":{\"currency\":\"USD\",\"revenue\":45.89,\"city\":\"Disney\",\"country\":\"USA\",\"email\":\"[email protected]\",\"firstName\":\"Mickey\",\"ip\":\"0.0.0.0\",\"$current_url\":\"https://docs.rudderstack.com/destinations/mixpanel\",\"$screen_dpi\":2,\"mp_lib\":\"RudderLabs JavaScript SDK\",\"$app_build_number\":\"1.0.0\",\"$app_version_string\":\"1.0.5\",\"$insert_id\":\"a6a0ad5a-bd26-4f19-8f75-38484e580fc7\",\"token\":\"dummyApiKey\",\"distinct_id\":\"e6ab2c5e-2cda-44a9-a962-e2f67df78bca\",\"time\":1579847342,\"$browser\":\"Chrome\",\"$browser_version\":\"79.0.3945.117\"}}]"
"batch": "[{\"event\":\"test revenue MIXPANEL\",\"properties\":{\"currency\":\"USD\",\"revenue\":45.89,\"counter\":1,\"item_purchased\":\"2\",\"number_of_logins\":\"\",\"city\":\"Disney\",\"country\":\"USA\",\"email\":\"[email protected]\",\"firstName\":\"Mickey\",\"ip\":\"0.0.0.0\",\"$current_url\":\"https://docs.rudderstack.com/destinations/mixpanel\",\"$screen_dpi\":2,\"mp_lib\":\"RudderLabs JavaScript SDK\",\"$app_build_number\":\"1.0.0\",\"$app_version_string\":\"1.0.5\",\"$insert_id\":\"a6a0ad5a-bd26-4f19-8f75-38484e580fc7\",\"token\":\"dummyApiKey\",\"distinct_id\":\"e6ab2c5e-2cda-44a9-a962-e2f67df78bca\",\"time\":1579847342,\"$browser\":\"Chrome\",\"$browser_version\":\"79.0.3945.117\"}}]"
},
"XML": {},
"FORM": {}
Expand Down

0 comments on commit 38e7806

Please sign in to comment.