Skip to content

Commit c76d12b

Browse files
authored
feature: add logging for page views (#115)
even though it is mandatory.... hmmm
1 parent f64cbc1 commit c76d12b

7 files changed

Lines changed: 46 additions & 7 deletions

File tree

apps/web/astar-dev/.env.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ MJ_APIKEY_PRIVATE=
88
CONTACT_EMAIL=
99
MAILJET_FROM_EMAIL=
1010

11-
# leave blank to disable analytics
11+
# Server-side telemetry (Node SDK — read at runtime via process.env)
1212
APPLICATIONINSIGHTS_CONNECTION_STRING=
13+
# Client-side telemetry (browser JS SDK — baked in at build time, safe to expose)
14+
PUBLIC_APPINSIGHTS_CONNECTION_STRING=

apps/web/astar-dev/package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/web/astar-dev/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"vue": "^3.5.0"
2020
},
2121
"devDependencies": {
22+
"@types/node": "^25.5.0",
2223
"typescript": "^5.7.0",
2324
"vitest": "^4.1.2"
2425
},

apps/web/astar-dev/src/lib/telemetry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function getClient(): appInsights.TelemetryClient | null {
1717
return client;
1818
}
1919

20-
const connectionString = import.meta.env.APPLICATIONINSIGHTS_CONNECTION_STRING;
20+
const connectionString = process.env.APPLICATIONINSIGHTS_CONNECTION_STRING;
2121
if (typeof connectionString !== 'string' || connectionString.length === 0) {
2222
return null;
2323
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { defineMiddleware } from 'astro:middleware';
2+
import { trackTrace } from './lib/telemetry';
3+
4+
export const onRequest = defineMiddleware(async (context, next) => {
5+
const start = Date.now();
6+
const response = await next();
7+
const duration = Date.now() - start;
8+
9+
trackTrace('page/view', {
10+
method: context.request.method,
11+
path: context.url.pathname,
12+
status: String(response.status),
13+
durationMs: String(duration),
14+
});
15+
16+
return response;
17+
});

apps/web/astar-dev/src/pages/api/contact.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,10 @@ export const POST: APIRoute = async ({ request }) => {
218218
const message = (body.message as string).trim();
219219
const sendCopy = body.sendCopy === true;
220220

221-
const apiKey = import.meta.env.MJ_APIKEY_PUBLIC;
222-
const apiSecret = import.meta.env.MJ_APIKEY_PRIVATE;
223-
const contactEmail = import.meta.env.CONTACT_EMAIL;
224-
const fromEmail = import.meta.env.MAILJET_FROM_EMAIL;
221+
const apiKey = process.env.MJ_APIKEY_PUBLIC;
222+
const apiSecret = process.env.MJ_APIKEY_PRIVATE;
223+
const contactEmail = process.env.CONTACT_EMAIL;
224+
const fromEmail = process.env.MAILJET_FROM_EMAIL;
225225

226226
if (typeof apiKey !== 'string' || apiKey.length === 0 || typeof apiSecret !== 'string' || apiSecret.length === 0) {
227227
trackException(new Error('contact/missing-mailjet-credentials: MJ_APIKEY_PUBLIC or MJ_APIKEY_PRIVATE is not configured'));

apps/web/astar-dev/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"extends": "astro/tsconfigs/strict",
33
"compilerOptions": {
44
"strictNullChecks": true,
5-
"baseUrl": "."
5+
"baseUrl": ".",
6+
"types": ["node"]
67
}
78
}

0 commit comments

Comments
 (0)