Browse Source

accurate duration

main
pvincent 2 months ago
parent
commit
34a0366ca3
  1. 34
      lib/semantic/subscribers/active_record.rb

34
lib/semantic/subscribers/active_record.rb

@ -14,40 +14,45 @@ module Semantic
category, model, *remaining = name.split.reverse category, model, *remaining = name.split.reverse
return if category == 'TRANSACTION' return if category == 'TRANSACTION'
statement_taint = AnsiColors::TEXT_GRAY_300
case category case category
when 'Count' when 'Count'
statement_taint = AnsiColors::TEXT_GRAY_300
name = "#{category} #{model}" name = "#{category} #{model}"
when 'Load' when 'Load'
name = if event.payload[:cached]
statement_taint = AnsiColors::TEXT_GRAY_300
if event.payload[:cached]
name = "Cache #{model}" name = "Cache #{model}"
no_stats = true
else else
statement_taint = TEXT_BLUE statement_taint = TEXT_BLUE
row_count = event.payload[:row_count] row_count = event.payload[:row_count]
name = "#{category} #{row_count} #{model.pluralize(row_count)}" name = "#{category} #{row_count} #{model.pluralize(row_count)}"
end end
when 'Update', 'Create', 'Destroy' when 'Update', 'Create', 'Destroy'
statement_taint = TEXT__PINK
name = "#{category} #{model}" name = "#{category} #{model}"
category_taint = TRANSACTION_TAINT category_taint = TRANSACTION_TAINT
statement_taint = TEXT__PINK
increment_transaction_local(event.payload[:transaction].uuid, category.downcase.to_sym)
increment_transaction_local(event.payload[:transaction].uuid, category.downcase.to_sym, event.duration)
else raise "unknown sql category: <#{category}>" else raise "unknown sql category: <#{category}>"
end end
name = "#{name} #{stats_event(event.duration)}" unless no_stats
name = "#{name} #{remaining.join(' ')}" if remaining.any? name = "#{name} #{remaining.join(' ')}" if remaining.any?
name = colorize(name, category_taint) if category_taint name = colorize(name, category_taint) if category_taint
statement = colorize(event.payload[:sql], statement_taint) statement = colorize(event.payload[:sql], statement_taint)
logger.debug("#{name} #{statement}") logger.debug("#{name} #{statement}")
end end
def start_transaction(event)
def start_transaction(_event)
logger.info(colorize('Begin', TRANSACTION_TAINT)) logger.info(colorize('Begin', TRANSACTION_TAINT))
end end
def transaction(event) def transaction(event)
outcome = colorize(event.payload[:outcome].capitalize, TRANSACTION_TAINT) outcome = colorize(event.payload[:outcome].capitalize, TRANSACTION_TAINT)
summary = transaction_summary(event.payload[:transaction])
logger.info("#{outcome} (#{summary})")
transaction = event.payload[:transaction]
summary = transaction_summary(transaction)
stats = stats_event(event.duration - transaction_internal_duration(transaction))
logger.info("#{outcome} #{summary} #{stats}")
end end
def instantiation(event); end def instantiation(event); end
@ -56,18 +61,27 @@ module Semantic
private private
def stats_event(duration)= colorize("(#{number_to_ms(duration)})", TEXT_GRAY_400)
def number_to_ms(number) = "#{::ActionController::Base.helpers.number_with_precision(number, precision: 1)}ms"
def transaction_internal_duration(transaction) = transaction_local(transaction.uuid)[:total_duration]
def transaction_summary(transaction) def transaction_summary(transaction)
transaction_local(transaction.uuid) transaction_local(transaction.uuid)
.except(:total_duration)
.select { |_, value| value.positive? } .select { |_, value| value.positive? }
.map { |k, v| "#{v} #{k.to_s.pluralize(v)}" } .map { |k, v| "#{v} #{k.to_s.pluralize(v)}" }
.join(',') .join(',')
end end
def increment_transaction_local(transaction_id, sql_command) = transaction_local(transaction_id)[sql_command] += 1
def increment_transaction_local(transaction_id, sql_command, duration)
transaction_local(transaction_id)[sql_command] += 1
transaction_local(transaction_id)[:total_duration] += duration
end
def thread_local = Thread.current[self.class.to_s] ||= {} def thread_local = Thread.current[self.class.to_s] ||= {}
def transaction_local(transaction_id) def transaction_local(transaction_id)
thread_local[transaction_id] ||= { update: 0, create: 0, destroy: 0 }
thread_local[transaction_id] ||= { update: 0, create: 0, destroy: 0, total_duration: 0 }
end end
end end
end end

Loading…
Cancel
Save