From e52488751c25dc4c2707835d5a6b60ff2d792c10 Mon Sep 17 00:00:00 2001 From: "Stanko K.R." Date: Mon, 13 Nov 2023 07:28:32 +0100 Subject: [PATCH] Expose latency through notifications --- config/initializers/notifications.rb | 4 ++++ lib/monkey_patches/action_cable/heartbeat_with_pong.rb | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 config/initializers/notifications.rb diff --git a/config/initializers/notifications.rb b/config/initializers/notifications.rb new file mode 100644 index 0000000..9238ed8 --- /dev/null +++ b/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 + diff --git a/lib/monkey_patches/action_cable/heartbeat_with_pong.rb b/lib/monkey_patches/action_cable/heartbeat_with_pong.rb index 474387d..b4b0892 100644 --- a/lib/monkey_patches/action_cable/heartbeat_with_pong.rb +++ b/lib/monkey_patches/action_cable/heartbeat_with_pong.rb @@ -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)