Skip to content

Commit

Permalink
chore: add mixpanel batch size metrics (#3201)
Browse files Browse the repository at this point in the history
* chore: add mixpanel batch size metrics

* refactor: reduce duplicate code

* fix: calculate batch metrics after chuncking
  • Loading branch information
Gauravudia authored Mar 27, 2024
1 parent 8a09144 commit b1b479f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/util/prometheus.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,30 @@ class Prometheus {
type: 'gauge',
labelNames: ['destination_id'],
},
{
name: 'mixpanel_batch_engage_pack_size',
help: 'mixpanel_batch_engage_pack_size',
type: 'gauge',
labelNames: ['destination_id'],
},
{
name: 'mixpanel_batch_group_pack_size',
help: 'mixpanel_batch_group_pack_size',
type: 'gauge',
labelNames: ['destination_id'],
},
{
name: 'mixpanel_batch_track_pack_size',
help: 'mixpanel_batch_track_pack_size',
type: 'gauge',
labelNames: ['destination_id'],
},
{
name: 'mixpanel_batch_import_pack_size',
help: 'mixpanel_batch_import_pack_size',
type: 'gauge',
labelNames: ['destination_id'],
},

// Histograms
{
Expand Down
16 changes: 16 additions & 0 deletions src/v0/destinations/mp/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const {
batchEvents,
trimTraits,
generatePageOrScreenCustomEventName,
recordBatchSizeMetrics,
} = require('./util');
const { CommonUtils } = require('../../../util/common');

Expand Down Expand Up @@ -479,6 +480,13 @@ const process = (event) => processSingleMessage(event.message, event.destination
// Ref: https://help.mixpanel.com/hc/en-us/articles/115004613766-Default-Properties-Collected-by-Mixpanel
// Ref: https://help.mixpanel.com/hc/en-us/articles/115004561786-Track-UTM-Tags
const processRouterDest = async (inputs, reqMetadata) => {
const batchSize = {
engage: 0,
groups: 0,
track: 0,
import: 0,
};

const groupedEvents = groupEventsByType(inputs);
const response = await Promise.all(
groupedEvents.map(async (listOfEvents) => {
Expand Down Expand Up @@ -521,12 +529,20 @@ const processRouterDest = async (inputs, reqMetadata) => {
...importRespList,
];

batchSize.engage += engageRespList.length;
batchSize.groups += groupsRespList.length;
batchSize.track += trackRespList.length;
batchSize.import += importRespList.length;

return [...batchSuccessRespList, ...batchErrorRespList];
}),
);

// Flatten the response array containing batched events from multiple groups
const allBatchedEvents = lodash.flatMap(response);

const { destination } = allBatchedEvents[0];
recordBatchSizeMetrics(batchSize, destination.ID);
return combineBatchRequestsWithSameJobIds(allBatchedEvents);
};

Expand Down
28 changes: 28 additions & 0 deletions src/v0/destinations/mp/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const {
mappingConfig,
} = require('./config');
const { CommonUtils } = require('../../../util/common');
const stats = require('../../../util/stats');

const mPIdentifyConfigJson = mappingConfig[ConfigCategory.IDENTIFY.name];
const mPProfileAndroidConfigJson = mappingConfig[ConfigCategory.PROFILE_ANDROID.name];
Expand Down Expand Up @@ -342,6 +343,32 @@ const generatePageOrScreenCustomEventName = (message, userDefinedEventTemplate)
return eventName;
};

/**
* Records the batch size metrics for different endpoints.
*
* @param {Object} batchSize - The object containing the batch size for different endpoints.
* @param {number} batchSize.engage - The batch size for engage endpoint.
* @param {number} batchSize.groups - The batch size for group endpoint.
* @param {number} batchSize.track - The batch size for track endpoint.
* @param {number} batchSize.import - The batch size for import endpoint.
* @param {string} destinationId - The ID of the destination.
* @returns {void}
*/
const recordBatchSizeMetrics = (batchSize, destinationId) => {
stats.gauge('mixpanel_batch_engage_pack_size', batchSize.engage, {
destination_id: destinationId,
});
stats.gauge('mixpanel_batch_group_pack_size', batchSize.groups, {
destination_id: destinationId,
});
stats.gauge('mixpanel_batch_track_pack_size', batchSize.track, {
destination_id: destinationId,
});
stats.gauge('mixpanel_batch_import_pack_size', batchSize.import, {
destination_id: destinationId,
});
};

module.exports = {
createIdentifyResponse,
isImportAuthCredentialsAvailable,
Expand All @@ -351,4 +378,5 @@ module.exports = {
batchEvents,
trimTraits,
generatePageOrScreenCustomEventName,
recordBatchSizeMetrics,
};

0 comments on commit b1b479f

Please sign in to comment.