diff --git a/Makefile b/Makefile index edfd92476..266eaf9b5 100644 --- a/Makefile +++ b/Makefile @@ -11,4 +11,4 @@ up-prod: docker-compose -f docker-compose.yml up --build down: - docker-compose down \ No newline at end of file + docker-compose down diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index 9ff1ad9bc..1b2178ca0 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -12,8 +12,8 @@ const MONGO_URL = process.env.MONGO_URL!; const NODE_ENV = process.env.NODE_ENV! || 'production'; const OAUTH_CLIENT_SECRET_HEROKU = process.env.OAUTH_CLIENT_SECRET_HEROKU!; const OAUTH_TOKEN_URL_HEROKU = process.env.OAUTH_TOKEN_URL_HEROKU!; -const POSTHOG_HOST = process.env.POSTHOG_HOST!; -const POSTHOG_PROJECT_API_KEY = process.env.POSTHOG_PROJECT_API_KEY!; +const POSTHOG_HOST = process.env.POSTHOG_HOST! || 'https://app.posthog.com'; +const POSTHOG_PROJECT_API_KEY = process.env.POSTHOG_PROJECT_API_KEY! || 'phc_nSin8j5q2zdhpFDI1ETmFNUIuTG4DwKVyIigrY10XiE'; const PRIVATE_KEY = process.env.PRIVATE_KEY!; const PUBLIC_KEY = process.env.PUBLIC_KEY!; const SENTRY_DSN = process.env.SENTRY_DSN!; @@ -28,6 +28,7 @@ const STRIPE_PRODUCT_STARTER = process.env.STRIPE_PRODUCT_STARTER!; const STRIPE_PUBLISHABLE_KEY = process.env.STRIPE_PUBLISHABLE_KEY!; const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY!; const STRIPE_WEBHOOK_SECRET = process.env.STRIPE_WEBHOOK_SECRET!; +const TELEMETRY_ENABLED = (process.env.TELEMETRY_ENABLED! !== 'false') && true; export { PORT, @@ -59,5 +60,6 @@ export { STRIPE_PRODUCT_STARTER, STRIPE_PUBLISHABLE_KEY, STRIPE_SECRET_KEY, - STRIPE_WEBHOOK_SECRET + STRIPE_WEBHOOK_SECRET, + TELEMETRY_ENABLED }; diff --git a/backend/src/controllers/secretController.ts b/backend/src/controllers/secretController.ts index 5a8913113..d1cf5f65d 100644 --- a/backend/src/controllers/secretController.ts +++ b/backend/src/controllers/secretController.ts @@ -7,16 +7,9 @@ import { reformatPullSecrets } from '../helpers/secret'; import { pushKeys } from '../helpers/key'; -import { PostHog } from 'posthog-node'; import { ENV_SET } from '../variables'; -import { NODE_ENV, POSTHOG_PROJECT_API_KEY, POSTHOG_HOST } from '../config'; -let client: any; -if (NODE_ENV === 'production' && POSTHOG_PROJECT_API_KEY && POSTHOG_HOST) { - client = new PostHog(POSTHOG_PROJECT_API_KEY, { - host: POSTHOG_HOST - }); -} +import { postHogClient } from '../services'; interface PushSecret { ciphertextKey: string; @@ -68,11 +61,10 @@ export const pushSecrets = async (req: Request, res: Response) => { keys }); - if (client) { - // capture secrets pushed event in production - client.capture({ - distinctId: req.user.email, + if (postHogClient) { + postHogClient.capture({ event: 'secrets pushed', + distinctId: req.user.email, properties: { numberOfSecrets: secrets.length, environment, @@ -81,6 +73,7 @@ export const pushSecrets = async (req: Request, res: Response) => { } }); } + } catch (err) { Sentry.setUser({ email: req.user.email }); Sentry.captureException(err); @@ -131,9 +124,9 @@ export const pullSecrets = async (req: Request, res: Response) => { secrets = reformatPullSecrets({ secrets }); } - if (client) { + if (postHogClient) { // capture secrets pushed event in production - client.capture({ + postHogClient.capture({ distinctId: req.user.email, event: 'secrets pulled', properties: { @@ -198,9 +191,9 @@ export const pullSecretsServiceToken = async (req: Request, res: Response) => { workspace: req.serviceToken.workspace }; - if (client) { + if (postHogClient) { // capture secrets pushed event in production - client.capture({ + postHogClient.capture({ distinctId: req.serviceToken.user.email, event: 'secrets pulled', properties: { diff --git a/backend/src/index.ts b/backend/src/index.ts index e25e18f59..fc1d9a417 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -6,7 +6,8 @@ import mongoose from 'mongoose'; import dotenv from 'dotenv'; dotenv.config(); import * as Sentry from '@sentry/node'; -import { PORT, SENTRY_DSN, NODE_ENV, MONGO_URL, SITE_URL } from './config'; +// import { PostHogClient } from './services'; +import { PORT, SENTRY_DSN, NODE_ENV, MONGO_URL, SITE_URL, POSTHOG_PROJECT_API_KEY, POSTHOG_HOST, TELEMETRY_ENABLED } from './config'; import { apiLimiter } from './helpers/rateLimiter'; const app = express(); @@ -18,6 +19,12 @@ Sentry.init({ environment: NODE_ENV }); +// PostHogClient.init({ +// projectApiKey: POSTHOG_PROJECT_API_KEY, +// host: POSTHOG_HOST, +// telemetryEnabled: TELEMETRY_ENABLED +// }); + import { signup as signupRouter, auth as authRouter, diff --git a/backend/src/services/PostHogClient.ts b/backend/src/services/PostHogClient.ts new file mode 100644 index 000000000..eaf56aa6a --- /dev/null +++ b/backend/src/services/PostHogClient.ts @@ -0,0 +1,15 @@ +import { PostHog } from 'posthog-node'; +import { NODE_ENV, POSTHOG_HOST, POSTHOG_PROJECT_API_KEY, TELEMETRY_ENABLED } from '../config'; + +let postHogClient: any; +if ( + NODE_ENV === 'production' + && TELEMETRY_ENABLED +) { + // case: enable opt-out telemetry in production + postHogClient = new PostHog(POSTHOG_PROJECT_API_KEY, { + host: POSTHOG_HOST + }); +} + +export default postHogClient; \ No newline at end of file diff --git a/backend/src/services/index.ts b/backend/src/services/index.ts new file mode 100644 index 000000000..54cdf94f4 --- /dev/null +++ b/backend/src/services/index.ts @@ -0,0 +1,5 @@ +import postHogClient from './PostHogClient'; + +export { + postHogClient +} \ No newline at end of file diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 234fa97bd..dc3c68058 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -51,8 +51,11 @@ services: - ./frontend/components:/app/components env_file: .env environment: - - NEXT_PUBLIC_ENV=production + - NEXT_PUBLIC_ENV=development - NEXT_PUBLIC_WEBSITE_URL=${SITE_URL} + - NEXT_PUBLIC_POSTHOG_HOST=${POSTHOG_HOST} + - NEXT_PUBLIC_POSTHOG_API_KEY=${POSTHOG_PROJECT_API_KEY} + - NEXT_PUBLIC_TELEMETRY_ENABLED=${TELEMETRY_ENABLED} networks: - infisical-dev