Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat: enrich PostHog person records for machine identities with Redis-based dedup

Devin AI4b493856e952
diff --git a/backend/e2e-test/mocks/keystore.ts b/backend/e2e-test/mocks/keystore.ts
index d112b54b0b..1e59a69a35 100644
--- a/backend/e2e-test/mocks/keystore.ts
+++ b/backend/e2e-test/mocks/keystore.ts
@@ -21,6 +21,11 @@ export const mockKeyStore = (): TKeyStoreFactory => {
store[key] = value;
return "OK";
},
+ setItemWithExpiryNX: async (key, _expiryInSeconds, value) => {
+ if (store[key] !== undefined) return null;
+ store[key] = value;
+ return "OK";
+ },
deleteItem: async (key) => {
delete store[key];
return 1;
diff --git a/backend/src/keystore/keystore.ts b/backend/src/keystore/keystore.ts
index 73d8df9a71..6d4e76475b 100644
--- a/backend/src/keystore/keystore.ts
+++ b/backend/src/keystore/keystore.ts
@@ -140,6 +140,12 @@ export type TKeyStoreFactory = {
value: string | number | Buffer,
prefix?: string
) => Promise<"OK">;
+ setItemWithExpiryNX: (
+ key: string,
+ expiryInSeconds: number | string,
+ value: string | number | Buffer,
+ prefix?: string
+ ) => Promise<"OK" | null>;
deleteItem: (key: string) => Promise<number>;
deleteItemsByKeyIn: (keys: string[]) => Promise<number>;
deleteItems: (arg: TDeleteItems) => Promise<number>;
@@ -221,6 +227,13 @@ export const keyStoreFactory = (
prefix?: string
) => primaryRedis.set(prefix ? `${prefix}:${key}` : key, value, "EX", expiryInSeconds);
+ const setItemWithExpiryNX = async (
+ key: string,
+ expiryInSeconds: number | string,
+ value: string | number | Buffer,
+ prefix?: string
+ ) => primaryRedis.set(prefix ? `${prefix}:${key}` : key, value, "EX", expiryInSeconds, "NX");
+
const deleteItem = async (key: string) => primaryRedis.del(key);
const deleteItemsByKeyIn = async (keys: string[]) => {
@@ -376,6 +389,7 @@ export const keyStoreFactory = (
getItem,
setExpiry,
setItemWithExpiry,
+ setItemWithExpiryNX,
deleteItem,
deleteItems,
incrementBy,
diff --git a/backend/src/keystore/memory.ts b/backend/src/keystore/memory.ts
index 556da56e2a..44e25eb5bc 100644
--- a/backend/src/keystore/memory.ts
+++ b/backend/src/keystore/memory.ts
@@ -22,6 +22,11 @@ export const inMemoryKeyStore = (): TKeyStoreFactory => {
store[key] = value;
return "OK";
},
+ setItemWithExpiryNX: async (key, _expiryInSeconds, value) => {
+ if (store[key] !== undefined) return null;
+ store[key] = value;
+ return "OK";
+ },
deleteItem: async (key) => {
delete store[key];
return 1;
diff --git a/backend/src/server/plugins/auth/inject-identity.ts b/backend/src/server/plugins/auth/inject-identity.ts
index 8b7e7efa89..895a2d75f9 100644
--- a/backend/src/server/plugins/auth/inject-identity.ts
+++ b/backend/src/server/plugins/auth/inject-identity.ts
@@ -241,6 +241,17 @@ export const injectIdentity = fp(
}
requestContext.set("identityAuthInfo", identityAuthInfo);
+
+ // Fire-and-forget: enrich PostHog person record for this machine identity
+ void server.services.telemetry
+ .identifyIdentity(identity.identityId, {
+ name: identity.identityName,
+ authMethod: identity.authMethod
+ })
+ .catch((error) => {
+ req.log.error(error, "Failed to enrich PostHog identity");
+ });
+
break;
}
case AuthMode.SERVICE_TOKEN: {
diff --git a/backend/src/services/telemetry/telemetry-service.ts b/backend/src/services/telemetry/telemetry-service.ts
index 5961344618..5be1b110db 100644
--- a/backend/src/services/telemetry/telemetry-service.ts
+++ b/backend/src/services/telemetry/telemetry-service.ts
@@ -38,7 +38,7 @@ export type TTelemetryServiceFactory = ReturnType<typeof telemetryServiceFactory
export type TTelemetryServiceFactoryDep = {
keyStore: Pick<
TKeyStoreFactory,
- "incrementBy" | "deleteItemsByKeyIn" | "setItemWithExpiry" | "getKeysByPattern" | "getItems"
+ "incrementBy" | "deleteItemsByKeyIn" | "setItemWithExpiry" | "setItemWithExpiryNX" | "getKeysByPattern" | "getItems"
>;
licenseService: Pick<TLicenseServiceFactory, "getInstanceType" | "getPlan">;
orgDAL: Pick<TOrgDALFactory, "findOrgById">;
@@ -391,6 +391,47 @@ To opt into telemetry, you can set "TELEMETRY_ENABLED=true" within the environme
}
};
+ const IDENTITY_IDENTIFY_CACHE_KEY_PREFIX = "telemetry-identify-identity";
+ const IDENTITY_IDENTIFY_CACHE_TTL = 600; // 10 minutes in seconds
+
+ // In-memory fallback dedup set to limit blast radius during Redis outages
+ const inMemoryIdentityDedup = new Set<string>();
+
+ const identifyIdentity = async (
+ identityId: string,
+ properties: {
+ name?: string;
+ authMethod?: string;
+ }
+ ) => {
+ if (postHog && identityId) {
+ const instanceType = licenseService.getInstanceType();
+ if (instanceType === InstanceType.Cloud) {
+ const dedupKey = `${identityId}-${properties.authMethod ?? ""}`;
+ try {
+ const cacheKey = `${IDENTITY_IDENTIFY_CACHE_KEY_PREFIX}:${dedupKey}`;
+ // Atomic SET NX + EX: only the first caller within the TTL window proceeds
+ const wasSet = await keyStore.setItemWithExpiryNX(cacheKey, IDENTITY_IDENTIFY_CACHE_TTL, "1");
+ if (!wasSet) return;
+ } catch (error) {
+ logger.error(error, `Failed to check PostHog identity dedup cache [identityId=${identityId}]`);
+ // In-memory fallback to limit blast radius during Redis outage
+ if (inMemoryIdentityDedup.has(dedupKey)) return;
+ inMemoryIdentityDedup.add(dedupKey);
+ const timer = setTimeout(() => inMemoryIdentityDedup.delete(dedupKey), IDENTITY_IDENTIFY_CACHE_TTL * 1000);
+ timer.unref();
+ }
+
+ const distinctId = `identity-${identityId}`;
+ try {
+ postHog.identify({ distinctId, properties });
+ } catch (err) {
+ logger.error(err, `Failed to call postHog.identify for machine identity [identityId=${identityId}]`);
+ }
+ }
+ }
+ };
+
const flushAll = async () => {
if (postHog) {
await postHog.shutdownAsync();
@@ -401,6 +442,7 @@ To opt into telemetry, you can set "TELEMETRY_ENABLED=true" within the environme
sendLoopsEvent,
sendPostHogEvents,
identifyUser,
+ identifyIdentity,
processAggregatedEvents,
flushAll,
getBucketForDistinctId