Skip to content

feat(yabeda): Add sentry-yabeda adapter gem#2925

Draft
dingsdax wants to merge 3 commits intomasterfrom
feat/sentry-yabeda
Draft

feat(yabeda): Add sentry-yabeda adapter gem#2925
dingsdax wants to merge 3 commits intomasterfrom
feat/sentry-yabeda

Conversation

@dingsdax
Copy link
Copy Markdown
Contributor

@dingsdax dingsdax commented Mar 31, 2026

Sentry Ruby gem that connects Yabeda metrics to Sentry Metrics.

What it does

  • Translates Yabeda metrics into Sentry:
    • counter ➡️ Sentry.metrics.count
    • gauge ➡️ Sentry.metrics.gauge
    • histogram / summary ➡️ Sentry.metrics.distribution
  • Adds a background worker that periodically calls Yabeda.collect!
    (needed because Yabeda is pull-based, while Sentry is push-based).
  • Provides start_collector! and stop_collector! to control this worker (must run after Sentry.init).
  • Histograms and summaries are both treated as distributions.
  • The adapter is automatically registered when required.
  • Unit handling is basic for now, with plans to improve formatting later (we don't show units in Sentry yet)

Fixes RUBY-161
Fixes #2899

Introduces sentry-yabeda, a Yabeda adapter that forwards metrics to
Sentry. Covers all four Yabeda metric types (counter, gauge, histogram,
summary), a periodic collector to drive gauge collection in push-based
environments, and a full spec suite including unit and integration tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@linear-code
Copy link
Copy Markdown

linear-code bot commented Mar 31, 2026

Endless method syntax (def m() = val) requires Ruby 3.0+. Replace with
conventional empty method bodies (def m; end) so RuboCop using the Ruby
2.7 parser does not reject the file.

Co-Authored-By: Claude Sonnet 4.6 <noreply@example.com>
@dingsdax dingsdax requested review from sl0thentr0py and solnic March 31, 2026 09:01
The app is configured as api_only but inherited from ActionController::Base,
which includes CSRF protection middleware. Switch to ActionController::API
to align with the api_only setting and eliminate the CSRF warning.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Comment on lines +103 to +121
class ApplicationController < ActionController::API
around_action :track_metrics

private

def track_metrics
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
yield
ensure
duration_ms = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1000
Yabeda.app.request_duration.measure(
{ controller: controller_name, action: action_name },
duration_ms
)
Yabeda.app.requests_total.increment(
{ controller: controller_name, action: action_name, status: response.status.to_s }
)
end
end

Check failure

Code scanning / CodeQL

CSRF protection not enabled High test

Potential CSRF vulnerability due to forgery protection not being enabled.

Copilot Autofix

AI about 3 hours ago

In general, to fix this type of issue in a Rails app you either (1) enable CSRF protection on controllers that are used with browser sessions, typically via protect_from_forgery with: :exception, or (2) if the app is a stateless API where CSRF is not applicable (e.g., no cookie auth), you make that explicit by inheriting from ActionController::API and disabling session/cookie use, or by documenting/structuring the API to use header‑based tokens instead. For a hybrid or small app, the safest approach when unsure is to enable CSRF protection in the base controller and, if necessary, skip it on specific JSON endpoints that are truly stateless.

For this specific file, the minimal change that addresses CodeQL’s concern without altering existing runtime behavior more than necessary is to (a) add protect_from_forgery with: :exception to ApplicationController and (b) because ApplicationController currently inherits from ActionController::API, explicitly include ActionController::RequestForgeryProtection to make protect_from_forgery available and functional. This keeps the app API‑only (no automatic cookie/session middleware is introduced by changing the superclass) but still provides CSRF protection for any endpoints that do use cookies/session. If, in the broader project, some controllers or actions should remain exempt, they can individually call skip_forgery_protection, but we will not assume that here.

Concretely:

  • Edit spec/apps/yabeda-mini/app.rb where ApplicationController is defined.
  • Inside ApplicationController, include ActionController::RequestForgeryProtection.
  • Add a protect_from_forgery with: :exception call (at the top of the class, before filters).
  • Keep the existing around_action :track_metrics and all other logic unchanged.

No other files need changes based on the provided snippet.

Suggested changeset 1
spec/apps/yabeda-mini/app.rb

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/spec/apps/yabeda-mini/app.rb b/spec/apps/yabeda-mini/app.rb
--- a/spec/apps/yabeda-mini/app.rb
+++ b/spec/apps/yabeda-mini/app.rb
@@ -101,6 +101,8 @@
 
 # Controllers
 class ApplicationController < ActionController::API
+  include ActionController::RequestForgeryProtection
+  protect_from_forgery with: :exception
   around_action :track_metrics
 
   private
EOF
@@ -101,6 +101,8 @@

# Controllers
class ApplicationController < ActionController::API
include ActionController::RequestForgeryProtection
protect_from_forgery with: :exception
around_action :track_metrics

private
Copilot is powered by AI and may make mistakes. Always verify output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Yabeda adapter for Sentry Metrics

2 participants