From 0581c67d6cd618c3f73729436fff10837860b210 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Mon, 22 Apr 2024 16:45:06 +0200 Subject: [PATCH] refactor: extract ApplicationVersion version from sentry --- app/controllers/application_controller.rb | 2 +- config/initializers/application_version.rb | 19 +++++++++++++++++++ config/initializers/sentry.rb | 14 +------------- 3 files changed, 21 insertions(+), 14 deletions(-) create mode 100644 config/initializers/application_version.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9b0a6ffef..32d8c088f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -339,7 +339,7 @@ class ApplicationController < ActionController::Base environment: sentry[:environment], browser: { modern: BrowserSupport.supported?(browser) }, user: sentry_user, - release: SentryRelease.current + release: ApplicationVersion.current } end diff --git a/config/initializers/application_version.rb b/config/initializers/application_version.rb new file mode 100644 index 000000000..c615edc05 --- /dev/null +++ b/config/initializers/application_version.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class ApplicationVersion + @@current = nil + + # Detect the current release version, which helps Sentry identifying the current release + # or can be used as cache key when for some contents susceptible to change between releases. + # + # The deploy process can write a "version" file at root + # containing a string identifying the release, like the sha256 commit used by its release. + # It defaults to a random string if the file is not found (so each restart will behave like a new version) + def self.current + @@current ||= begin + version = Rails.root.join('version') + version.readable? ? version.read.strip : SecureRandom.hex + end + @@current.presence + end +end diff --git a/config/initializers/sentry.rb b/config/initializers/sentry.rb index 473efa285..6273b0f13 100644 --- a/config/initializers/sentry.rb +++ b/config/initializers/sentry.rb @@ -1,15 +1,3 @@ -class SentryRelease - @@current = nil - - def self.current - @@current ||= begin - version = Rails.root.join('version') - version.readable? ? version.read.strip : '' - end - @@current.presence - end -end - Sentry.init do |config| secrets = Rails.application.secrets.sentry @@ -19,7 +7,7 @@ Sentry.init do |config| config.dsn = secrets[:enabled] ? secrets[:rails_client_key] : nil config.send_default_pii = false - config.release = SentryRelease.current + config.release = ApplicationVersion.current config.environment = secrets[:environment] || Rails.env config.enabled_environments = ['production', secrets[:environment].presence].compact config.breadcrumbs_logger = [:active_support_logger]