Skip to content

Commit

Permalink
chore: add subscription metrics to ddp-streamer (#30289)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego authored Sep 11, 2023
1 parent be98f53 commit bb3a0d4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ee/apps/ddp-streamer/src/DDPStreamer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ export class DDPStreamer extends ServiceClass {
return;
}

metrics.register({
name: 'rocketchat_subscription',
type: 'histogram',
labelNames: ['subscription'],
description: 'Client subscriptions to Rocket.Chat',
unit: 'millisecond',
quantiles: true,
});

metrics.register({
name: 'users_connected',
type: 'gauge',
Expand All @@ -86,6 +95,8 @@ export class DDPStreamer extends ServiceClass {
description: 'Users logged by streamer',
});

server.setMetrics(metrics);

server.on(DDP_EVENTS.CONNECTED, () => {
metrics.increment('users_connected', { nodeID }, 1);
});
Expand Down
11 changes: 11 additions & 0 deletions ee/apps/ddp-streamer/src/Server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EventEmitter } from 'events';

import type { IServiceMetrics } from '@rocket.chat/core-services';
import { MeteorService, isMeteorError, MeteorError } from '@rocket.chat/core-services';
import { Logger } from '@rocket.chat/logger';
import ejson from 'ejson';
Expand Down Expand Up @@ -38,6 +39,8 @@ export class Server extends EventEmitter {

private _methods = new Map<string, MethodFn>();

private metrics?: IServiceMetrics;

public readonly id = uuidv1();

serialize = ejson.stringify;
Expand All @@ -52,6 +55,10 @@ export class Server extends EventEmitter {
return ejson.parse(payload);
};

setMetrics(metrics: IServiceMetrics): void {
this.metrics = metrics;
}

async call(client: Client, packet: IPacket): Promise<void> {
// if client is not connected we don't need to do anything
if (client.ws.readyState !== WebSocket.OPEN) {
Expand Down Expand Up @@ -103,9 +110,13 @@ export class Server extends EventEmitter {
throw new MeteorError(404, `Subscription '${packet.name}' not found`);
}

const end = this.metrics?.timer('rocketchat_subscription', { subscription: packet.name });

const publication = new Publication(client, packet, this);
const [eventName, options] = packet.params;
await fn.call(publication, eventName, options);

end?.();
} catch (err: unknown) {
return this.nosub(client, packet, handleInternalException(err, 'Subscription error'));
}
Expand Down
1 change: 1 addition & 0 deletions packages/core-services/src/types/IBroker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type BaseMetricOptions = {
labelNames?: Array<string>;
unit?: string;
aggregator?: string;
[key: string]: unknown;
};

export interface IServiceMetrics {
Expand Down

0 comments on commit bb3a0d4

Please sign in to comment.