Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -556,14 +556,12 @@ GEM
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
semantic_range (3.0.0)
sentry-rails (5.3.0)
railties (>= 5.0)
sentry-ruby-core (~> 5.3.0)
sentry-ruby (5.3.0)
sentry-rails (6.3.0)
railties (>= 5.2.0)
sentry-ruby (~> 6.3.0)
sentry-ruby (6.3.0)
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
sentry-ruby-core (= 5.3.0)
sentry-ruby-core (5.3.0)
concurrent-ruby
sexp_processor (4.16.0)
shakapacker (6.0.0)
activesupport (>= 5.2)
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/api_passthru_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@ def passthru
url.path = "/api/v3/scenarios/#{params[:id]}/#{params[:rest]}"
url.query = { access_token: identity_access_token.token }.to_query if signed_in?

track_csv_download

redirect_to url.to_s, allow_other_host: true
end

private

def track_csv_download
return unless params[:rest]&.end_with?('.csv')

Sentry.metrics.count(
'csv_download',
attributes: { type: params[:rest] }
)
end

# Allows the browser to make requests to this endpoint only from within ETModel.
def set_cors_headers
url = URI(request.url)
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ApplicationController < ActionController::Base
before_action :ensure_modern_browser

after_action :teardown_current
after_action :set_document_policy_header
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this for every action?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need it according to Sentry docs. For every action? I believe so, in this case since all etmodel requests are basically the UI requests we want to profile.


rescue_from YModel::RecordNotFound do
render_not_found
Expand Down Expand Up @@ -127,6 +128,12 @@ def current_user

private

# Sets the Document-Policy header required for Sentry browser profiling.
# See: https://docs.sentry.io/platforms/javascript/profiling/
def set_document_policy_header
response.headers['Document-Policy'] = 'js-profiling'
end

# Internal: Renders a 404 page.
#
# thing - An optional noun, describing what thing could not be found. Leave
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/scenarios_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ def play_multi_year_charts
end

def inputs
track_csv_download('inputs.csv')

default_values = @scenario.inputs(engine_client)

csv = CSV.generate do |row|
Expand Down Expand Up @@ -206,6 +208,13 @@ def inputs

private

def track_csv_download(type)
Sentry.metrics.count(
'csv_download',
attributes: { type: type }
)
end

# Finds the scenario from id
def find_scenario
@scenario = FetchAPIScenario.call(engine_client, params.require(:id).to_i).or do
Expand Down
7 changes: 7 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def js_globals
json.api_proxy_url Settings.api_proxy_url
json.disable_cors Settings.disable_cors
json.standalone Settings.standalone
json.version Settings.version
json.settings settings_as_json(Current.setting)
json.debug_js admin?
json.env Rails.env
Expand All @@ -163,6 +164,12 @@ def js_globals
else
json.api_session_id nil
end

if Settings.sentry_dsn
json.sentry_dsn Settings.sentry_dsn
json.sentry_traces Settings.sentry_traces
json.sentry_profiles Settings.sentry_profiles
end
end.html_safe
end
end
5 changes: 5 additions & 0 deletions app/javascript/packs/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,8 @@ window.MultiCurveChooserView = MultiCurveChooserView;
import { onClick as saveAsPNGClick } from '../charts/utils/saveAsPNG';

window.BaseChartView.saveAsPNG = saveAsPNGClick;

// Sentry Browser Profiling
// ------------------------

import '../sentry';
32 changes: 32 additions & 0 deletions app/javascript/sentry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as Sentry from '@sentry/browser';

declare const globals: {
sentry_dsn?: string;
sentry_traces?: number;
sentry_profiles?: number;
version?: string;
env?: string;
};

const enabledEnvs = ['production', 'staging'];

if (globals && globals.sentry_dsn && enabledEnvs.includes(globals.env)) {
Sentry.init({
dsn: globals.sentry_dsn,
release: globals.version,
environment: globals.env,
integrations: [
Sentry.browserTracingIntegration(),
Sentry.browserProfilingIntegration(),
],

// Percentage of transactions to capture for tracing
tracesSampleRate: globals.sentry_traces,

// Percentage of sampled transactions to profile
profileSessionSampleRate: globals.sentry_profiles,

// Automatically start/stop profiler based on tracing spans
profileLifecycle: 'trace',
});
}
2 changes: 1 addition & 1 deletion config/initializers/sentry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
if Settings.sentry_dsn
Sentry.init do |config|
# Set release version
config.release = 'stable.01'
config.release = Settings.version
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should start to manually update the string here. Settings.version comes from the docker compose on the (production) server, and will not change when we deploy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the idea of manually setting it here for each release too easy to forget. I think it would be best to come up with a rather automatic way, if not fully automatic perhaps a automatic check during one of our CI/CD steps. And that goes for all our apps actually (at least all apps that use Sentry).

For example, I see a couple of routes depending on what's possible:

  • A: We add another entry Settings.release (to config/settings.yml) reading from a new ENV variable if we find a way to keep that ENV updated with the current "git release tag" (not sure if that is possible both in beta and production).
