From 6cc2bbadd77084786158b4a6d526e6eaefb0a9b9 Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Wed, 15 Jun 2022 11:46:03 +0100 Subject: [PATCH] Add deployment environment to identities --- .../backend-core/src/events/identification.ts | 20 +++++++++++++++++++ .../src/events/processors/PosthogProcessor.ts | 2 +- .../types/src/sdk/events/identification.ts | 5 +++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/backend-core/src/events/identification.ts b/packages/backend-core/src/events/identification.ts index abc1f2a50b..a29a6821cd 100644 --- a/packages/backend-core/src/events/identification.ts +++ b/packages/backend-core/src/events/identification.ts @@ -35,6 +35,7 @@ const pkg = require("../../package.json") */ export const getCurrentIdentity = async (): Promise => { let identityContext = identityCtx.getIdentity() + const environment = getDeploymentEnvironment() let identityType @@ -52,6 +53,7 @@ export const getCurrentIdentity = async (): Promise => { hosting, type: identityType, installationId, + environment, } } else if (identityType === IdentityType.TENANT) { const installationId = await getInstallationId() @@ -64,6 +66,7 @@ export const getCurrentIdentity = async (): Promise => { hosting, installationId, tenantId, + environment, } } else if (identityType === IdentityType.USER) { const userContext = identityContext as UserContext @@ -84,6 +87,7 @@ export const getCurrentIdentity = async (): Promise => { hosting, installationId, tenantId, + environment, } } else { throw new Error("Unknown identity type") @@ -98,12 +102,14 @@ export const identifyInstallationGroup = async ( const type = IdentityType.INSTALLATION const hosting = getHostingFromEnv() const version = pkg.version + const environment = getDeploymentEnvironment() const group: InstallationGroup = { id, type, hosting, version, + environment, } await identifyGroup(group, timestamp) @@ -120,6 +126,7 @@ export const identifyTenantGroup = async ( const id = await getEventTenantId(tenantId) const type = IdentityType.TENANT const installationId = await getInstallationId() + const environment = getDeploymentEnvironment() let hosting: Hosting let profession: string | undefined @@ -137,6 +144,7 @@ export const identifyTenantGroup = async ( id, type, hosting, + environment, installationId, profession, companySize, @@ -164,6 +172,7 @@ export const identifyUser = async ( account && account?.budibaseUserId === user._id ? account.verified : false const installationId = await getInstallationId() const hosting = account ? account.hosting : getHostingFromEnv() + const environment = getDeploymentEnvironment() const identity: UserIdentity = { id, @@ -176,6 +185,7 @@ export const identifyUser = async ( providerType, builder, admin, + environment, } await identify(identity, timestamp) @@ -190,6 +200,7 @@ export const identifyAccount = async (account: Account) => { const accountHolder = true const hosting = account.hosting const installationId = await getInstallationId() + const environment = getDeploymentEnvironment() if (isCloudAccount(account)) { if (account.budibaseUserId) { @@ -207,6 +218,7 @@ export const identifyAccount = async (account: Account) => { providerType, verified, accountHolder, + environment, } await identify(identity) @@ -226,6 +238,14 @@ export const identifyGroup = async ( await processors.identifyGroup(group, timestamp) } +const getDeploymentEnvironment = () => { + if (env.isDev()) { + return "development" + } else { + return env.DEPLOYMENT_ENVIRONMENT + } +} + const getHostingFromEnv = () => { return env.SELF_HOSTED ? Hosting.SELF : Hosting.CLOUD } diff --git a/packages/backend-core/src/events/processors/PosthogProcessor.ts b/packages/backend-core/src/events/processors/PosthogProcessor.ts index 68d6e2d139..67407fdd5c 100644 --- a/packages/backend-core/src/events/processors/PosthogProcessor.ts +++ b/packages/backend-core/src/events/processors/PosthogProcessor.ts @@ -23,7 +23,7 @@ export default class PosthogProcessor implements EventProcessor { ): Promise { properties.version = pkg.version properties.service = env.SERVICE - properties.environment = env.DEPLOYMENT_ENVIRONMENT + properties.environment = identity.environment properties.hosting = identity.hosting const appId = context.getAppId() diff --git a/packages/types/src/sdk/events/identification.ts b/packages/types/src/sdk/events/identification.ts index bc0d08bfa9..3f4e7ec9d4 100644 --- a/packages/types/src/sdk/events/identification.ts +++ b/packages/types/src/sdk/events/identification.ts @@ -10,6 +10,8 @@ export enum GroupType { export interface Group { id: string type: IdentityType + environment: string + hosting: Hosting } export interface TenantGroup extends Group { @@ -17,13 +19,11 @@ export interface TenantGroup extends Group { // as we don't have this at the user level profession?: string // only available in cloud companySize?: string // only available in cloud - hosting: Hosting // need hosting at the tenant level for cloud self host accounts installationId: string } export interface InstallationGroup extends Group { version: string - hosting: Hosting } // IDENTITIES @@ -38,6 +38,7 @@ export interface Identity { id: string type: IdentityType hosting: Hosting + environment: string installationId?: string tenantId?: string }