Skip to content

Commit

Permalink
Implement instantiation.active_record subscriber
Browse files Browse the repository at this point in the history
Partially address #74
  • Loading branch information
ChrisBr committed May 4, 2020
1 parent 4c5ea1e commit 4cd6fcd
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ For the full commit log, [see here](https://github.com/influxdata/influxdb-rails
- Drop undocumented `instrumentation_enabled` setting, use
`ignored_environments` do disable instrumentation
- Simplified spec with a PORO test client
- Implement `instantiation.active_record` subscriber (https://guides.rubyonrails.org/active_support_instrumentation.html#instantiation-active-record)

## v1.0.0, released 2019-10-23
The Final release, no code changes.
Expand Down
1 change: 1 addition & 0 deletions lib/influxdb-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "influxdb/rails/middleware/render_subscriber"
require "influxdb/rails/middleware/request_subscriber"
require "influxdb/rails/middleware/sql_subscriber"
require "influxdb/rails/middleware/active_record_subscriber"
require "influxdb/rails/sql/query"
require "influxdb/rails/version"
require "influxdb/rails/configuration"
Expand Down
26 changes: 26 additions & 0 deletions lib/influxdb/rails/middleware/active_record_subscriber.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "influxdb/rails/middleware/subscriber"
require "influxdb/rails/sql/query"

module InfluxDB
module Rails
module Middleware
class ActiveRecordSubscriber < Subscriber # :nodoc:
private

def values(started, finished, payload)
{
value: ((finished - started) * 1000).ceil,
record_count: payload[:record_count],
}
end

def tags(payload)
{
hook: "instantiation",
class_name: payload[:class_name],
}
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/influxdb/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Railtie < ::Rails::Railtie # :nodoc:
"render_partial.action_view" => Middleware::RenderSubscriber,
"render_collection.action_view" => Middleware::RenderSubscriber,
"sql.active_record" => Middleware::SqlSubscriber,
"instantiation.active_record" => Middleware::ActiveRecordSubscriber,
}.each do |hook_name, subscriber_class|
subscribe_to(hook_name, subscriber_class)
end
Expand Down
65 changes: 65 additions & 0 deletions spec/requests/active_record_instantiation_metrics_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require File.dirname(__FILE__) + "/../spec_helper"

RSpec.describe "ActiveRecord instantiation metrics", type: :request do
let(:tags_middleware) do
lambda do |tags|
tags.merge(tags_middleware: :tags_middleware)
end
end
let(:metric) { Metric.create!(name: "name") }
before do
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_environments).and_return(%w[development])
allow_any_instance_of(ActionDispatch::Request).to receive(:request_id).and_return(:request_id)
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:application_name).and_return(:app_name)
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:tags_middleware).and_return(tags_middleware)
end

it "writes metric" do
get metric_path(metric)

expect_metric(
tags: a_hash_including(
location: "MetricsController#show",
hook: "instantiation",
class_name: "Metric",
additional_tag: :value,
server: Socket.gethostname,
app_name: :app_name,
tags_middleware: :tags_middleware
),
values: a_hash_including(
additional_value: :value,
request_id: :request_id,
value: be_between(1, 30),
record_count: 1
)
)
end

it "includes correct timestamps" do
travel_to Time.zone.local(2018, 1, 1, 9, 0, 0)

get metric_path(metric)

expect_metric(
tags: a_hash_including(
location: "MetricsController#show",
hook: "instantiation"
),
timestamp: 1_514_797_200
)
end

it "does not write metric when hook is ignored" do
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_hooks).and_return(["instantiation.active_record"])

get metric_path(metric)

expect_no_metric(
tags: a_hash_including(
location: "MetricsController#show",
hook: "instantiation"
)
)
end
end
6 changes: 5 additions & 1 deletion spec/support/rails4/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
app.initialize!

app.routes.draw do
resources :metrics, only: :index
resources :metrics, only: %i[index show]
resources :exceptions, only: :index
end

Expand Down Expand Up @@ -41,6 +41,10 @@ class MetricsController < ApplicationController
def index
Metric.create!(name: "name")
end

def show
@metric = Metric.find_by(name: "name")
end
end

class ExceptionsController < ApplicationController
Expand Down
6 changes: 5 additions & 1 deletion spec/support/rails5/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
app.initialize!

app.routes.draw do
resources :metrics, only: :index
resources :metrics, only: %i[index show]
resources :exceptions, only: :index
end

Expand Down Expand Up @@ -41,6 +41,10 @@ class MetricsController < ApplicationController
def index
Metric.create!(name: "name")
end

def show
@metric = Metric.find_by(name: "name")
end
end

class ExceptionsController < ApplicationController
Expand Down
6 changes: 5 additions & 1 deletion spec/support/rails6/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
app.initialize!

app.routes.draw do
resources :metrics, only: :index
resources :metrics, only: %i[index show]
resources :exceptions, only: :index
end

Expand Down Expand Up @@ -41,6 +41,10 @@ class MetricsController < ApplicationController
def index
Metric.create!(name: "name")
end

def show
@metric = Metric.find_by(name: "name")
end
end

class ExceptionsController < ApplicationController
Expand Down
4 changes: 4 additions & 0 deletions spec/support/views/metrics/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>Show</h1>
<div>
<%= @metric.name %>
</div>

0 comments on commit 4cd6fcd

Please sign in to comment.