Skip to content

Commit

Permalink
Backend: add support for analytics (log which host name responds)
Browse files Browse the repository at this point in the history
  • Loading branch information
enricoros committed Dec 5, 2023
1 parent 31684c2 commit 1b4a8da
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
9 changes: 5 additions & 4 deletions docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ which take place over _defaults_. This file is kept in sync with [`../src/server

Environment variables can be set by creating a `.env` file in the root directory of the project.

> For Docker deployment, ensure all necessary environment variables are set **both during build and run**.
> If the Docker container is built without setting environment variables, the frontend UI will be unaware
> of them, despite the backend being able to use them at runtime.
The following is an example `.env` for copy-paste convenience:

```bash
Expand Down Expand Up @@ -45,6 +41,9 @@ GOOGLE_CLOUD_API_KEY=
GOOGLE_CSE_ID=
# Browse
PUPPETEER_WSS_ENDPOINT=

# Backend Analytics
BACKEND_ANALYTICS=
```

## Variables Documentation
Expand Down Expand Up @@ -108,6 +107,8 @@ Enable the app to Talk, Draw, and Google things up.
| `PRODIA_API_KEY` | Prodia API Key - used with '/imagine ...' |
| **Browse** | |
| `PUPPETEER_WSS_ENDPOINT` | Puppeteer WebSocket endpoint - used for browsing, etc. |
| **Backend** | |
| `BACKEND_ANALYTICS` | Semicolon-separated list of analytics flags (see backend.analytics.ts). Flags: `domain` logs the responding domain. |

---

Expand Down
23 changes: 23 additions & 0 deletions src/modules/backend/backend.analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { track } from '@vercel/analytics/server';

import { env } from '~/server/env.mjs';


// all the backend analytics flags
type BackendAnalyticsFlag =
| 'domain'; // logs which domain the initial (capabilities) request is sent to


const checkAnalyticsFlag = (flag: BackendAnalyticsFlag): boolean =>
env.BACKEND_ANALYTICS?.includes(flag) || false;


export function analyticsListCapabilities(backendHostName: string) {
if (checkAnalyticsFlag('domain')) {
// Note: fire-and-forget
void track('backend-domain', {
hostname: backendHostName,
vercel_url: process.env.VERCEL_URL || 'no-vercel',
});
}
}
5 changes: 4 additions & 1 deletion src/modules/backend/backend.router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createTRPCRouter, publicProcedure } from '~/server/api/trpc.server';
import { env } from '~/server/env.mjs';

import { analyticsListCapabilities } from './backend.analytics';


/**
* This is the primary router for the backend. Mainly, this deals with letting
Expand All @@ -12,7 +14,8 @@ export const backendRouter = createTRPCRouter({

/* List server-side capabilities (pre-configured by the deployer) */
listCapabilities: publicProcedure
.query(async () => {
.query(async ({ ctx }) => {
analyticsListCapabilities(ctx.hostName);
return {
hasDB: !!env.POSTGRES_PRISMA_URL && !!env.POSTGRES_URL_NON_POOLING,
hasBrowsing: !!env.PUPPETEER_WSS_ENDPOINT,
Expand Down
7 changes: 5 additions & 2 deletions src/server/api/trpc.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ import { ZodError } from 'zod';
* These allow you to access things when processing a request, like the database, the session, etc.
*/

export const createTRPCFetchContext = ({ /*req, resHeaders*/ }: { req: Request; resHeaders: Headers; }) => {
export const createTRPCFetchContext = ({ req /*, resHeaders*/ }: { req: Request; resHeaders: Headers; }) => {
// const user = { name: req.headers.get('username') ?? 'anonymous' };
// return { req, resHeaders };
return {};
return {
// only used by Backend Analytics
hostName: req.headers?.get('host') ?? 'localhost',
};
};


Expand Down
7 changes: 5 additions & 2 deletions src/server/env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { z } from 'zod';
export const env = createEnv({
server: {

// Postgres, for optional storage via Prisma
// Backend Postgres, for optional storage via Prisma
POSTGRES_PRISMA_URL: z.string().url().optional(),
POSTGRES_URL_NON_POOLING: z.string().url().optional(),

Expand Down Expand Up @@ -44,7 +44,10 @@ export const env = createEnv({

// Browsing Service
PUPPETEER_WSS_ENDPOINT: z.string().url().optional(),


// Backend: Analytics flags (e.g. which hostname responds) for managed installs
BACKEND_ANALYTICS: z.string().optional().transform(list => (list || '').split(';').filter(flag => !!flag)),

},

onValidationError: error => {
Expand Down

0 comments on commit 1b4a8da

Please sign in to comment.