Skip to content
Draft
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
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ Gemfile.lock
node_modules
.vite

.DS_Store

mise.toml

.devcontainer/.env
vendor/gems

sentry-rails/Gemfile-*.lock
mise.toml

sentry-yabeda/.DS_Store
sentry-yabeda/.rspec_status
sentry-yabeda/Gemfile-*.lock
13 changes: 13 additions & 0 deletions sentry-yabeda/Gemfile
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"
21 changes: 21 additions & 0 deletions sentry-yabeda/LICENSE.txt
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.
79 changes: 79 additions & 0 deletions sentry-yabeda/README.md
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.
11 changes: 11 additions & 0 deletions sentry-yabeda/Rakefile
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
37 changes: 37 additions & 0 deletions sentry-yabeda/lib/sentry-yabeda.rb
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)
79 changes: 79 additions & 0 deletions sentry-yabeda/lib/sentry/yabeda/adapter.rb
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
30 changes: 30 additions & 0 deletions sentry-yabeda/lib/sentry/yabeda/collector.rb
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
7 changes: 7 additions & 0 deletions sentry-yabeda/lib/sentry/yabeda/version.rb
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
35 changes: 35 additions & 0 deletions sentry-yabeda/sentry-yabeda.gemspec
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
Loading
Loading