Skip to content

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

fix(telemetry): address review feedback - use unified tables, fix cache race, filter invited members

Devin AI3be7ea548660
diff --git a/backend/src/services/telemetry/telemetry-dal.ts b/backend/src/services/telemetry/telemetry-dal.ts
index 02b838f9bd..74f9fbf5e1 100644
--- a/backend/src/services/telemetry/telemetry-dal.ts
+++ b/backend/src/services/telemetry/telemetry-dal.ts
@@ -1,8 +1,10 @@
import { TDbClient } from "@app/db";
-import { TableName } from "@app/db/schemas";
+import { AccessScope, OrgMembershipRole, ProjectMembershipRole, TableName } from "@app/db/schemas";
import { HEARTBEAT_BUFFER_SECONDS } from "@app/ee/services/gateway-v2/gateway-v2-constants";
import { DatabaseError } from "@app/lib/errors";
+const BUILT_IN_ROLE_SLUGS = [...Object.values(OrgMembershipRole), ...Object.values(ProjectMembershipRole)] as string[];
+
export type TTelemetryDALFactory = ReturnType<typeof telemetryDALFactory>;
const IDENTITY_AUTH_TABLE_MAP: Record<string, string> = {
@@ -91,8 +93,18 @@ export const telemetryDALFactory = (db: TDbClient) => {
countTable(db, TableName.AuditLogStream),
countTable(db, TableName.SecretRotationV2),
countTable(db, TableName.Webhook),
- countTable(db, TableName.ProjectRoles),
- countTable(db, TableName.OrgRoles),
+ (async () => {
+ const result = (
+ await db(TableName.Role).whereNotNull("projectId").whereNotIn("slug", BUILT_IN_ROLE_SLUGS).count().first()
+ )?.count as string;
+ return parseInt(result || "0", 10);
+ })(),
+ (async () => {
+ const result = (
+ await db(TableName.Role).whereNull("projectId").whereNotIn("slug", BUILT_IN_ROLE_SLUGS).count().first()
+ )?.count as string;
+ return parseInt(result || "0", 10);
+ })(),
countTable(db, TableName.KmipClient),
countTable(db, TableName.SshHost),
countTable(db, TableName.SshCertificateAuthority),
@@ -173,10 +185,10 @@ export const telemetryDALFactory = (db: TDbClient) => {
const organizations = organizationRows.length;
const organizationNames = organizationRows.map(({ name }) => name);
- const orgUserResult = await db.raw<{ rows: { orgId: string; count: string }[] }>(
- `SELECT "orgId", COUNT(*)::text AS count FROM ${TableName.OrgMembership} GROUP BY "orgId"`
+ const orgUserResult = await db.raw<{ rows: { scopeOrgId: string; count: string }[] }>(
+ `SELECT "scopeOrgId", COUNT(*)::text AS count FROM ${TableName.Membership} WHERE scope = '${AccessScope.Organization}' AND "actorUserId" IS NOT NULL AND status = 'accepted' GROUP BY "scopeOrgId"`
);
- const orgUserMap = new Map(orgUserResult.rows.map((r) => [r.orgId, parseInt(r.count, 10)]));
+ const orgUserMap = new Map(orgUserResult.rows.map((r) => [r.scopeOrgId, parseInt(r.count, 10)]));
const orgProjectResult = await db.raw<{ rows: { orgId: string; count: string }[] }>(
`SELECT "orgId", COUNT(*)::text AS count FROM ${TableName.Project} GROUP BY "orgId"`
diff --git a/backend/src/services/telemetry/telemetry-service.ts b/backend/src/services/telemetry/telemetry-service.ts
index 9468a8b3e4..ff939a6023 100644
--- a/backend/src/services/telemetry/telemetry-service.ts
+++ b/backend/src/services/telemetry/telemetry-service.ts
@@ -111,17 +111,18 @@ const getDeploymentType = (
export const telemetryServiceFactory = ({ keyStore, licenseService, orgDAL }: TTelemetryServiceFactoryDep) => {
const appCfg = getConfig();
- let cachedInstanceId: string | undefined;
- const getInstanceId = async (): Promise<string | undefined> => {
- if (appCfg.INFISICAL_CLOUD) return undefined;
- if (cachedInstanceId) return cachedInstanceId;
- try {
- const { instanceId } = await getServerCfg();
- cachedInstanceId = instanceId;
- return instanceId;
- } catch {
- return undefined;
+ let instanceIdPromise: Promise<string | undefined> | undefined;
+ const getInstanceId = (): Promise<string | undefined> => {
+ if (appCfg.INFISICAL_CLOUD) return Promise.resolve(undefined);
+ if (!instanceIdPromise) {
+ instanceIdPromise = getServerCfg()
+ .then(({ instanceId }) => instanceId)
+ .catch(() => {
+ instanceIdPromise = undefined;
+ return undefined;
+ });
}
+ return instanceIdPromise;
};
if (appCfg.isProductionMode && !appCfg.TELEMETRY_ENABLED) {