feat(telemetry): added telemetry events for creation of integration,project,machine identity and user invitation to org

This commit is contained in:
Akhil Mohan
2024-02-23 13:19:16 +05:30
parent a231813f01
commit 76daa20d69
8 changed files with 148 additions and 18 deletions
+17
View File
@@ -0,0 +1,17 @@
import { FastifyRequest } from "fastify";
import { ActorType } from "@app/services/auth/auth-type";
// this is a unique id for sending posthog event
export const getTelemetryDistinctId = (req: FastifyRequest) => {
if (req.auth.actor === ActorType.USER) {
return req.auth.user.email;
}
if (req.auth.actor === ActorType.IDENTITY) {
return `identity-${req.auth.identityId}`;
}
if (req.auth.actor === ActorType.SERVICE) {
return `service-token-${req.auth.serviceToken.id}`;
}
return "unknown-auth-data";
};
+4
View File
@@ -585,4 +585,8 @@ export const registerRoutes = async (
); );
await server.register(registerV2Routes, { prefix: "/api/v2" }); await server.register(registerV2Routes, { prefix: "/api/v2" });
await server.register(registerV3Routes, { prefix: "/api/v3" }); await server.register(registerV3Routes, { prefix: "/api/v3" });
server.addHook("onClose", async () => {
await telemetryService.flushAll();
});
}; };
@@ -2,8 +2,10 @@ import { z } from "zod";
import { IdentitiesSchema, OrgMembershipRole } from "@app/db/schemas"; import { IdentitiesSchema, OrgMembershipRole } from "@app/db/schemas";
import { EventType } from "@app/ee/services/audit-log/audit-log-types"; import { EventType } from "@app/ee/services/audit-log/audit-log-types";
import { getTelemetryDistinctId } from "@app/server/lib/telemtry";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth"; import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { AuthMode } from "@app/services/auth/auth-type"; import { AuthMode } from "@app/services/auth/auth-type";
import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types";
export const registerIdentityRouter = async (server: FastifyZodProvider) => { export const registerIdentityRouter = async (server: FastifyZodProvider) => {
server.route({ server.route({
@@ -49,6 +51,17 @@ export const registerIdentityRouter = async (server: FastifyZodProvider) => {
} }
}); });
server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.MachineIdentityCreated,
distinctId: getTelemetryDistinctId(req),
properties: {
orgId: req.body.organizationId,
name: identity.name,
identityId: identity.id,
...req.auditLogInfo
}
});
return { identity }; return { identity };
} }
}); });
@@ -3,8 +3,10 @@ import { z } from "zod";
import { IntegrationsSchema } from "@app/db/schemas"; import { IntegrationsSchema } from "@app/db/schemas";
import { EventType } from "@app/ee/services/audit-log/audit-log-types"; import { EventType } from "@app/ee/services/audit-log/audit-log-types";
import { removeTrailingSlash, shake } from "@app/lib/fn"; import { removeTrailingSlash, shake } from "@app/lib/fn";
import { getTelemetryDistinctId } from "@app/server/lib/telemtry";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth"; import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { AuthMode } from "@app/services/auth/auth-type"; import { AuthMode } from "@app/services/auth/auth-type";
import { PostHogEventTypes, TIntegrationCreatedEvent } from "@app/services/telemetry/telemetry-types";
export const registerIntegrationRouter = async (server: FastifyZodProvider) => { export const registerIntegrationRouter = async (server: FastifyZodProvider) => {
server.route({ server.route({
@@ -53,28 +55,40 @@ export const registerIntegrationRouter = async (server: FastifyZodProvider) => {
actorOrgId: req.permission.orgId, actorOrgId: req.permission.orgId,
...req.body ...req.body
}); });
const createIntegrationEventProperty = shake({
integrationId: integration.id.toString(),
integration: integration.integration,
environment: req.body.sourceEnvironment,
secretPath: req.body.secretPath,
url: integration.url,
app: integration.app,
appId: integration.appId,
targetEnvironment: integration.targetEnvironment,
targetEnvironmentId: integration.targetEnvironmentId,
targetService: integration.targetService,
targetServiceId: integration.targetServiceId,
path: integration.path,
region: integration.region
}) as TIntegrationCreatedEvent["properties"];
await server.services.auditLog.createAuditLog({ await server.services.auditLog.createAuditLog({
...req.auditLogInfo, ...req.auditLogInfo,
projectId: integrationAuth.projectId, projectId: integrationAuth.projectId,
event: { event: {
type: EventType.CREATE_INTEGRATION, type: EventType.CREATE_INTEGRATION,
// eslint-disable-next-line // eslint-disable-next-line
metadata: shake({ metadata: createIntegrationEventProperty
integrationId: integration.id.toString(), }
integration: integration.integration, });
environment: req.body.sourceEnvironment,
secretPath: req.body.secretPath, server.services.telemetry.sendPostHogEvents({
url: integration.url, event: PostHogEventTypes.IntegrationCreated,
app: integration.app, distinctId: getTelemetryDistinctId(req),
appId: integration.appId, properties: {
targetEnvironment: integration.targetEnvironment, ...createIntegrationEventProperty,
targetEnvironmentId: integration.targetEnvironmentId, projectId: integrationAuth.projectId,
targetService: integration.targetService, ...req.auditLogInfo
targetServiceId: integration.targetServiceId,
path: integration.path,
region: integration.region
// eslint-disable-next-line
}) as any
} }
}); });
return { integration }; return { integration };
@@ -1,8 +1,10 @@
import { z } from "zod"; import { z } from "zod";
import { UsersSchema } from "@app/db/schemas"; import { UsersSchema } from "@app/db/schemas";
import { getTelemetryDistinctId } from "@app/server/lib/telemtry";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth"; import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { ActorType, AuthMode } from "@app/services/auth/auth-type"; import { ActorType, AuthMode } from "@app/services/auth/auth-type";
import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types";
export const registerInviteOrgRouter = async (server: FastifyZodProvider) => { export const registerInviteOrgRouter = async (server: FastifyZodProvider) => {
server.route({ server.route({
@@ -30,6 +32,15 @@ export const registerInviteOrgRouter = async (server: FastifyZodProvider) => {
actorOrgId: req.permission.orgId actorOrgId: req.permission.orgId
}); });
server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.UserOrgInvitation,
distinctId: getTelemetryDistinctId(req),
properties: {
inviteeEmail: req.body.inviteeEmail,
...req.auditLogInfo
}
});
return { return {
completeInviteLink, completeInviteLink,
message: `Send an invite link to ${req.body.inviteeEmail}` message: `Send an invite link to ${req.body.inviteeEmail}`
@@ -4,8 +4,10 @@ import { z } from "zod";
import { ProjectKeysSchema, ProjectsSchema } from "@app/db/schemas"; import { ProjectKeysSchema, ProjectsSchema } from "@app/db/schemas";
import { EventType } from "@app/ee/services/audit-log/audit-log-types"; import { EventType } from "@app/ee/services/audit-log/audit-log-types";
import { authRateLimit } from "@app/server/config/rateLimiter"; import { authRateLimit } from "@app/server/config/rateLimiter";
import { getTelemetryDistinctId } from "@app/server/lib/telemtry";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth"; import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { AuthMode } from "@app/services/auth/auth-type"; import { AuthMode } from "@app/services/auth/auth-type";
import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types";
const projectWithEnv = ProjectsSchema.merge( const projectWithEnv = ProjectsSchema.merge(
z.object({ z.object({
@@ -152,6 +154,16 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
slug: req.body.slug slug: req.body.slug
}); });
server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.ProjectCreated,
distinctId: getTelemetryDistinctId(req),
properties: {
orgId: req.body.organizationId,
name: project.name,
...req.auditLogInfo
}
});
return { project }; return { project };
} }
}); });
@@ -61,8 +61,15 @@ To opt into telemetry, you can set "TELEMETRY_ENABLED=true" within the environme
} }
}; };
const flushAll = async () => {
if (postHog) {
await postHog.shutdownAsync();
}
};
return { return {
sendLoopsEvent, sendLoopsEvent,
sendPostHogEvents sendPostHogEvents,
flushAll
}; };
}; };
@@ -8,7 +8,11 @@ export enum PostHogEventTypes {
UserSignedUp = "User Signed Up", UserSignedUp = "User Signed Up",
SecretRotated = "secrets rotated", SecretRotated = "secrets rotated",
SecretScannerFull = "historical cloud secret scan", SecretScannerFull = "historical cloud secret scan",
SecretScannerPush = "cloud secret scan" SecretScannerPush = "cloud secret scan",
ProjectCreated = "project created",
IntegrationCreated = "integration created",
MachineIdentityCreated = "machine identity created",
UserOrgInvitation = "user org invitation"
} }
export type TSecretModifiedEvent = { export type TSecretModifiedEvent = {
@@ -53,9 +57,57 @@ export type TSecretScannerEvent = {
}; };
}; };
export type TProjectCreateEvent = {
event: PostHogEventTypes.ProjectCreated;
properties: {
name: string;
orgId: string;
};
};
export type TMachineIdentityCreatedEvent = {
event: PostHogEventTypes.MachineIdentityCreated;
properties: {
name: string;
orgId: string;
identityId: string;
};
};
export type TIntegrationCreatedEvent = {
event: PostHogEventTypes.IntegrationCreated;
properties: {
projectId: string;
integrationId: string;
integration: string; // TODO: fix type
environment: string;
secretPath: string;
url?: string;
app?: string;
appId?: string;
targetEnvironment?: string;
targetEnvironmentId?: string;
targetService?: string;
targetServiceId?: string;
path?: string;
region?: string;
};
};
export type TUserOrgInvitedEvent = {
event: PostHogEventTypes.UserOrgInvitation;
properties: {
inviteeEmail: string;
};
};
export type TPostHogEvent = { distinctId: string } & ( export type TPostHogEvent = { distinctId: string } & (
| TSecretModifiedEvent | TSecretModifiedEvent
| TAdminInitEvent | TAdminInitEvent
| TUserSignedUpEvent | TUserSignedUpEvent
| TSecretScannerEvent | TSecretScannerEvent
| TUserOrgInvitedEvent
| TMachineIdentityCreatedEvent
| TIntegrationCreatedEvent
| TProjectCreateEvent
); );