From 94477d50b178cf519d1e56f82f31e27cacc223c4 Mon Sep 17 00:00:00 2001 From: Gil Desmarais Date: Sat, 2 Nov 2024 16:21:38 +0100 Subject: [PATCH] feat: optionally allow APM using Sentry via env variable (#696) --- Gemfile | 5 +++++ Gemfile.lock | 6 ++++++ README.md | 6 ++++++ config.ru | 25 ++++++++++++++++++++++++- 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 68f2952..abd532d 100644 --- a/Gemfile +++ b/Gemfile @@ -42,3 +42,8 @@ group :test do gem 'vcr' gem 'webmock' end + +group :sentry do + gem 'sentry-ruby' + gem 'stackprof' +end diff --git a/Gemfile.lock b/Gemfile.lock index c03dcfe..98e185c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -135,6 +135,9 @@ GEM sanitize (6.1.3) crass (~> 1.0.2) nokogiri (>= 1.12.0) + sentry-ruby (5.21.0) + bigdecimal + concurrent-ruby (~> 1.0, >= 1.0.2) simplecov (0.22.0) docile (~> 1.1) simplecov-html (~> 0.11) @@ -142,6 +145,7 @@ GEM simplecov-html (0.12.3) simplecov_json_formatter (0.1.4) ssrf_filter (1.1.2) + stackprof (0.2.26) thor (1.3.2) tilt (2.4.0) tzinfo (2.0.6) @@ -185,8 +189,10 @@ DEPENDENCIES rubocop-rake rubocop-rspec rubocop-thread_safety + sentry-ruby simplecov ssrf_filter + stackprof tilt vcr webmock diff --git a/README.md b/README.md index 29d9339..6619447 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,8 @@ If you're going to host a public instance, _please, please, please_: | `RACK_TIMEOUT_SERVICE_TIMEOUT` | default: 15 | | `WEB_CONCURRENCY` | default: 2 | | `WEB_MAX_THREADS` | default: 5 | +| | | +| `SENTRY_DSN` | no default. | ### Runtime monitoring via `GET /health_check.txt` @@ -181,6 +183,10 @@ Create a monitor of type _Keyword_ with this information and make it aware of yo ![A screenshot showing the Keyword Monitor: a name, the instance's URL to /health_check.txt, and an interval.](docs/uptimerobot_monitor.jpg) +### Application Performance Monitoring using Sentry + +When you specify `SENTRY_DSN` in your environment variables, the application will be setup to use Sentry. + ## Setup for development Check out the git repository and… diff --git a/config.ru b/config.ru index cf3e536..9f084cd 100644 --- a/config.ru +++ b/config.ru @@ -4,8 +4,31 @@ require 'rubygems' require 'bundler/setup' require 'rack-timeout' -dev = ENV.fetch('RACK_ENV', nil) == 'development' +if ENV.key?('SENTRY_DSN') + Bundler.require(:sentry) + require 'sentry-ruby' + + Sentry.init do |config| + config.dsn = ENV.fetch('SENTRY_DSN') + + # Set traces_sample_rate to 1.0 to capture 100% + # of transactions for tracing. + # We recommend adjusting this value in production. + config.traces_sample_rate = 1.0 + # or + # config.traces_sampler = lambda do |_context| + # true + # end + # Set profiles_sample_rate to profile 100% + # of sampled transactions. + # We recommend adjusting this value in production. + config.profiles_sample_rate = 1.0 + end + + use Sentry::Rack::CaptureExceptions +end +dev = ENV.fetch('RACK_ENV', nil) == 'development' requires = Dir['app/**/*.rb'] if dev