Skip to content

Commit a33c21b

Browse files
feat(metrics): add subscription count metrics
Track channel subscriptions separately from unique channels: - reverb_subscriptions_total: subscriptions per app and channel type - reverb_subscriptions_current: total subscriptions across all apps This clarifies the relationship between connections and channels, where multiple connections can subscribe to the same channel. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 82458f0 commit a33c21b

2 files changed

Lines changed: 27 additions & 11 deletions

File tree

app/Http/Controllers/MetricsController.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ protected function addReverbMetrics(): void
7070
$reverbUp = false;
7171
$totalConnections = 0;
7272
$totalChannels = 0;
73+
$totalSubscriptions = 0;
7374

7475
foreach ($this->appProvider->all() as $app) {
7576
try {
@@ -85,13 +86,18 @@ protected function addReverbMetrics(): void
8586
]);
8687
$totalConnections += $metrics['connections'];
8788

88-
// Channel counts per app
89-
foreach ($metrics['channels'] as $type => $count) {
90-
$this->store->gauge('reverb_channels_active', $count, [
89+
// Channel counts and subscriptions per app
90+
foreach ($metrics['channels'] as $type => $data) {
91+
$this->store->gauge('reverb_channels_active', $data['count'], [
9192
'app_id' => $appId,
9293
'type' => $type,
9394
]);
94-
$totalChannels += $count;
95+
$this->store->gauge('reverb_subscriptions_total', $data['subscriptions'], [
96+
'app_id' => $appId,
97+
'type' => $type,
98+
]);
99+
$totalChannels += $data['count'];
100+
$totalSubscriptions += $data['subscriptions'];
95101
}
96102
}
97103
} catch (Throwable) {
@@ -106,13 +112,14 @@ protected function addReverbMetrics(): void
106112
if ($reverbUp) {
107113
$this->store->gauge('reverb_connections_current', $totalConnections);
108114
$this->store->gauge('reverb_channels_current', $totalChannels);
115+
$this->store->gauge('reverb_subscriptions_current', $totalSubscriptions);
109116
}
110117
}
111118

112119
/**
113120
* Fetch metrics for a specific app from Reverb's API.
114121
*
115-
* @return array{connections: int, channels: array<string, int>}|null
122+
* @return array{connections: int, channels: array<string, array{count: int, subscriptions: int}>}|null
116123
*/
117124
protected function fetchAppMetrics(Application $app): ?array
118125
{
@@ -126,22 +133,23 @@ protected function fetchAppMetrics(Application $app): ?array
126133

127134
$connections = $connectionsResult->connections ?? 0;
128135

129-
// Get channel info
136+
// Get channel info with subscription counts
130137
$channelsResult = $pusher->get('/channels', [
131138
'info' => 'subscription_count',
132139
]);
133140

134141
$channels = [
135-
'public' => 0,
136-
'private' => 0,
137-
'presence' => 0,
138-
'encrypted' => 0,
142+
'public' => ['count' => 0, 'subscriptions' => 0],
143+
'private' => ['count' => 0, 'subscriptions' => 0],
144+
'presence' => ['count' => 0, 'subscriptions' => 0],
145+
'encrypted' => ['count' => 0, 'subscriptions' => 0],
139146
];
140147

141148
if ($channelsResult && isset($channelsResult->channels)) {
142149
foreach ($channelsResult->channels as $name => $info) {
143150
$type = $this->determineChannelType($name);
144-
$channels[$type]++;
151+
$channels[$type]['count']++;
152+
$channels[$type]['subscriptions'] += $info->subscription_count ?? 0;
145153
}
146154
}
147155

app/Metrics/PrometheusExporter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ class PrometheusExporter
3232
'type' => 'gauge',
3333
'help' => 'Total current active channels across all apps',
3434
],
35+
'reverb_subscriptions_total' => [
36+
'type' => 'gauge',
37+
'help' => 'Current number of channel subscriptions by type per app',
38+
],
39+
'reverb_subscriptions_current' => [
40+
'type' => 'gauge',
41+
'help' => 'Total current channel subscriptions across all apps',
42+
],
3543
'reverb_server_info' => [
3644
'type' => 'gauge',
3745
'help' => 'Reverb server information',

0 commit comments

Comments
 (0)