-
-
Notifications
You must be signed in to change notification settings - Fork 532
feat(yabeda): Add sentry-yabeda adapter gem #2925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dingsdax
wants to merge
11
commits into
master
Choose a base branch
from
feat/sentry-yabeda
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c41fc31
feat(yabeda): Add sentry-yabeda adapter gem
dingsdax 2ad73c0
fix(yabeda): Replace endless method syntax for Ruby 2.7 compatibility
dingsdax f890d48
fix(yabeda): Use ActionController::API in yabeda-mini app
dingsdax 8261a62
Potential fix for code scanning alert no. 22: CSRF protection not ena…
dingsdax 670e40a
chore(yabeda): Remove unused yabeda-mini manual test app
dingsdax 73fd417
ci(yabeda): Add GitHub Actions workflow for sentry-yabeda
dingsdax c51d7ad
docs(yabeda): Add sentry-yabeda to root README and trim per-gem README
dingsdax 07bbe18
docs(yabeda): Add sentry-yabeda to sentry-ruby/README.md
dingsdax 855c165
ref(yabeda): Auto-start collector via Sentry::Configuration callback
dingsdax 39ca600
feat(yabeda): Fire :closed callbacks on Sentry.close for final flush
dingsdax 36ac89a
docs(yabeda): Add How it works section to README
dingsdax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| source "https://rubygems.org" | ||
| git_source(:github) { |name| "https://github.com/#{name}.git" } | ||
|
|
||
| eval_gemfile "../Gemfile.dev" | ||
|
|
||
| # Specify your gem's dependencies in sentry-yabeda.gemspec | ||
| gemspec | ||
|
|
||
| gem "sentry-ruby", path: "../sentry-ruby" | ||
|
|
||
| gem "timecop" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| The MIT License (MIT) | ||
|
|
||
| Copyright (c) 2020 Sentry | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # sentry-yabeda | ||
|
|
||
| A [Yabeda](https://github.com/yabeda-rb/yabeda) adapter that forwards Ruby application metrics to [Sentry](https://sentry.io). | ||
|
|
||
| ## Installation | ||
|
|
||
| Add this line to your application's Gemfile: | ||
|
|
||
| ```ruby | ||
| gem "sentry-yabeda" | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| Require `sentry-yabeda` in your application. If you're using Bundler (most cases), simply adding it to your Gemfile is enough. | ||
|
|
||
| ```ruby | ||
| # config/initializers/sentry.rb | ||
| Sentry.init do |config| | ||
| config.dsn = ENV["SENTRY_DSN"] | ||
| config.enable_metrics = true | ||
| end | ||
|
|
||
| # config/initializers/yabeda.rb (or wherever Yabeda is configured) | ||
| require "sentry-yabeda" | ||
| ``` | ||
|
|
||
| That's it! All Yabeda metrics will automatically flow to Sentry. | ||
|
|
||
| ### Periodic Gauge Collection | ||
|
|
||
| Many Yabeda plugins (puma, gc, gvl\_metrics) measure process-level state using **gauge metrics** with `collect` blocks. These blocks are designed for Prometheus's pull model. A scrape request triggers `Yabeda.collect!`, which reads the current state and sets gauge values. | ||
|
|
||
| In a push-based system like Sentry, there's no scrape request. `sentry-yabeda` solves this with a built-in **periodic collector** that calls `Yabeda.collect!` on a background thread: | ||
|
|
||
| ```ruby | ||
| require "sentry-yabeda" | ||
|
|
||
| # Start the collector (default: every 15 seconds) | ||
| Sentry::Yabeda.start_collector! | ||
|
|
||
| # Or with a custom interval | ||
| Sentry::Yabeda.start_collector!(interval: 30) | ||
|
|
||
| # Stop the collector | ||
| Sentry::Yabeda.stop_collector! | ||
| ``` | ||
|
|
||
| Without starting the collector, only **event-driven metrics** (counters incremented on each request, histograms measured per-operation) will flow to Sentry. Gauges that depend on periodic collection (e.g. GC stats, GVL contention, and Puma thread pool utilization) require the collector. | ||
|
|
||
| ** How it works ** | ||
|
|
||
| Every 15s (or set interval) | ||
| 1. Collector calls Yabeda.collect! | ||
| 2. Plugin collect blocks fire (read GC.stat, fetch Puma /stats, etc.) | ||
| 3. gauge.set(value) calls flow through the adapter | ||
| 4. Sentry.metrics.gauge(name, value, attributes: tags) | ||
| 5. Sentry buffers and sends in the next envelope flush | ||
|
|
||
| ### Metric Type Mapping | ||
|
|
||
| | Yabeda Type | Sentry Type | | ||
| |-------------|-------------| | ||
| | Counter | `Sentry.metrics.count` | | ||
| | Gauge | `Sentry.metrics.gauge` | | ||
| | Histogram | `Sentry.metrics.distribution` | | ||
| | Summary | `Sentry.metrics.distribution` | | ||
|
|
||
| ### Tags | ||
|
|
||
| Yabeda tags are passed directly as Sentry metric attributes, enabling filtering and grouping in the Sentry UI. | ||
|
|
||
| ### Metric Naming | ||
|
|
||
| Metrics are named using the pattern `{group}.{name}` (e.g., `rails.request_duration`). Metrics without a group use just the name. | ||
|
|
||
| ### Trace Integration | ||
|
|
||
| Since Sentry metrics carry trace context automatically, metrics emitted via the adapter are connected to active traces when `sentry-rails` or other Sentry integrations are active. This enables pivoting from metric spikes to relevant traces in the Sentry UI. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "bundler/gem_tasks" | ||
| require_relative "../lib/sentry/test/rake_tasks" | ||
|
|
||
| Sentry::Test::RakeTasks.define_spec_tasks( | ||
| spec_pattern: "spec/sentry/**/*_spec.rb", | ||
| spec_rspec_opts: "--order rand --format progress" | ||
| ) | ||
|
|
||
| task default: :spec |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "yabeda" | ||
| require "sentry-ruby" | ||
| require "sentry/integrable" | ||
| require "sentry/yabeda/version" | ||
| require "sentry/yabeda/adapter" | ||
| require "sentry/yabeda/collector" | ||
|
|
||
| module Sentry | ||
| module Yabeda | ||
| extend Sentry::Integrable | ||
|
|
||
| register_integration name: "yabeda", version: Sentry::Yabeda::VERSION | ||
|
|
||
| class << self | ||
| attr_accessor :collector | ||
|
|
||
| # Start periodic collection of Yabeda gauge metrics. | ||
| # Call this after Sentry.init to begin pushing runtime metrics | ||
| # (GC, GVL, Puma stats, etc.) to Sentry. | ||
| def start_collector!(interval: Collector::DEFAULT_INTERVAL) | ||
| raise ArgumentError, "call start_collector! after Sentry.init" unless Sentry.initialized? | ||
|
|
||
| @collector&.kill | ||
| @collector = Collector.new(interval: interval) | ||
| end | ||
|
|
||
| def stop_collector! | ||
| @collector&.kill | ||
| @collector = nil | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| ::Yabeda.register_adapter(:sentry, Sentry::Yabeda::Adapter.new) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "yabeda/base_adapter" | ||
|
|
||
| module Sentry | ||
| module Yabeda | ||
| class Adapter < ::Yabeda::BaseAdapter | ||
| # Sentry does not require pre-registration of metrics | ||
| def register_counter!(_metric); end | ||
| def register_gauge!(_metric); end | ||
| def register_histogram!(_metric); end | ||
| def register_summary!(_metric); end | ||
|
|
||
| def perform_counter_increment!(counter, tags, increment) | ||
| return unless enabled? | ||
|
|
||
| Sentry.metrics.count( | ||
| metric_name(counter), | ||
| value: increment, | ||
| attributes: attributes_for(tags) | ||
| ) | ||
| end | ||
|
|
||
| def perform_gauge_set!(gauge, tags, value) | ||
| return unless enabled? | ||
|
|
||
| Sentry.metrics.gauge( | ||
| metric_name(gauge), | ||
| value, | ||
| unit: unit_for(gauge), | ||
| attributes: attributes_for(tags) | ||
| ) | ||
| end | ||
|
|
||
| def perform_histogram_measure!(histogram, tags, value) | ||
| return unless enabled? | ||
|
|
||
| Sentry.metrics.distribution( | ||
| metric_name(histogram), | ||
| value, | ||
| unit: unit_for(histogram), | ||
| attributes: attributes_for(tags) | ||
| ) | ||
| end | ||
|
|
||
| def perform_summary_observe!(summary, tags, value) | ||
| return unless enabled? | ||
|
|
||
| Sentry.metrics.distribution( | ||
| metric_name(summary), | ||
| value, | ||
| unit: unit_for(summary), | ||
| attributes: attributes_for(tags) | ||
| ) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def enabled? | ||
| Sentry.initialized? && Sentry.configuration.enable_metrics | ||
| end | ||
|
|
||
| def attributes_for(tags) | ||
| tags.empty? ? nil : tags | ||
| end | ||
|
|
||
| def metric_name(metric) | ||
| [metric.group, metric.name].compact.join(".") | ||
| end | ||
|
|
||
| # TODO: Normalize Yabeda unit symbols (e.g. :milliseconds) to Sentry's | ||
| # canonical singular strings (e.g. "millisecond") once units are visible | ||
| # in the Sentry product. See https://develop.sentry.dev/sdk/foundations/state-management/scopes/attributes/#units | ||
| def unit_for(metric) | ||
| metric.unit&.to_s | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "sentry/threaded_periodic_worker" | ||
|
|
||
| module Sentry | ||
| module Yabeda | ||
| # Periodically calls Yabeda.collect! to trigger gauge collection blocks | ||
| # registered by plugins like yabeda-puma-plugin, yabeda-gc, and | ||
| # yabeda-gvl_metrics. | ||
| # | ||
| # In a pull-based system (Prometheus), the scrape request triggers | ||
| # collection. In a push-based system (Sentry), we need this periodic | ||
| # worker to drive the collect → gauge.set → adapter.perform_gauge_set! | ||
| # pipeline. | ||
| class Collector < Sentry::ThreadedPeriodicWorker | ||
| DEFAULT_INTERVAL = 15 # seconds | ||
|
|
||
| def initialize(interval: DEFAULT_INTERVAL) | ||
| super(Sentry.sdk_logger, interval) | ||
| ensure_thread | ||
| end | ||
|
|
||
| def run | ||
| ::Yabeda.collect! | ||
| rescue => e | ||
| log_warn("[Sentry::Yabeda::Collector] collection failed: #{e.message}") | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Sentry | ||
| module Yabeda | ||
| VERSION = "6.5.0" | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative "lib/sentry/yabeda/version" | ||
|
|
||
| Gem::Specification.new do |spec| | ||
| spec.name = "sentry-yabeda" | ||
| spec.version = Sentry::Yabeda::VERSION | ||
| spec.authors = ["Sentry Team"] | ||
| spec.description = spec.summary = "A gem that provides Yabeda integration for the Sentry error logger" | ||
| spec.email = "accounts@sentry.io" | ||
| spec.license = 'MIT' | ||
|
|
||
| spec.platform = Gem::Platform::RUBY | ||
| spec.required_ruby_version = '>= 2.7' | ||
| spec.extra_rdoc_files = ["README.md", "LICENSE.txt"] | ||
| spec.files = `git ls-files | grep -Ev '^(spec|benchmarks|examples|\.rubocop\.yml)'`.split("\n") | ||
|
|
||
| github_root_uri = 'https://github.com/getsentry/sentry-ruby' | ||
| spec.homepage = "#{github_root_uri}/tree/#{spec.version}/#{spec.name}" | ||
|
|
||
| spec.metadata = { | ||
| "homepage_uri" => spec.homepage, | ||
| "source_code_uri" => spec.homepage, | ||
| "changelog_uri" => "#{github_root_uri}/blob/#{spec.version}/CHANGELOG.md", | ||
| "bug_tracker_uri" => "#{github_root_uri}/issues", | ||
| "documentation_uri" => "http://www.rubydoc.info/gems/#{spec.name}/#{spec.version}" | ||
| } | ||
|
|
||
| spec.bindir = "exe" | ||
| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } | ||
| spec.require_paths = ["lib"] | ||
|
|
||
| spec.add_dependency "sentry-ruby", "~> 6.5" | ||
| spec.add_dependency "yabeda", ">= 0.11" | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.