diff --git a/server/package.json b/server/package.json index 01f3294bd..ff2569f0d 100644 --- a/server/package.json +++ b/server/package.json @@ -24,6 +24,7 @@ "@sentry/cli": "^2.37.0", "@sentry/integrations": "^7.114.0", "@sentry/node": "^7.119.2", + "@sentry/types": "^7.114.0", "@supercharge/promise-pool": "^2.4.0", "@types/boom": "^7.3.5", "@types/multer": "^1.4.12", diff --git a/server/src/common/services/sentry/sentry.ts b/server/src/common/services/sentry/sentry.ts index 3e2ac46b2..1ed442cb8 100644 --- a/server/src/common/services/sentry/sentry.ts +++ b/server/src/common/services/sentry/sentry.ts @@ -1,15 +1,33 @@ import { CaptureConsole, ExtraErrorData } from "@sentry/integrations"; import * as Sentry from "@sentry/node"; +import type { Integration } from "@sentry/types"; import config from "../../../config"; -function getSentryOptions() { +function getSentryOptions(extraIntegrations: Integration[]): Sentry.NodeOptions { return { - tracesSampleRate: config.env === "production" ? 0.1 : 1.0, + tracesSampler: (samplingContext) => { + // Continue trace decision, if there is any parentSampled information + if (samplingContext.parentSampled != null) { + return samplingContext.parentSampled; + } + + if (samplingContext.transactionContext?.op === "queue.item") { + // We want to sample the process effectif transaction at a rate of 1/1_000 + return 1 / 1_000; + } + + if (samplingContext.transactionContext?.op === "processor.job") { + // Sample 100% of processor jobs + return 1.0; + } + + return config.env === "production" ? 0.01 : 1.0; + }, tracePropagationTargets: [/^https:\/\/[^/]*\.apprentissage\.beta\.gouv\.fr/], environment: config.env, release: config.version, - enabled: config.env !== "local" && config.env !== "test", + enabled: true, // config.env !== "local" && config.env !== "test", integrations: [ new Sentry.Integrations.Http({ tracing: true }), new Sentry.Integrations.Mongo({ useMongoose: false }), @@ -19,15 +37,13 @@ function getSentryOptions() { new ExtraErrorData({ depth: 16 }) as any, // eslint-disable-next-line @typescript-eslint/no-explicit-any new Sentry.Integrations.Anr({ captureStackTrace: true }) as any, + ...extraIntegrations, ], }; } export function initSentryProcessor(): void { - Sentry.init({ - ...getSentryOptions(), - tracesSampleRate: 1.0, - }); + Sentry.init(getSentryOptions([])); } export async function closeSentry(): Promise { @@ -35,9 +51,5 @@ export async function closeSentry(): Promise { } export function initSentryExpress(app): void { - const defaultOptions = getSentryOptions(); - Sentry.init({ - ...defaultOptions, - integrations: [...defaultOptions.integrations, new Sentry.Integrations.Express({ app })], - }); + Sentry.init(getSentryOptions([new Sentry.Integrations.Express({ app })])); } diff --git a/ui/sentry.client.config.ts b/ui/sentry.client.config.ts index 70978a63b..237e5f134 100644 --- a/ui/sentry.client.config.ts +++ b/ui/sentry.client.config.ts @@ -14,7 +14,7 @@ import { publicConfig } from "./config.public"; init({ dsn: publicConfig.sentry_dsn, - tracesSampleRate: publicConfig.env === "production" ? 0.1 : 1.0, + tracesSampleRate: publicConfig.env === "production" ? 0.01 : 1.0, tracePropagationTargets: [/^https:\/\/[^/]*\.apprentissage\.beta\.gouv\.fr/, publicConfig.baseUrl, /^\//], environment: publicConfig.env, enabled: publicConfig.env !== "local", diff --git a/ui/sentry.edge.config.ts b/ui/sentry.edge.config.ts index 1bec06097..083121da1 100644 --- a/ui/sentry.edge.config.ts +++ b/ui/sentry.edge.config.ts @@ -10,7 +10,7 @@ import { publicConfig } from "./config.public"; init({ dsn: publicConfig.sentry_dsn, - tracesSampleRate: publicConfig.env === "production" ? 0.1 : 1.0, + tracesSampleRate: publicConfig.env === "production" ? 0.01 : 1.0, tracePropagationTargets: [/^https:\/\/[^/]*\.apprentissage\.beta\.gouv\.fr/, publicConfig.baseUrl], environment: publicConfig.env, enabled: publicConfig.env !== "local", diff --git a/ui/sentry.server.config.ts b/ui/sentry.server.config.ts index a80de9995..d58267e60 100644 --- a/ui/sentry.server.config.ts +++ b/ui/sentry.server.config.ts @@ -9,7 +9,7 @@ import { publicConfig } from "./config.public"; init({ dsn: publicConfig.sentry_dsn, - tracesSampleRate: publicConfig.env === "production" ? 0.1 : 1.0, + tracesSampleRate: publicConfig.env === "production" ? 0.01 : 1.0, tracePropagationTargets: [/^https:\/\/[^/]*\.apprentissage\.beta\.gouv\.fr/, publicConfig.baseUrl], environment: publicConfig.env, enabled: publicConfig.env !== "local", diff --git a/yarn.lock b/yarn.lock index aa0b30176..4922bf9b6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3768,6 +3768,13 @@ __metadata: languageName: node linkType: hard +"@sentry/types@npm:^7.114.0": + version: 7.120.2 + resolution: "@sentry/types@npm:7.120.2" + checksum: 45a46cd13f2b7a0590439c0b98e14b50cd1a0a610d8b5d977934a82336ad88a9fd0efb6a93c86063ae25ae2e587994b6b7ad122e808c82110d71c132cc55b02b + languageName: node + linkType: hard + "@sentry/utils@npm:7.119.2": version: 7.119.2 resolution: "@sentry/utils@npm:7.119.2" @@ -16186,6 +16193,7 @@ __metadata: "@sentry/cli": ^2.37.0 "@sentry/integrations": ^7.114.0 "@sentry/node": ^7.119.2 + "@sentry/types": ^7.114.0 "@supercharge/promise-pool": ^2.4.0 "@types/boom": ^7.3.5 "@types/bunyan": ^1.8.11