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

Instantiate middleware #118

Merged
merged 1 commit into from
Jul 20, 2020
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
12 changes: 2 additions & 10 deletions lib/influxdb/rails/metric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,20 @@
module InfluxDB
module Rails
class Metric
def initialize(configuration:, timestamp:, tags: {}, values: {}, hook_name:)
def initialize(configuration:, timestamp:, tags: {}, values: {})
@configuration = configuration
@timestamp = timestamp
@tags = tags
@values = values
@hook_name = hook_name
end

def write
return unless enabled?

client.write_point configuration.measurement_name, options
end

private

attr_reader :configuration, :tags, :values, :timestamp, :hook_name
attr_reader :configuration, :tags, :values, :timestamp

def options
{
Expand All @@ -34,11 +31,6 @@ def timestamp_with_precision
InfluxDB.convert_timestamp(timestamp.utc, configuration.client.time_precision)
end

def enabled?
!configuration.ignore_current_environment? &&
!configuration.ignored_hooks.include?(hook_name)
end

def client
InfluxDB::Rails.client
end
Expand Down
45 changes: 31 additions & 14 deletions lib/influxdb/rails/middleware/active_job_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,23 @@ module Middleware
class ActiveJobSubscriber < Subscriber # :nodoc:
private

def values(_start, duration, _payload)
value = measure_performance? ? duration : 1
def values
{
value: value,
}
end

def tags(payload)
def tags
{
hook: short_hook_name,
state: job_state(payload),
job: payload[:job].class.name,
queue: payload[:job].queue_name,
state: job_state,
job: job.class.name,
queue: job.queue_name,
}
end

def short_hook_name
return "enqueue" if hook_name.include?("enqueue")
return "perform_start" if hook_name.include?("perform_start")
return "perform" if hook_name.include?("perform")
end

def job_state(payload)
return "failed" if payload[:exception_object]
def job_state
return "failed" if failed?

case short_hook_name
when "enqueue"
Expand All @@ -44,6 +37,30 @@ def job_state(payload)
def measure_performance?
short_hook_name == "perform"
end

def short_hook_name
@short_hook_name ||= fetch_short_hook_name
end

def fetch_short_hook_name
return "enqueue" if hook_name.include?("enqueue")
return "perform_start" if hook_name.include?("perform_start")
return "perform" if hook_name.include?("perform")
end

def job
@job ||= payload[:job]
end

def value
return duration if measure_performance?

1
end

def failed?
payload[:exception_object]
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/influxdb/rails/middleware/active_record_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ module Middleware
class ActiveRecordSubscriber < Subscriber # :nodoc:
private

def values(_start, duration, payload)
def values
{
value: duration,
record_count: payload[:record_count],
}
end

def tags(payload)
def tags
{
hook: "instantiation",
class_name: payload[:class_name],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ module InfluxDB
module Rails
module Middleware
class BlockInstrumentationSubscriber < Subscriber
def values(_start, duration, payload)
private

def values
{
value: duration,
}.merge(payload[:values].to_h)
end

def tags(payload)
def tags
{
hook: "block_instrumentation",
name: payload[:name],
Expand Down
16 changes: 8 additions & 8 deletions lib/influxdb/rails/middleware/render_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ module InfluxDB
module Rails
module Middleware
class RenderSubscriber < Subscriber # :nodoc:
def short_hook_name
return "render_template" if hook_name.include?("render_template")
return "render_partial" if hook_name.include?("render_partial")
return "render_collection" if hook_name.include?("render_collection")
end

private

def values(_start, duration, payload)
def values
{
value: duration,
count: payload[:count],
cache_hits: payload[:cache_hits],
}
end

def tags(payload)
def tags
{
hook: short_hook_name,
filename: payload[:identifier],
}
end

def short_hook_name
return "render_template" if hook_name.include?("render_template")
return "render_partial" if hook_name.include?("render_partial")
return "render_collection" if hook_name.include?("render_collection")
end
end
end
end
Expand Down
18 changes: 11 additions & 7 deletions lib/influxdb/rails/middleware/request_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ module InfluxDB
module Rails
module Middleware
class RequestSubscriber < Subscriber # :nodoc:
def call(_name, started, finished, _unique_id, payload)
def write
super
ensure
InfluxDB::Rails.current.reset
end

private

def tags(payload)
def tags
{
method: "#{payload[:controller]}##{payload[:action]}",
hook: "process_action",
Expand All @@ -22,17 +22,21 @@ def tags(payload)
}
end

def values(start, duration, payload)
def values
{
controller: duration,
view: (payload[:view_runtime] || 0).ceil,
db: (payload[:db_runtime] || 0).ceil,
started: InfluxDB.convert_timestamp(
start.utc,
configuration.client.time_precision
),
started: started,
}
end

def started
InfluxDB.convert_timestamp(
start.utc,
configuration.client.time_precision
)
end
end
end
end
Expand Down
17 changes: 10 additions & 7 deletions lib/influxdb/rails/middleware/sql_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@ module InfluxDB
module Rails
module Middleware
class SqlSubscriber < Subscriber # :nodoc:
def call(_name, started, finished, _unique_id, payload)
super if InfluxDB::Rails::Sql::Query.new(payload).track?
end

private

def values(_start, duration, payload)
def values
{
value: duration,
sql: InfluxDB::Rails::Sql::Normalizer.new(payload[:sql]).perform,
}
end

def tags(payload)
query = InfluxDB::Rails::Sql::Query.new(payload)
def tags
{
hook: "sql",
operation: query.operation,
Expand All @@ -28,6 +23,14 @@ def tags(payload)
location: :raw,
}
end

def disabled?
super || !query.track?
hennevogel marked this conversation as resolved.
Show resolved Hide resolved
end

def query
@query ||= InfluxDB::Rails::Sql::Query.new(payload)
end
end
end
end
Expand Down
50 changes: 36 additions & 14 deletions lib/influxdb/rails/middleware/subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,61 @@ module Middleware
# which are intended as ActiveSupport::Notifications.subscribe
# consumers.
class Subscriber
attr_reader :configuration
attr_reader :hook_name

def initialize(configuration, hook_name)
def initialize(configuration:, hook_name:, start:, finish:, payload:)
@configuration = configuration
@hook_name = hook_name
@start = start
@finish = finish
@payload = payload
end

def self.call(name, start, finish, _id, payload)
new(
configuration: InfluxDB::Rails.configuration,
start: start,
finish: finish,
payload: payload,
hook_name: name
).write
end

def call(_name, start, finish, _id, payload)
write_metric(start, finish, payload)
def write
return if disabled?

metric.write
rescue StandardError => e
::Rails.logger.error("[InfluxDB::Rails] Unable to write points: #{e.message}")
end

private

def write_metric(start, finish, payload)
attr_reader :configuration, :hook_name, :start, :finish, :payload

def metric
InfluxDB::Rails::Metric.new(
values: values(start, ((finish - start) * 1000).ceil, payload),
tags: tags(payload),
values: values,
tags: tags,
configuration: configuration,
timestamp: finish,
hook_name: hook_name
).write
timestamp: finish
)
end

def tags(*)
def tags
raise NotImplementedError, "must be implemented in subclass"
end

def values(*)
def values
raise NotImplementedError, "must be implemented in subclass"
end

def duration
((finish - start) * 1000).ceil
end

def disabled?
configuration.ignore_current_environment? ||
configuration.ignored_hooks.include?(hook_name)
end
end
end
end
Expand Down
9 changes: 2 additions & 7 deletions lib/influxdb/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,11 @@ class Railtie < ::Rails::Railtie # :nodoc:
"perform_start.active_job" => Middleware::ActiveJobSubscriber,
"perform.active_job" => Middleware::ActiveJobSubscriber,
"block_instrumentation.influxdb_rails" => Middleware::BlockInstrumentationSubscriber,
}.each do |hook_name, subscriber_class|
subscribe_to(hook_name, subscriber_class)
}.each do |hook_name, subscriber|
ActiveSupport::Notifications.subscribe(hook_name, subscriber)
end
end
# rubocop:enable Metrics/BlockLength

def subscribe_to(hook_name, subscriber_class)
subscriber = subscriber_class.new(InfluxDB::Rails.configuration, hook_name)
ActiveSupport::Notifications.subscribe hook_name, subscriber
end
end
end
end
11 changes: 4 additions & 7 deletions lib/influxdb/rails/tags.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module InfluxDB
module Rails
class Tags
def initialize(tags: {}, config:)
def initialize(tags: {}, config:, additional_tags: InfluxDB::Rails.current.tags)
@tags = tags
@config = config
@additional_tags = additional_tags
end

def to_h
Expand All @@ -14,7 +15,7 @@ def to_h

private

attr_reader :tags, :config
attr_reader :additional_tags, :tags, :config

def expanded_tags
config.tags_middleware.call(tags.merge(default_tags))
Expand All @@ -25,11 +26,7 @@ def default_tags
server: Socket.gethostname,
app_name: config.application_name,
location: :raw,
}.merge(InfluxDB::Rails.current.tags)
end

def current
@current ||= InfluxDB::Rails.current
}.merge(additional_tags)
end
end
end
Expand Down
Loading