release: <%= ENV.fetch('RELEASE_TAG', '2026-01') %>
  • B: We populate the Settings.release ourselves and we make sure that during a relevant CI/CD step we corroborate that this value is indeed the current "git release tag" (If its easier for the CI/CD check we could even use a dedicated RELEASE file)
release: <%= File.read(File.join(__dir__, 'RELEASE')).strip rescue '2026-01' %>

Isn't something like this reasonable?


config.dsn = Settings.sentry_dsn
config.enabled_environments = %w[production staging]
Expand Down
6 changes: 4 additions & 2 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ feedback_email: info@energytransitionmodel.com
# Sentry
# ------

# Optionally send error messages to the Sentry service by providing your
# Sentry DSN:
# Optionally send error messages and profiling data to the Sentry service by providing your
# Sentry details:
sentry_dsn: <%= ENV['SENTRY_DSN'] %>
sentry_traces: <%= ENV.fetch('SENTRY_TRACES', 0.1) %>
sentry_profiles: <%= ENV.fetch('SENTRY_PROFILES', 0.1) %>

# Mailchimp
# ---------
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@babel/preset-env": "7",
"@babel/preset-typescript": "^7.22.5",
"@babel/runtime": "7",
"@sentry/browser": "^10.35.0",
"babel-loader": "8",
"babel-plugin-macros": "^3.1.0",
"compression-webpack-plugin": "9",
Expand Down
46 changes: 46 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,52 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@sentry-internal/browser-utils@10.35.0":
version "10.35.0"
resolved "https://registry.yarnpkg.com/@sentry-internal/browser-utils/-/browser-utils-10.35.0.tgz#6f1238fd11800a79b2ec0b99519049ed9d18d271"
integrity sha512-YjVbyqpJu6E6U/BCdOgIUuUQPUDZ7XdFiBYXtGy59xqQB1qSqNfei163hkfnXxIN90csDubxWNrnit+W5Wo/uQ==
dependencies:
"@sentry/core" "10.35.0"

"@sentry-internal/feedback@10.35.0":
version "10.35.0"
resolved "https://registry.yarnpkg.com/@sentry-internal/feedback/-/feedback-10.35.0.tgz#fc4de2357f5f806e68cb4807391a3216b4d803b5"
integrity sha512-h/rtGcgvGtZIY9njxnzHHMzMwFYAYG/UwDaNtpf8jN63JD6cTQDQ8wNWp0arD9gmUr96YjER55BNRRF8oSg6Fw==
dependencies:
"@sentry/core" "10.35.0"

"@sentry-internal/replay-canvas@10.35.0":
version "10.35.0"
resolved "https://registry.yarnpkg.com/@sentry-internal/replay-canvas/-/replay-canvas-10.35.0.tgz#864b4c9f4eeac905415b4054b572f6cf5a2f92c1"
integrity sha512-efaz8ETDLd0rSpoqX4m8fMnq7abzUJAdqeChz9Jdq6OgvHeBgM6tTfqWSes6sFnSCvFUVkdFngZQfgmBxWGuEA==
dependencies:
"@sentry-internal/replay" "10.35.0"
"@sentry/core" "10.35.0"

"@sentry-internal/replay@10.35.0":
version "10.35.0"
resolved "https://registry.yarnpkg.com/@sentry-internal/replay/-/replay-10.35.0.tgz#1bc7c0506231b9e360a7144288ce322550b01b9b"
integrity sha512-9hGP3lD+7o/4ovGTdwv3T9K2t9LxSlR/CAcRQeFApW2c0AGsjTdcglOxsgxYei4YmaISx0CBJ/YqJfQVYxaxWw==
dependencies:
"@sentry-internal/browser-utils" "10.35.0"
"@sentry/core" "10.35.0"

"@sentry/browser@^10.35.0":
version "10.35.0"
resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-10.35.0.tgz#814e6540e031a2904a04735364780dfcd1c42bb8"
integrity sha512-3wCdmKOTqg6Fvmb9HLHzCVIpSSYCPhXFQ95VaYsb1rESIgL7BMS9nyqhecPcPR3oJppU2a/TqZk4YH3nFrPXmA==
dependencies:
"@sentry-internal/browser-utils" "10.35.0"
"@sentry-internal/feedback" "10.35.0"
"@sentry-internal/replay" "10.35.0"
"@sentry-internal/replay-canvas" "10.35.0"
"@sentry/core" "10.35.0"

"@sentry/core@10.35.0":
version "10.35.0"
resolved "https://registry.yarnpkg.com/@sentry/core/-/core-10.35.0.tgz#caf84bdd4ac630e6c37b787029de50736b06f594"
integrity sha512-lEK1WFqt6oHtMq5dDLVE/FDzHDGs1PlYT5cZH4aBirYtJVyUiTf0NknKFob4a2zTywczlq7SbLv6Ba8UMU9dYg==

"@types/body-parser@*":
version "1.19.2"
resolved "https://registry.yarnpkg.com/@types/body-parser/-/body-parser-1.19.2.tgz#aea2059e28b7658639081347ac4fab3de166e6f0"
Expand Down