Skip to content

Commit

Permalink
fix: reduce sentry trace sample rate (#3974)
Browse files Browse the repository at this point in the history
  • Loading branch information
moroine authored Jan 7, 2025
1 parent 8cb1939 commit 056358e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 15 deletions.
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
36 changes: 24 additions & 12 deletions server/src/common/services/sentry/sentry.ts
Original file line number Diff line number Diff line change
@@ -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 }),
Expand All @@ -19,25 +37,19 @@ 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<void> {
await Sentry.close(2_000);
}

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 })]));
}
2 changes: 1 addition & 1 deletion ui/sentry.client.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion ui/sentry.edge.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion ui/sentry.server.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 056358e

Please sign in to comment.