Skip to content

Commit

Permalink
Merge pull request #1444 from akhilmhdh/feat/telemetry-new-fields
Browse files Browse the repository at this point in the history
feat(telemetry): added new telemetry events
  • Loading branch information
maidul98 authored Feb 23, 2024
2 parents a231813 + e91499b commit 6ef988f
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 45 deletions.
17 changes: 17 additions & 0 deletions backend/src/server/lib/telemetry.ts
Original file line number Diff line number Diff line change
@@ -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 req.auth.serviceToken.createdByEmail || `service-token-null-creator-${req.auth.serviceTokenId}`; // when user gets removed from system
}
return "unknown-auth-data";
};
4 changes: 4 additions & 0 deletions backend/src/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,4 +585,8 @@ export const registerRoutes = async (
);
await server.register(registerV2Routes, { prefix: "/api/v2" });
await server.register(registerV3Routes, { prefix: "/api/v3" });

server.addHook("onClose", async () => {
await telemetryService.flushAll();
});
};
13 changes: 13 additions & 0 deletions backend/src/server/routes/v1/identity-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { z } from "zod";

import { IdentitiesSchema, OrgMembershipRole } from "@app/db/schemas";
import { EventType } from "@app/ee/services/audit-log/audit-log-types";
import { getTelemetryDistinctId } from "@app/server/lib/telemetry";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { AuthMode } from "@app/services/auth/auth-type";
import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types";

export const registerIdentityRouter = async (server: FastifyZodProvider) => {
server.route({
Expand Down Expand Up @@ -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 };
}
});
Expand Down
46 changes: 30 additions & 16 deletions backend/src/server/routes/v1/integration-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { z } from "zod";
import { IntegrationsSchema } from "@app/db/schemas";
import { EventType } from "@app/ee/services/audit-log/audit-log-types";
import { removeTrailingSlash, shake } from "@app/lib/fn";
import { getTelemetryDistinctId } from "@app/server/lib/telemetry";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { AuthMode } from "@app/services/auth/auth-type";
import { PostHogEventTypes, TIntegrationCreatedEvent } from "@app/services/telemetry/telemetry-types";

export const registerIntegrationRouter = async (server: FastifyZodProvider) => {
server.route({
Expand Down Expand Up @@ -53,28 +55,40 @@ export const registerIntegrationRouter = async (server: FastifyZodProvider) => {
actorOrgId: req.permission.orgId,
...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({
...req.auditLogInfo,
projectId: integrationAuth.projectId,
event: {
type: EventType.CREATE_INTEGRATION,
// eslint-disable-next-line
metadata: 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
// eslint-disable-next-line
}) as any
metadata: createIntegrationEventProperty
}
});

server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.IntegrationCreated,
distinctId: getTelemetryDistinctId(req),
properties: {
...createIntegrationEventProperty,
projectId: integrationAuth.projectId,
...req.auditLogInfo
}
});
return { integration };
Expand Down
11 changes: 11 additions & 0 deletions backend/src/server/routes/v1/invite-org-router.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { z } from "zod";

import { UsersSchema } from "@app/db/schemas";
import { getTelemetryDistinctId } from "@app/server/lib/telemetry";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { ActorType, AuthMode } from "@app/services/auth/auth-type";
import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types";

export const registerInviteOrgRouter = async (server: FastifyZodProvider) => {
server.route({
Expand Down Expand Up @@ -30,6 +32,15 @@ export const registerInviteOrgRouter = async (server: FastifyZodProvider) => {
actorOrgId: req.permission.orgId
});

server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.UserOrgInvitation,
distinctId: getTelemetryDistinctId(req),
properties: {
inviteeEmail: req.body.inviteeEmail,
...req.auditLogInfo
}
});

return {
completeInviteLink,
message: `Send an invite link to ${req.body.inviteeEmail}`
Expand Down
12 changes: 12 additions & 0 deletions backend/src/server/routes/v2/project-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { z } from "zod";
import { ProjectKeysSchema, ProjectsSchema } from "@app/db/schemas";
import { EventType } from "@app/ee/services/audit-log/audit-log-types";
import { authRateLimit } from "@app/server/config/rateLimiter";
import { getTelemetryDistinctId } from "@app/server/lib/telemetry";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { AuthMode } from "@app/services/auth/auth-type";
import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types";

const projectWithEnv = ProjectsSchema.merge(
z.object({
Expand Down Expand Up @@ -152,6 +154,16 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
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 };
}
});
Expand Down
41 changes: 14 additions & 27 deletions backend/src/server/routes/v3/secret-router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FastifyRequest } from "fastify";
import picomatch from "picomatch";
import { z } from "zod";

