Adds Sentry and Umami monitoring to the site
Continuous Integration / lighthouse (pull_request) Canceled after 0s
Continuous Integration / deploy-test (pull_request) Canceled after 0s
Continuous Integration / deploy-production (pull_request) Canceled after 0s
Continuous Integration / check (pull_request) Canceled after 2m22s
Continuous Integration / lighthouse (pull_request) Canceled after 0s
Continuous Integration / deploy-test (pull_request) Canceled after 0s
Continuous Integration / deploy-production (pull_request) Canceled after 0s
Continuous Integration / check (pull_request) Canceled after 2m22s
Sentry loads after the page and reports errors only, without personal data; Umami counts visits per environment. Both only run on andreaslop.nl and test.andreaslop.nl. The CSP allows just these two hosts, and builds carry the commit SHA as release. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import fc from 'fast-check';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
monitoredHostnames,
|
||||
monitoringFor,
|
||||
sentryIngestOrigin,
|
||||
} from './config';
|
||||
|
||||
// Alles wat geen test- of productiehost is
|
||||
const otherHostname = fc
|
||||
.oneof(
|
||||
fc.domain(),
|
||||
fc.ipV4(),
|
||||
fc.ipV6(),
|
||||
fc.string(),
|
||||
fc.constantFrom(
|
||||
'localhost',
|
||||
'127.0.0.1',
|
||||
'www.andreaslop.nl',
|
||||
'andreaslop.nl.',
|
||||
'test.andreaslop.nl.evil.com',
|
||||
'__proto__',
|
||||
'constructor',
|
||||
),
|
||||
)
|
||||
.filter((host) => !monitoredHostnames.includes(host));
|
||||
|
||||
describe('monitoringFor', () => {
|
||||
it('productiehost meet als production met de productiewebsite', () => {
|
||||
expect(monitoringFor('andreaslop.nl')).toEqual({
|
||||
environment: 'production',
|
||||
umamiWebsiteId: '3edbb7d1-4388-4f2d-9cef-11ade88ace96',
|
||||
});
|
||||
});
|
||||
|
||||
it('testhost meet als test met de testwebsite', () => {
|
||||
expect(monitoringFor('test.andreaslop.nl')).toEqual({
|
||||
environment: 'test',
|
||||
umamiWebsiteId: '7fb301ab-6ecc-4aea-beee-71bcb6009bfe',
|
||||
});
|
||||
});
|
||||
|
||||
it('elke andere host meet niets (M1)', () => {
|
||||
fc.assert(
|
||||
fc.property(otherHostname, (host) => {
|
||||
expect(monitoringFor(host)).toBeNull();
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('Sentry-ingest is de EU-host uit de DSN, zonder sleutel', () => {
|
||||
expect(sentryIngestOrigin).toBe(
|
||||
'https://o4511795618185216.ingest.de.sentry.io',
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,43 @@
|
||||
// Instellingen voor Sentry en Umami (monitoring-plan M1/M2). Deze waarden zijn
|
||||
// openbaar: ze staan hoe dan ook in de browser van elke bezoeker.
|
||||
// Geen imports uit Vite: config/securityHeaders.ts leest dit bestand ook in Node.
|
||||
|
||||
export type MonitoringEnvironment = 'test' | 'production';
|
||||
|
||||
export interface Monitoring {
|
||||
environment: MonitoringEnvironment;
|
||||
umamiWebsiteId: string;
|
||||
}
|
||||
|
||||
export const sentryDsn =
|
||||
'https://12ec77dcc3216a98b96d3fb3dac13f66@o4511795618185216.ingest.de.sentry.io/4512282287472720';
|
||||
|
||||
// Waar de browser fouten naartoe stuurt; nodig voor connect-src in de CSP
|
||||
export const sentryIngestOrigin = new URL(sentryDsn).origin;
|
||||
|
||||
export const umamiOrigin = 'https://analytics.slpsoftware.nl';
|
||||
|
||||
// Eén build voor beide omgevingen: de hostname bepaalt de omgeving
|
||||
const monitoringByHostname = new Map<string, Monitoring>([
|
||||
[
|
||||
'andreaslop.nl',
|
||||
{
|
||||
environment: 'production',
|
||||
umamiWebsiteId: '3edbb7d1-4388-4f2d-9cef-11ade88ace96',
|
||||
},
|
||||
],
|
||||
[
|
||||
'test.andreaslop.nl',
|
||||
{
|
||||
environment: 'test',
|
||||
umamiWebsiteId: '7fb301ab-6ecc-4aea-beee-71bcb6009bfe',
|
||||
},
|
||||
],
|
||||
]);
|
||||
|
||||
// Onbekende host (localhost, Lighthouse CI, previews): niets meten (M1)
|
||||
export function monitoringFor(hostname: string): Monitoring | null {
|
||||
return monitoringByHostname.get(hostname) ?? null;
|
||||
}
|
||||
|
||||
export const monitoredHostnames = [...monitoringByHostname.keys()];
|
||||
@@ -0,0 +1,99 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const init = vi.fn();
|
||||
const captureException = vi.fn();
|
||||
vi.mock('@sentry/react', () => ({ init, captureException }));
|
||||
|
||||
// De module houdt zijn toestand bij; elke test begint met een verse kopie
|
||||
async function freshSentry() {
|
||||
vi.resetModules();
|
||||
return import('./sentry');
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
init.mockReset();
|
||||
captureException.mockReset();
|
||||
});
|
||||
|
||||
describe('Sentry', () => {
|
||||
it('start niet en stuurt niets op een onbekende host', async () => {
|
||||
const { startSentry, reportError } = await freshSentry();
|
||||
startSentry('localhost');
|
||||
reportError(new Error('lokaal'));
|
||||
|
||||
await Promise.resolve();
|
||||
expect(init).not.toHaveBeenCalled();
|
||||
expect(captureException).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('start op test met omgeving test en zonder persoonsgegevens', async () => {
|
||||
const { startSentry } = await freshSentry();
|
||||
startSentry('test.andreaslop.nl');
|
||||
|
||||
await vi.waitFor(() => expect(init).toHaveBeenCalledOnce());
|
||||
expect(init.mock.calls[0][0]).toMatchObject({
|
||||
environment: 'test',
|
||||
dataCollection: {
|
||||
userInfo: false,
|
||||
cookies: false,
|
||||
httpHeaders: false,
|
||||
httpBodies: [],
|
||||
urlQueryParams: false,
|
||||
},
|
||||
});
|
||||
expect(init.mock.calls[0][0]).not.toHaveProperty('tracesSampleRate');
|
||||
});
|
||||
|
||||
it('fouten van voor de start gaan mee zodra Sentry klaar is', async () => {
|
||||
const { startSentry, reportError } = await freshSentry();
|
||||
startSentry('andreaslop.nl');
|
||||
const early = new Error('vroeg');
|
||||
reportError(early);
|
||||
expect(captureException).not.toHaveBeenCalled();
|
||||
|
||||
await vi.waitFor(() =>
|
||||
expect(captureException).toHaveBeenCalledWith(early),
|
||||
);
|
||||
const later = new Error('later');
|
||||
reportError(later);
|
||||
expect(captureException).toHaveBeenLastCalledWith(later);
|
||||
expect(init.mock.calls[0][0]).toMatchObject({
|
||||
environment: 'production',
|
||||
});
|
||||
});
|
||||
|
||||
it('vangt onafgevangen fouten tijdens het laden op', async () => {
|
||||
const { startSentry } = await freshSentry();
|
||||
startSentry('andreaslop.nl');
|
||||
const error = new Error('onafgevangen');
|
||||
window.dispatchEvent(new ErrorEvent('error', { error }));
|
||||
|
||||
await vi.waitFor(() =>
|
||||
expect(captureException).toHaveBeenCalledWith(error),
|
||||
);
|
||||
});
|
||||
|
||||
it('wacht op het load-event als de pagina nog laadt', async () => {
|
||||
const readyState = vi
|
||||
.spyOn(document, 'readyState', 'get')
|
||||
.mockReturnValue('loading');
|
||||
const { startSentry } = await freshSentry();
|
||||
startSentry('andreaslop.nl');
|
||||
|
||||
await Promise.resolve();
|
||||
expect(init).not.toHaveBeenCalled();
|
||||
|
||||
readyState.mockReturnValue('complete');
|
||||
window.dispatchEvent(new Event('load'));
|
||||
await vi.waitFor(() => expect(init).toHaveBeenCalledOnce());
|
||||
});
|
||||
|
||||
it('start maar één keer', async () => {
|
||||
const { startSentry } = await freshSentry();
|
||||
startSentry('andreaslop.nl');
|
||||
startSentry('andreaslop.nl');
|
||||
|
||||
await vi.waitFor(() => expect(init).toHaveBeenCalled());
|
||||
expect(init).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
import { monitoringFor, sentryDsn } from './config';
|
||||
|
||||
// Sentry laadt pas na het load-event, buiten de hoofdbundel (M4). Fouten van
|
||||
// daarvoor wachten in een wachtrij en gaan mee zodra Sentry klaar is.
|
||||
|
||||
type State =
|
||||
| { kind: 'off' }
|
||||
| { kind: 'loading'; queue: unknown[] }
|
||||
| { kind: 'ready'; capture: (error: unknown) => void };
|
||||
|
||||
let state: State = { kind: 'off' };
|
||||
|
||||
export function reportError(error: unknown): void {
|
||||
if (state.kind === 'ready') state.capture(error);
|
||||
else if (state.kind === 'loading') state.queue.push(error);
|
||||
}
|
||||
|
||||
// Onafgevangen fouten tijdens het laden; daarna vangt Sentry ze zelf
|
||||
const onError = (event: ErrorEvent) => reportError(event.error);
|
||||
const onRejection = (event: PromiseRejectionEvent) => reportError(event.reason);
|
||||
|
||||
export function startSentry(hostname = window.location.hostname): void {
|
||||
const monitoring = monitoringFor(hostname);
|
||||
if (!monitoring || state.kind !== 'off') return;
|
||||
|
||||
const queue: unknown[] = [];
|
||||
state = { kind: 'loading', queue };
|
||||
window.addEventListener('error', onError);
|
||||
window.addEventListener('unhandledrejection', onRejection);
|
||||
|
||||
const load = async () => {
|
||||
const Sentry = await import('./sentryClient');
|
||||
// Alleen fouten: geen tracing, geen replay, geen persoonsgegevens (M5).
|
||||
// Sinds SDK 11 verzamelt Sentry standaard alles; daarom alles expliciet uit.
|
||||
Sentry.init({
|
||||
dsn: sentryDsn,
|
||||
environment: monitoring.environment,
|
||||
release: import.meta.env.VITE_RELEASE,
|
||||
dataCollection: {
|
||||
userInfo: false,
|
||||
cookies: false,
|
||||
httpHeaders: false,
|
||||
httpBodies: [],
|
||||
urlQueryParams: false,
|
||||
},
|
||||
});
|
||||
window.removeEventListener('error', onError);
|
||||
window.removeEventListener('unhandledrejection', onRejection);
|
||||
state = { kind: 'ready', capture: Sentry.captureException };
|
||||
queue.forEach(reportError);
|
||||
};
|
||||
|
||||
if (document.readyState === 'complete') void load();
|
||||
else window.addEventListener('load', () => void load(), { once: true });
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// Alleen wat sentry.ts gebruikt. Een dynamische import van '@sentry/react'
|
||||
// zelf haalt de hele SDK binnen; via losse named exports kan Vite de rest
|
||||
// weglaten (tree-shaking).
|
||||
export { captureException, init } from '@sentry/react';
|
||||
Reference in New Issue
Block a user