Skip to content

Commit

Permalink
More some responsibilites to Store.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Feb 12, 2010
1 parent 12b3d85 commit e0a4696
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 86 deletions.
65 changes: 13 additions & 52 deletions lib/rails_metrics.rb
Expand Up @@ -50,39 +50,6 @@ def self.listening?
Thread.current[:rails_metrics_listening] || false
end

class Node
attr_reader :name, :started_at, :ended_at, :payload

def initialize(name, started_at, ended_at, transaction_id, payload)
@name = name
@started_at = started_at
@ended_at = ended_at
@payload = payload
self
end

def root?
false
end

def children
@children ||= []
end

def duration
@duration ||= 1000.0 * (@ended_at - @started_at)
end

def parent_of?(node)
start = (self.started_at - node.started_at) * 1000
start <= 0 && (start + self.duration >= node.duration)
end

def child_of?(node)
node.parent_of?(self)
end
end

# Allow you to specify a condition to ignore a notification based
# on its name and/or payload. For example, if you want to ignore
# all notifications with empty payload, one can do:
Expand Down Expand Up @@ -113,31 +80,25 @@ def self.ignore_patterns

# Holds the queue which store stuff in the database.
def self.async_consumer
@@async_consumer ||= AsyncConsumer.new do |nodes|
next if nodes.empty?

nodes.map! { |i| RailsMetrics::Node.new(*i) }
@@async_consumer ||= AsyncConsumer.new do |events|
next if events.empty?
root_node = nil

while node = nodes.shift
if parent = nodes.find { |n| n.parent_of?(node) }
parent.children << node
metrics = events.map do |event|
metric = RailsMetrics.store.new
metric.configure(event)
metric
end

while metric = metrics.shift
if parent = metrics.find { |n| n.parent_of?(metric) }
parent.children << metric
else
root_node = node
root_metric = metric
end
end

save_nodes!(root_node)
end
end

def self.save_nodes!(node, parent_id=nil)
metric = RailsMetrics.store.new
metric.store!([node.name, node.started_at, node.ended_at, parent_id, node.payload])
parent_id = metric.id

node.children.each do |child|
save_nodes!(child, parent_id)
root_metric.save_metrics!
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/rails_metrics/async_consumer.rb
Expand Up @@ -13,10 +13,10 @@ def instrument(name, payload={})
class AsyncConsumer
attr_reader :thread

def initialize(&block)
def initialize(queue=Queue.new, &block)
@off = true
@block = block
@queue = Queue.new
@queue = queue

@thread = Thread.new do
set_void_instrumenter
Expand Down
6 changes: 0 additions & 6 deletions lib/rails_metrics/orm/active_record.rb
Expand Up @@ -54,12 +54,6 @@ def connections_ids
self.connection_pool.connections.map(&:object_id)
end
end

protected

def save_metrics!
save!
end
end
end
end
34 changes: 23 additions & 11 deletions lib/rails_metrics/store.rb
Expand Up @@ -15,21 +15,33 @@ module RailsMetrics
module Store
VALID_ORDERS = %w(earliest latest slowest fastest).freeze

def store!(args)
self.name = args[0].to_s
self.started_at = args[1]
self.duration = (args[2] - args[1]) * 1000
self.instrumenter_id = args[3]
self.payload = RailsMetrics::PayloadParser.filter(name, args[4])
def configure(args)
self.name = args[0].to_s
self.started_at = args[1]
self.duration = (args[2] - args[1]) * 1000
self.payload = RailsMetrics::PayloadParser.filter(name, args[4])
end

def children
@children ||= []
end

def parent_of?(node)
start = (self.started_at - node.started_at) * 1000
start <= 0 && (start + self.duration >= node.duration)
end

save_metrics!
self
def child_of?(node)
node.parent_of?(self)
end

protected
def save_metrics!(parent_id=nil)
self.instrumenter_id = parent_id
save!

def save_metrics!
raise NotImplementedError
children.each do |child|
child.save_metrics!(self.id)
end
end
end
end
20 changes: 5 additions & 15 deletions test/store_test.rb
Expand Up @@ -8,7 +8,9 @@ def sample_args

# We need to mute RailsMetrics, otherwise we get Sqlite3 database lock errors
def store!(args=sample_args)
Metric.new.store!(args)
metric = Metric.new
metric.configure(args)
metric
end

test "sets the name" do
Expand All @@ -23,23 +25,11 @@ def store!(args=sample_args)
assert_kind_of Time, store!.started_at
end

test "sets the instrumenter id" do
assert_equal 1, store!.instrumenter_id
end

test "sets the payload" do
assert_equal Hash[:some => :info], store!.payload
end

test "saves the record" do
assert_difference "Metric.count" do
store!
end
end

test "raises an error if cannot be saved" do
assert_raise ActiveRecord::RecordInvalid do
store!([nil, Time.now, Time.now])
end
test "does not set the instrumenter id from args" do
assert_nil store!.instrumenter_id
end
end

0 comments on commit e0a4696

Please sign in to comment.