Expand All @@ -13,26 +12,14 @@ import { EventType } from "@app/ee/services/audit-log/audit-log-types";
import { CommitType } from "@app/ee/services/secret-approval-request/secret-approval-request-types";
import { BadRequestError } from "@app/lib/errors";
import { removeTrailingSlash } from "@app/lib/fn";
import { getTelemetryDistinctId } from "@app/server/lib/telemetry";
import { getUserAgentType } from "@app/server/plugins/audit-log";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { ActorType, AuthMode } from "@app/services/auth/auth-type";
import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types";

import { secretRawSchema } from "../sanitizedSchemas";

const getDistinctId = (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 req.auth.serviceToken.createdByEmail || `service-token-null-creator-${req.auth.serviceTokenId}`; // when user gets removed from system
}
return "unknown-auth-data";
};

export const registerSecretRouter = async (server: FastifyZodProvider) => {
server.route({
url: "/raw",
Expand Down Expand Up @@ -110,7 +97,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => {

server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.SecretPulled,
distinctId: getDistinctId(req),
distinctId: getTelemetryDistinctId(req),
properties: {
numberOfSecrets: secrets.length,
workspaceId,
Expand Down Expand Up @@ -200,7 +187,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => {

server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.SecretPulled,
distinctId: getDistinctId(req),
distinctId: getTelemetryDistinctId(req),
properties: {
numberOfSecrets: 1,
workspaceId,
Expand Down Expand Up @@ -276,7 +263,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => {

server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.SecretCreated,
distinctId: getDistinctId(req),
distinctId: getTelemetryDistinctId(req),
properties: {
numberOfSecrets: 1,
workspaceId: req.body.workspaceId,
Expand Down Expand Up @@ -351,7 +338,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => {

server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.SecretUpdated,
distinctId: getDistinctId(req),
distinctId: getTelemetryDistinctId(req),
properties: {
numberOfSecrets: 1,
workspaceId: req.body.workspaceId,
Expand Down Expand Up @@ -421,7 +408,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => {

server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.SecretDeleted,
distinctId: getDistinctId(req),
distinctId: getTelemetryDistinctId(req),
properties: {
numberOfSecrets: 1,
workspaceId: req.body.workspaceId,
Expand Down Expand Up @@ -527,7 +514,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => {
if (shouldCapture) {
server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.SecretPulled,
distinctId: getDistinctId(req),
distinctId: getTelemetryDistinctId(req),
properties: {
numberOfSecrets: shouldRecordK8Event ? approximateNumberTotalSecrets : secrets.length,
workspaceId: req.query.workspaceId,
Expand Down Expand Up @@ -604,7 +591,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => {

server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.SecretPulled,
distinctId: getDistinctId(req),
distinctId: getTelemetryDistinctId(req),
properties: {
numberOfSecrets: 1,
workspaceId: req.query.workspaceId,
Expand Down Expand Up @@ -767,7 +754,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => {

server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.SecretCreated,
distinctId: getDistinctId(req),
distinctId: getTelemetryDistinctId(req),
properties: {
numberOfSecrets: 1,
workspaceId: req.body.workspaceId,
Expand Down Expand Up @@ -949,7 +936,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => {

server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.SecretUpdated,
distinctId: getDistinctId(req),
distinctId: getTelemetryDistinctId(req),
properties: {
numberOfSecrets: 1,
workspaceId: req.body.workspaceId,
Expand Down Expand Up @@ -1067,7 +1054,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => {

server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.SecretDeleted,
distinctId: getDistinctId(req),
distinctId: getTelemetryDistinctId(req),
properties: {
numberOfSecrets: 1,
workspaceId: req.body.workspaceId,
Expand Down Expand Up @@ -1187,7 +1174,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => {

server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.SecretCreated,
distinctId: getDistinctId(req),
distinctId: getTelemetryDistinctId(req),
properties: {
numberOfSecrets: secrets.length,
workspaceId: req.body.workspaceId,
Expand Down Expand Up @@ -1307,7 +1294,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => {

server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.SecretUpdated,
distinctId: getDistinctId(req),
distinctId: getTelemetryDistinctId(req),
properties: {
numberOfSecrets: secrets.length,
workspaceId: req.body.workspaceId,
Expand Down Expand Up @@ -1415,7 +1402,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => {

server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.SecretDeleted,
distinctId: getDistinctId(req),
distinctId: getTelemetryDistinctId(req),
properties: {
numberOfSecrets: secrets.length,
workspaceId: req.body.workspaceId,
Expand Down
9 changes: 8 additions & 1 deletion backend/src/services/telemetry/telemetry-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
sendLoopsEvent,
sendPostHogEvents
sendPostHogEvents,
flushAll
};
};
Loading

0 comments on commit 6ef988f

Please sign in to comment.