Skip to content

Commit

Permalink
feat: no sdk dappid (#1062)
Browse files Browse the repository at this point in the history
* feat: no sdk dappid

* feat: bump version

* feat: cleanup
  • Loading branch information
abretonc7s authored and christopherferreira9 committed Oct 18, 2024
1 parent e3c7511 commit 092a500
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/sdk-socket-server-next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@metamask/sdk-socket-server-scale",
"version": "2.9.0",
"version": "2.10.0",
"private": true,
"description": "",
"homepage": "https://github.com/MetaMask/metamask-sdk#readme",
Expand Down
54 changes: 32 additions & 22 deletions packages/sdk-socket-server-next/src/analytics-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,24 @@ app.post('/evt', async (_req, res) => {
return res.status(400).json({ error: 'wrong event name' });
}

const channelId: string = body.id || 'socket.io-server';
let channelId: string = body.id || 'sdk';
// Prevent caching of events coming from extension since they are not re-using the same id and prevent increasing redis queue size.
let isExtensionEvent = body.from === 'extension';

if (typeof channelId !== 'string') {
logger.error(`Received event with invalid channelId: ${channelId}`, body);
return res.status(400).json({ status: 'error' });
}

logger.debug(`Received event /evt channelId=${channelId}`, body);
if (channelId === 'sdk') {
channelId = uuidv4();
isExtensionEvent = true;
}

logger.debug(
`Received event /evt channelId=${channelId} isExtensionEvent=${isExtensionEvent}`,
body,
);
let userIdHash = await pubClient.get(channelId);

if (!userIdHash) {
Expand All @@ -247,12 +258,14 @@ app.post('/evt', async (_req, res) => {
`event: ${body.event} channelId: ${channelId} - No cached channel info found for ${userIdHash} - creating new channelId`,
);

await pubClient.set(
channelId,
userIdHash,
'EX',
config.channelExpiry.toString(),
);
if (!isExtensionEvent) {
await pubClient.set(
channelId,
userIdHash,
'EX',
config.channelExpiry.toString(),
);
}
}

if (REDIS_DEBUG_LOGS) {
Expand Down Expand Up @@ -291,12 +304,14 @@ app.post('/evt', async (_req, res) => {
channelInfo,
);

await pubClient.set(
userIdHash,
JSON.stringify(channelInfo),
'EX',
config.channelExpiry.toString(),
);
if (!isExtensionEvent) {
await pubClient.set(
userIdHash,
JSON.stringify(channelInfo),
'EX',
config.channelExpiry.toString(),
);
}
}

if (REDIS_DEBUG_LOGS) {
Expand All @@ -315,7 +330,7 @@ app.post('/evt', async (_req, res) => {
};

// Always check for userId to avoid hot sharding events
if (!event.userId) {
if (!event.userId || event.userId === SDK_EXTENSION_DEFAULT_ID) {
const newUserId = uuidv4();
logger.debug(
`event: ${event.event} - Replacing 'sdk' id with '${newUserId}'`,
Expand All @@ -324,17 +339,12 @@ app.post('/evt', async (_req, res) => {
event.userId = newUserId;
}

// Make sure each events have a valid dappId
// Replace 'sdk' id which translates to '5a374dcd2e5eb762b527af3a5bab6072a4d24493' with fallback to url / title / random uuid
if (
!event.properties.dappId ||
event.properties.dappId === SDK_EXTENSION_DEFAULT_ID
) {
if (!event.properties.dappId) {
// Prevent "N/A" in url and ensure a valid dappId
const newDappId =
event.properties.url && event.properties.url !== 'N/A'
? event.properties.url
: event.properties.title || uuidv4();
: event.properties.title || 'N/A';
event.properties.dappId = newDappId;
logger.debug(
`event: ${event.event} - dappId missing - replacing with '${newDappId}'`,
Expand Down

0 comments on commit 092a500

Please sign in to comment.