Commit f4f197fd authored by Alex Kalderimis's avatar Alex Kalderimis

Merge branch 'issue_326364' into 'master'

Record message size transmitted over action cable

See merge request gitlab-org/gitlab!65286
parents 946f3833 31407b25
......@@ -121,6 +121,7 @@ The following metrics are available:
| `action_cable_single_client_transmissions_total` | Counter | 13.10 | The number of ActionCable messages transmitted to any client in any channel | `server_mode` |
| `action_cable_subscription_confirmations_total` | Counter | 13.10 | The number of ActionCable subscriptions from clients confirmed | `server_mode` |
| `action_cable_subscription_rejections_total` | Counter | 13.10 | The number of ActionCable subscriptions from clients rejected | `server_mode` |
| `action_cable_transmitted_bytes` | Histogram | 14.1 | Message size, in bytes, transmitted over action cable | `operation`, `channel` |
| `gitlab_issuable_fast_count_by_state_total` | Counter | 13.5 | Total number of row count operations on issue/merge request list pages | |
| `gitlab_issuable_fast_count_by_state_failures_total` | Counter | 13.5 | Number of soft-failed row count operations on issue/merge request list pages | |
| `gitlab_external_http_total` | Counter | 13.8 | Total number of HTTP calls to external systems | `controller`, `action` |
......
......@@ -12,6 +12,7 @@ module Gitlab
TRANSMIT_SUBSCRIPTION_CONFIRMATION = :action_cable_subscription_confirmations_total
TRANSMIT_SUBSCRIPTION_REJECTION = :action_cable_subscription_rejections_total
BROADCAST = :action_cable_broadcasts_total
DATA_TRANSMITTED_BYTES = :action_cable_transmitted_bytes
def transmit_subscription_confirmation(event)
confirm_subscription_counter.increment
......@@ -23,6 +24,14 @@ module Gitlab
def transmit(event)
transmit_counter.increment
if event.payload.present?
channel = event.payload[:channel_class]
operation = operation_name_from(event.payload)
data_size = ::ActiveSupport::JSON.encode(event.payload[:data]).bytesize
transmitted_bytes_histogram.observe({ channel: channel, operation: operation }, data_size)
end
end
def broadcast(event)
......@@ -31,6 +40,13 @@ module Gitlab
private
# When possible tries to query operation name
def operation_name_from(payload)
data = payload.dig(:data, 'result', 'data') || {}
data.each_key.first
end
def transmit_counter
strong_memoize("transmission_counter") do
::Gitlab::Metrics.counter(
......@@ -66,6 +82,12 @@ module Gitlab
)
end
end
def transmitted_bytes_histogram
strong_memoize("transmitted_bytes_histogram") do
::Gitlab::Metrics.histogram(DATA_TRANSMITTED_BYTES, 'Message size, in bytes, transmitted over action cable')
end
end
end
end
end
......
......@@ -5,7 +5,7 @@ require 'spec_helper'
RSpec.describe Gitlab::Metrics::Subscribers::ActionCable, :request_store do
let(:subscriber) { described_class.new }
let(:counter) { double(:counter) }
let(:data) { { data: { event: 'updated' } } }
let(:data) { { 'result' => { 'data' => { 'event' => 'updated' } } } }
let(:channel_class) { 'IssuesChannel' }
let(:event) do
double(
......@@ -35,6 +35,17 @@ RSpec.describe Gitlab::Metrics::Subscribers::ActionCable, :request_store do
subscriber.transmit(event)
end
it 'tracks size of payload as JSON' do
allow(::Gitlab::Metrics).to receive(:histogram).with(
:action_cable_transmitted_bytes, /transmit/
).and_return(counter)
message_size = ::ActiveSupport::JSON.encode(data).bytesize
expect(counter).to receive(:observe).with({ channel: channel_class, operation: 'event' }, message_size)
subscriber.transmit(event)
end
end
describe '#broadcast' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment