Skip to content
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

feat: add aggregated metrics for rails and more #630

Merged
merged 7 commits into from
Nov 8, 2024
Merged
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
43 changes: 42 additions & 1 deletion lib/honeybadger/config/defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Boolean; end
'Sidekiq::JobRetry::Skip'].map(&:freeze).freeze

IGNORE_EVENTS_DEFAULT = [
{ event_type: 'metric.hb', metric_name: 'duration.sql.active_record', query: /^(begin|commit)( transaction)?$/i },
{ event_type: 'sql.active_record', query: /^(begin|commit)( transaction)?$/i },
{ event_type: 'sql.active_record', query: /(solid_queue|good_job)/i },
{ event_type: 'sql.active_record', name: /^GoodJob/ },
Expand Down Expand Up @@ -363,6 +364,36 @@ class Boolean; end
default: 60,
type: Integer
},
:'sidekiq.insights.enabled' => {
description: 'Enable automatic data collection for Sidekiq.',
default: true,
type: Boolean
},
:'sidekiq.insights.events' => {
description: 'Enable automatic event capturing for Sidekiq.',
default: true,
type: Boolean
},
:'sidekiq.insights.metrics' => {
description: 'Enable automatic metric data collection for Sidekiq.',
default: false,
type: Boolean
},
:'rails.insights.enabled' => {
description: 'Enable automatic data collection for Ruby on Rails.',
default: true,
type: Boolean
},
:'rails.insights.events' => {
description: 'Enable automatic event capturing for Ruby on Rails.',
default: true,
type: Boolean
},
:'rails.insights.metrics' => {
description: 'Enable automatic metric data collection for Ruby on Rails.',
default: false,
type: Boolean
},
:'karafka.insights.enabled' => {
description: 'Enable automatic data collection for Karafka.',
default: true,
Expand All @@ -375,14 +406,24 @@ class Boolean; end
},
:'karafka.insights.metrics' => {
description: 'Enable automatic metric data collection for Karafka.',
default: true,
default: false,
type: Boolean
},
:'net_http.insights.enabled' => {
description: 'Allow automatic instrumentation of Net::HTTP requests.',
default: true,
type: Boolean
},
:'net_http.insights.events' => {
description: 'Enable automatic event capturing for Net::HTTP requests.',
default: true,
type: Boolean
},
:'net_http.insights.metrics' => {
description: 'Enable automatic metric data collection for Net::HTTP requests.',
default: false,
type: Boolean
},
:'net_http.insights.full_url' => {
description: 'Record the full request url during instrumentation.',
default: false,
Expand Down
1 change: 1 addition & 0 deletions lib/honeybadger/gauge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def record(value)
def payloads
[
{
total: @total,
min: @min,
max: @max,
avg: @avg,
Expand Down
1 change: 1 addition & 0 deletions lib/honeybadger/histogram.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def bins

def payloads
[{
total: @total,
min: @min,
max: @max,
avg: @avg,
Expand Down
2 changes: 1 addition & 1 deletion lib/honeybadger/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def gauge(name, *args)
elsif block_given?
value = yield
else
value = attributes.delete(:value)
value = attributes.delete(:duration) || attributes.delete(:value)
end

Honeybadger::Gauge.register(registry, name, attributes).tap do |gauge|
Expand Down
42 changes: 25 additions & 17 deletions lib/honeybadger/notification_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

module Honeybadger
class NotificationSubscriber
include Honeybadger::InstrumentationHelper

def start(name, id, payload)
@start_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
end
Expand All @@ -21,7 +23,29 @@ def finish(name, id, payload)
end

def record(name, payload)
Honeybadger.event(name, payload)
if Honeybadger.config.load_plugin_insights_events?(:rails)
Honeybadger.event(name, payload)
end

if Honeybadger.config.load_plugin_insights_metrics?(:rails)
metric_source 'rails'
record_metrics(name, payload)
end
end

def record_metrics(name, payload)
case name
when 'sql.active_record'
gauge('duration.sql.active_record', value: payload[:duration], **payload.slice(:query))
when 'process_action.action_controller'
gauge('duration.process_action.action_controller', value: payload[:duration], **payload.slice(:method, :controller, :action, :format, :status))
gauge('db_runtime.process_action.action_controller', value: payload[:db_runtime], **payload.slice(:method, :controller, :action, :format, :status))
gauge('view_runtime.process_action.action_controller', value: payload[:view_runtime], **payload.slice(:method, :controller, :action, :format, :status))
when 'perform.active_job'
gauge('duration.perform.active_job', value: payload[:duration], **payload.slice(:job_class, :queue_name))
when /^cache_.*.active_support$/
gauge("duration.#{name}", value: payload[:duration], **payload.slice(:store, :key))
end
end

def process?(event, payload)
Expand Down Expand Up @@ -109,22 +133,6 @@ def format_payload(payload)
end
end

class ActiveJobMetricsSubscriber < NotificationSubscriber
include Honeybadger::InstrumentationHelper

def format_payload(payload)
{
job_class: payload[:job].class.to_s,
queue_name: payload[:job].queue_name
}
end

def record(name, payload)
metric_source 'active_job'
histogram name, { bins: [30, 60, 120, 300, 1800, 3600, 21_600] }.merge(payload)
end
end

class ActionMailerSubscriber < NotificationSubscriber
end

Expand Down
1 change: 0 additions & 1 deletion lib/honeybadger/plugins/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def context(job) # rubocop:disable Metrics/MethodLength

if config.load_plugin_insights?(:active_job)
::ActiveSupport::Notifications.subscribe(/(enqueue_at|enqueue|enqueue_retry|enqueue_all|perform|retry_stopped|discard)\.active_job/, Honeybadger::ActiveJobSubscriber.new)
::ActiveSupport::Notifications.subscribe('perform.active_job', Honeybadger::ActiveJobMetricsSubscriber.new)
end
end
end
Expand Down
20 changes: 17 additions & 3 deletions lib/honeybadger/plugins/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ module Honeybadger
module Plugins
module Net
module HTTP
@@hb_config = ::Honeybadger.config

def self.set_hb_config(config)
@@hb_config = config
end

def request(request_data, body = nil, &block)
return super unless started?
return super if hb?
Expand All @@ -18,19 +24,26 @@ def request(request_data, body = nil, &block)
status: response_data.code.to_i
}.merge(parsed_uri_data(request_data))

Honeybadger.event('request.net_http', context)
if @@hb_config.load_plugin_insights_events?(:net_http)
Honeybadger.event('request.net_http', context)
end

if @@hb_config.load_plugin_insights_metrics?(:net_http)
context.delete(:url)
Honeybadger.gauge('duration.request', context.merge(metric_source: 'net_http'))
end
end[1] # return the response data only
end

def hb?
address.to_s[/#{Honeybadger.config[:'connection.host'].to_s}/]
address.to_s[/#{@@hb_config[:'connection.host'].to_s}/]
end

def parsed_uri_data(request_data)
uri = request_data.uri || build_uri(request_data)
{}.tap do |uri_data|
uri_data[:host] = uri.host
uri_data[:url] = uri.to_s if Honeybadger.config[:'net_http.insights.full_url']
uri_data[:url] = uri.to_s if @@hb_config[:'net_http.insights.full_url']
end
end

Expand All @@ -43,6 +56,7 @@ def build_uri(request_data)
requirement { config.load_plugin_insights?(:net_http) }

execution do
Honeybadger::Plugins::Net::HTTP.set_hb_config(config)
::Net::HTTP.send(:prepend, Honeybadger::Plugins::Net::HTTP)
end
end
Expand Down
14 changes: 10 additions & 4 deletions lib/honeybadger/plugins/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ def call(worker, msg, queue, &block)
raise
ensure
context.merge!(duration: duration, status: status)
Honeybadger.event('perform.sidekiq', context)
if Honeybadger.config.load_plugin_insights_events?(:sidekiq)
Honeybadger.event('perform.sidekiq', context)
end

metric_source 'sidekiq'
histogram 'perform', { bins: [30, 60, 120, 300, 1800, 3600, 21_600] }.merge(context.slice(:worker, :queue, :duration))
if Honeybadger.config.load_plugin_insights_metrics?(:sidekiq)
metric_source 'sidekiq'
gauge 'perform', context.slice(:worker, :queue, :duration)
end
end
end
end
Expand All @@ -55,7 +59,9 @@ def call(worker, msg, queue, _redis)
queue: queue
}

Honeybadger.event('enqueue.sidekiq', context)
if Honeybadger.config.load_plugin_insights_events?(:sidekiq)
Honeybadger.event('enqueue.sidekiq', context)
end

yield
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/honeybadger/gauge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

before { metric.record(1) }

it { should eq [{ avg: 1.0, latest: 1, max: 1, min: 1 }] }
it { should eq [{ total: 1, avg: 1.0, latest: 1, max: 1, min: 1 }] }
end
end
1 change: 1 addition & 0 deletions spec/unit/honeybadger/histogram_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
it do
should eq [
{
total: 1,
avg: 1.0,
latest: 1,
max: 1,
Expand Down
26 changes: 24 additions & 2 deletions spec/unit/honeybadger/plugins/net_http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

before do
Honeybadger::Plugin.instances[:net_http].reset!
Honeybadger::Plugin.instances[:net_http].load!(config)
end

it "includes integration module into Net::HTTP" do
Honeybadger::Plugin.instances[:net_http].load!(config)
expect(Net::HTTP.ancestors).to include(Honeybadger::Plugins::Net::HTTP)
end

Expand All @@ -24,12 +24,34 @@
end

context "report domain and full url" do
before { ::Honeybadger.config[:'net_http.insights.full_url'] = true }
before { config[:'net_http.insights.full_url'] = true }

it "contains a domain and url" do
expect(Honeybadger).to receive(:event).with('request.net_http', hash_including({method: "GET", status: 200, url: "http://example.com", host: "example.com"}))
Net::HTTP.get(URI.parse('http://example.com'))
end
end

context "metrics collection enabled" do
let(:config) { Honeybadger::Config.new(logger: NULL_LOGGER, debug: true, :'insights.enabled' => true, :'net_http.insights.metrics' => true) }

context "report domain only" do
it "contains a domain" do
expect(Honeybadger).to receive(:event).with('request.net_http', hash_including({method: "GET", status: 200, host: "example.com"}))
expect(Honeybadger).to receive(:gauge).with('duration.request', hash_including({method: "GET", status: 200, host: "example.com"}))
Net::HTTP.get(URI.parse('http://example.com'))
end
end

context "report domain and full url" do
before { config[:'net_http.insights.full_url'] = true }

it "contains a domain and url" do
expect(Honeybadger).to receive(:event).with('request.net_http', hash_including({method: "GET", status: 200, url: "http://example.com", host: "example.com"}))
expect(Honeybadger).to receive(:gauge).with('duration.request', hash_including({method: "GET", status: 200, host: "example.com"}))
Net::HTTP.get(URI.parse('http://example.com'))
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/unit/honeybadger/timer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

before { metric.record(1) }

it { should eq [{ avg: 1.0, latest: 1, max: 1, min: 1 }] }
it { should eq [{ total: 1, avg: 1.0, latest: 1, max: 1, min: 1 }] }
end
end