Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Fix how we gather and derive sampling traces for sentry (#9395)

Oliver Günther99a8b3f140bd
diff --git a/app/helpers/sentry_helper.rb b/app/helpers/sentry_helper.rb
index 139df36d03c..7770699988d 100644
--- a/app/helpers/sentry_helper.rb
+++ b/app/helpers/sentry_helper.rb
@@ -14,7 +14,7 @@ module SentryHelper
data: {
dsn: OpenProject::Configuration.sentry_frontend_dsn,
version: OpenProject::VERSION.to_s,
- tracing_rate: OpenProject::Configuration.sentry_traces_sample_rate
+ tracing_factor: OpenProject::Configuration.sentry_frontend_trace_factor
}
end
diff --git a/config/initializers/sentry.rb b/config/initializers/sentry.rb
index bb4a4b8a837..3ddb84cee4c 100644
--- a/config/initializers/sentry.rb
+++ b/config/initializers/sentry.rb
@@ -37,10 +37,51 @@ if OpenProject::Logging::SentryLogger.enabled?
# Sample rate for performance
# 0.0 = disabled
# 1.0 = all samples are traced
- config.traces_sample_rate = OpenProject::Configuration.sentry_traces_sample_rate
- config.traces_sampler = lambda do |sampling_context|
- # ignore health checks transactions
- !["/health_checks"].include?(sampling_context[:transaction_context][:name])
+ sample_factor = OpenProject::Configuration.sentry_trace_factor.to_f
+
+ # Define a tracing sample handler
+ trace_sampler = lambda do |sampling_context|
+ # if this is the continuation of a trace, just use that decision (rate controlled by the caller)
+ next sampling_context[:parent_sampled] unless sampling_context[:parent_sampled].nil?
+
+ # transaction_context is the transaction object in hash form
+ # keep in mind that sampling happens right after the transaction is initialized
+ # for example, at the beginning of the request
+ transaction_context = sampling_context[:transaction_context]
+
+ # transaction_context helps you sample transactions with more sophistication
+ # for example, you can provide different sample rates based on the operation or name
+ op = transaction_context[:op]
+ transaction_name = transaction_context[:name]
+
+ rate = case op
+ when /request/
+ case transaction_name
+ when /health_check/
+ 0.0
+ else
+ [0.1 * sample_factor, 1.0].min
+ end
+ when /delayed_job/
+ [0.01 * sample_factor, 1.0].min
+ else
+ 0.0 # ignore all other transactions
+ end
+
+ Rails.logger.debug { "[SENTRY TRACE SAMPLER] Decided on sampling rate #{rate} for #{op}: #{transaction_name} " }
+
+ rate
+ end
+
+ # Assign the sampler conditionally to avoid running the lambda
+ # when we don't trace anyway
+ if sample_factor.zero?
+ Rails.logger.debug { "[SENTRY TRACE SAMPLER] Requested factor is zero, skipping performance tracing" }
+ config.traces_sample_rate = 0
+ config.traces_sampler = nil
+ else
+ Rails.logger.debug { "[SENTRY TRACE SAMPLER] Requested factor is #{sample_factor}, setting up performance tracing" }
+ config.traces_sampler = trace_sampler
end
# Set release info
diff --git a/frontend/src/app/sentry/sentry-reporter.ts b/frontend/src/app/sentry/sentry-reporter.ts
index a55c7a69af1..b9f43555858 100644
--- a/frontend/src/app/sentry/sentry-reporter.ts
+++ b/frontend/src/app/sentry/sentry-reporter.ts
@@ -79,7 +79,7 @@ export class SentryReporter implements ErrorReporter {
private loadSentry(sentryElement:HTMLElement) {
const dsn = sentryElement.dataset.dsn || '';
const version = sentryElement.dataset.version || 'unknown';
- const traceRate = parseFloat(sentryElement.dataset.tracesSampleRate || '0.1');
+ const traceFactor = parseFloat(sentryElement.dataset.tracingFactor || '0.0');
import('./sentry-dependency').then((imported) => {
const sentry = imported.Sentry;
@@ -92,8 +92,17 @@ export class SentryReporter implements ErrorReporter {
// Integrations
integrations: [new imported.Integrations.BrowserTracing()],
- // Tracing rate for performance
- tracesSampleRate: traceRate,
+ tracesSampler: (samplingContext) => {
+ switch (samplingContext.transactionContext.op) {
+ case 'op':
+ case 'navigation':
+ // Trace 1% of page loads and navigation events
+ return Math.min(0.01 * traceFactor, 1.0);
+ default:
+ // Trace 0.1% of requests
+ return Math.min(0.001 * traceFactor, 1.0);
+ }
+ },
ignoreErrors: [
// Transition movements,
diff --git a/lib/open_project/configuration.rb b/lib/open_project/configuration.rb
index 485beb06070..0ca678894e6 100644
--- a/lib/open_project/configuration.rb
+++ b/lib/open_project/configuration.rb
@@ -182,8 +182,13 @@ module OpenProject
# Allow separate error reporting for frontend errors
'sentry_frontend_dsn' => nil,
'sentry_host' => nil,
- # Sample rate for performance monitoring
- 'sentry_traces_sample_rate' => 0.1,
+ # Allow sentry to collect tracing samples
+ # set to 1 to enable default tracing samples (see sentry initializer)
+ # set to n >= 1 to enable n times the default tracing
+ 'sentry_trace_factor' => 0,
+ # Allow sentry to collect tracing samples on frontend
+ # set to n >= 1 to enable n times the default tracing
+ 'sentry_frontend_trace_factor' => 0,
# Allow connection to Augur
'enterprise_trial_creation_host' => 'https://augur.openproject.com',