-
Notifications
You must be signed in to change notification settings - Fork 20
feat: enhance federation-sdk with metrics tracking and new utility functions #331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dhulke
wants to merge
4
commits into
feat/tracing
Choose a base branch
from
feat/metrics
base: feat/tracing
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0fe2b38
feat: enhance federation-sdk with metrics tracking and new utility fu…
dhulke f11221b
refactor: reorder imports in event.service.ts for improved readability
dhulke b13ab0a
feat: add optional exception handler support to event emitter service
dhulke 942f67b
fix: update metric retrieval to use the correct registry reference
dhulke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /** | ||
| * Extracts the origin server domain from a Matrix room ID. | ||
| * @example extractOriginFromMatrixRoomId('!room:matrix.org') // 'matrix.org' | ||
| */ | ||
| export function extractOriginFromMatrixRoomId(roomId: string): string { | ||
| return roomId.split(':').pop() || 'unknown'; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts the origin server domain from a Matrix user ID. | ||
| * @example extractOriginFromMatrixUserId('@user:matrix.org') // 'matrix.org' | ||
| */ | ||
| export function extractOriginFromMatrixUserId(userId: string): string { | ||
| return userId.split(':').pop() || 'unknown'; | ||
| } | ||
|
|
||
| // File types for message type detection | ||
| const fileTypes = ['m.image', 'm.video', 'm.audio', 'm.file']; | ||
|
|
||
| /** | ||
| * Determines the message type from a Matrix event for metrics labeling. | ||
| * @returns 'text' | 'file' | 'encrypted' | ||
| */ | ||
| export function determineMessageType(event: { | ||
| type?: string; | ||
| content?: { msgtype?: string }; | ||
| }): 'text' | 'file' | 'encrypted' { | ||
| if (event.type === 'm.room.encrypted') { | ||
| return 'encrypted'; | ||
| } | ||
|
|
||
| const msgtype = event.content?.msgtype; | ||
| if (msgtype && fileTypes.includes(msgtype)) { | ||
| return 'file'; | ||
| } | ||
|
|
||
| return 'text'; | ||
| } | ||
|
|
||
| /** | ||
| * Bucketizes PDU count for metrics labeling to avoid high cardinality. | ||
| * Groups counts into buckets: 0, 1, 2-5, 6-10, 11-50, 51+ | ||
| */ | ||
| export function bucketizePduCount(count: number): string { | ||
| if (count === 0) return '0'; | ||
| if (count === 1) return '1'; | ||
| if (count <= 5) return '2-5'; | ||
| if (count <= 10) return '6-10'; | ||
| if (count <= 50) return '11-50'; | ||
| return '51+'; | ||
| } | ||
|
|
||
| /** | ||
| * Bucketizes EDU count for metrics labeling to avoid high cardinality. | ||
| * Groups counts into buckets: 0, 1, 2-5, 6-10, 11+ | ||
| */ | ||
| export function bucketizeEduCount(count: number): string { | ||
| if (count === 0) return '0'; | ||
| if (count === 1) return '1'; | ||
| if (count <= 5) return '2-5'; | ||
| if (count <= 10) return '6-10'; | ||
| return '11+'; | ||
| } | ||
|
|
||
| /** | ||
| * Maps event emitter event types to simplified event_type labels for metrics. | ||
| */ | ||
| export function getEventTypeLabel(event: string): string { | ||
| // Map homeserver.matrix.* events to simplified labels | ||
| const mapping: Record<string, string> = { | ||
| 'homeserver.matrix.message': 'message', | ||
| 'homeserver.matrix.encrypted': 'message', | ||
| 'homeserver.matrix.membership': 'membership', | ||
| 'homeserver.matrix.redaction': 'redaction', | ||
| 'homeserver.matrix.reaction': 'reaction', | ||
| 'homeserver.matrix.typing': 'typing', | ||
| 'homeserver.matrix.presence': 'presence', | ||
| 'homeserver.matrix.room.create': 'room_create', | ||
| 'homeserver.matrix.room.name': 'room_update', | ||
| 'homeserver.matrix.room.topic': 'room_update', | ||
| 'homeserver.matrix.room.power_levels': 'room_update', | ||
| 'homeserver.matrix.room.server_acl': 'room_update', | ||
| 'homeserver.matrix.room.role': 'role_change', | ||
| 'homeserver.matrix.encryption': 'encryption', | ||
| }; | ||
| return mapping[event] || event.replace('homeserver.matrix.', ''); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import client, { type Registry } from 'prom-client'; | ||
|
|
||
| let registry: Registry = client.register; | ||
|
|
||
| export function initMetrics(opts: { registry: Registry }) { | ||
| registry = opts.registry; | ||
| } | ||
|
|
||
| const percentiles = [0.01, 0.1, 0.5, 0.9, 0.95, 0.99, 1]; | ||
|
|
||
| /** | ||
| * Gets an existing metric from the registry or creates it if it doesn't exist. | ||
| * This ensures we don't get duplicate registration errors when the SDK | ||
| * is used alongside other apps that also register metrics. | ||
| */ | ||
| function getOrCreateMetric<T extends client.Metric>( | ||
| name: string, | ||
| createFn: () => T, | ||
| ): T { | ||
| const existing = registry.getSingleMetric(name); | ||
| if (existing) { | ||
| return existing as T; | ||
| } | ||
| return createFn(); | ||
| } | ||
|
|
||
| /** | ||
| * Federation metrics for incoming operations. | ||
| */ | ||
| export const federationMetrics = { | ||
| /** Counter for federation events processed */ | ||
| get federationEventsProcessed() { | ||
| return getOrCreateMetric( | ||
| 'rocketchat_federation_events_processed', | ||
| () => | ||
| new client.Counter({ | ||
| name: 'rocketchat_federation_events_processed', | ||
| labelNames: ['event_type', 'direction'], | ||
| help: 'Total federation events processed', | ||
| registers: [registry], | ||
| }), | ||
| ); | ||
| }, | ||
|
|
||
| /** Counter for failed federation events */ | ||
| get federationEventsFailed() { | ||
| return getOrCreateMetric( | ||
| 'rocketchat_federation_events_failed', | ||
| () => | ||
| new client.Counter({ | ||
| name: 'rocketchat_federation_events_failed', | ||
| labelNames: ['event_type', 'direction', 'error_type'], | ||
| help: 'Total federation events that failed to process', | ||
| registers: [registry], | ||
| }), | ||
| ); | ||
| }, | ||
|
|
||
| /** Counter for messages received from other federated servers */ | ||
| get federatedMessagesReceived() { | ||
| return getOrCreateMetric( | ||
| 'rocketchat_federation_messages_received', | ||
| () => | ||
| new client.Counter({ | ||
| name: 'rocketchat_federation_messages_received', | ||
| labelNames: ['message_type', 'origin'], | ||
| help: 'Total federated messages received', | ||
| registers: [registry], | ||
| }), | ||
| ); | ||
| }, | ||
|
|
||
| /** Counter for rooms joined */ | ||
| get federatedRoomsJoined() { | ||
| return getOrCreateMetric( | ||
| 'rocketchat_federation_rooms_joined', | ||
| () => | ||
| new client.Counter({ | ||
| name: 'rocketchat_federation_rooms_joined', | ||
| labelNames: ['origin'], | ||
| help: 'Total federated rooms joined', | ||
| registers: [registry], | ||
| }), | ||
| ); | ||
| }, | ||
|
|
||
| /** Duration to process incoming federation transaction */ | ||
| get federationTransactionProcessDuration() { | ||
| return getOrCreateMetric( | ||
| 'rocketchat_federation_transaction_process_duration_seconds', | ||
| () => | ||
| new client.Summary({ | ||
| name: 'rocketchat_federation_transaction_process_duration_seconds', | ||
| labelNames: ['pdu_count', 'edu_count', 'origin'], | ||
| help: 'Time to process incoming federation transaction', | ||
| percentiles, | ||
| registers: [registry], | ||
| }), | ||
| ); | ||
| }, | ||
dhulke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** Duration to process incoming federated message */ | ||
| get federationIncomingMessageProcessDuration() { | ||
| return getOrCreateMetric( | ||
| 'rocketchat_federation_incoming_message_process_duration_seconds', | ||
| () => | ||
| new client.Summary({ | ||
| name: 'rocketchat_federation_incoming_message_process_duration_seconds', | ||
| labelNames: ['message_type'], | ||
| help: 'Time to process incoming federated message', | ||
| percentiles, | ||
| registers: [registry], | ||
| }), | ||
| ); | ||
| }, | ||
|
|
||
| /** Duration to join a federated room */ | ||
| get federationRoomJoinDuration() { | ||
| return getOrCreateMetric( | ||
| 'rocketchat_federation_room_join_duration_seconds', | ||
| () => | ||
| new client.Summary({ | ||
| name: 'rocketchat_federation_room_join_duration_seconds', | ||
| labelNames: ['origin'], | ||
| help: 'Time to join a federated room (invite acceptance)', | ||
| percentiles, | ||
| registers: [registry], | ||
| }), | ||
| ); | ||
| }, | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.