Skip to content

Commit

Permalink
Expose latency through notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
monorkin committed Nov 13, 2023
1 parent 9da4ce9 commit e524887
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions config/initializers/notifications.rb
@@ -0,0 +1,4 @@
ActiveSupport::Notifications.subscribe("connection.latency") do |name, start, finish, id, payload|
Rails.logger.info "[LATENCY] [#{payload[:identifiers].map { |key, record| "#{key}:#{record.class}-#{record.id}" }.join(";")}] #{payload[:value].round(6)}ms"
end

9 changes: 8 additions & 1 deletion lib/monkey_patches/action_cable/heartbeat_with_pong.rb
Expand Up @@ -55,7 +55,14 @@ def register_client_pong!(data)
if latency.negative?
logger.info "🩹 We have a time traveler! Latency: #{latency}ms (#{connection_identifier})"
else
logger.info "🩹 Roundtrip latency: #{latency.round(6)}ms (#{connection_identifier})"
logger.info "🩹 Latency: #{latency}ms (#{connection_identifier})"
ActiveSupport::Notifications.instrument(
"connection.latency",
value: latency,
action: :timing,
connection_identifier: connection_identifier,
identifiers: identifiers.map { |id| [id, instance_variable_get("@#{id}")] }.to_h
)
end
rescue Exception => e
rescue_with_handler(e)
Expand Down

0 comments on commit e524887

Please sign in to comment.