@@ -26,6 +26,9 @@ import { env } from "~/env.server";
26
26
import { AuthenticatedEnvironment } from "~/services/apiAuth.server" ;
27
27
import { logger } from "~/services/logger.server" ;
28
28
import { DynamicFlushScheduler } from "./dynamicFlushScheduler.server" ;
29
+ import { singleton } from "~/utils/singleton" ;
30
+ import { Gauge } from "prom-client" ;
31
+ import { metricsRegister } from "~/metrics.server" ;
29
32
30
33
export type CreatableEvent = Omit <
31
34
Prisma . TaskEventCreateInput ,
@@ -148,6 +151,11 @@ export class EventRepository {
148
151
private readonly _flushScheduler : DynamicFlushScheduler < CreatableEvent > ;
149
152
private _randomIdGenerator = new RandomIdGenerator ( ) ;
150
153
private _redisPublishClient : Redis ;
154
+ private _subscriberCount = 0 ;
155
+
156
+ get subscriberCount ( ) {
157
+ return this . _subscriberCount ;
158
+ }
151
159
152
160
constructor ( private db : PrismaClient = prisma , private readonly _config : EventRepoConfig ) {
153
161
this . _flushScheduler = new DynamicFlushScheduler ( {
@@ -721,6 +729,9 @@ export class EventRepository {
721
729
// Subscribe to the channel.
722
730
await redis . psubscribe ( channel ) ;
723
731
732
+ // Increment the subscriber count.
733
+ this . _subscriberCount ++ ;
734
+
724
735
const eventEmitter = new EventEmitter ( ) ;
725
736
726
737
// Define the message handler.
@@ -733,6 +744,8 @@ export class EventRepository {
733
744
// Return a function that can be used to unsubscribe.
734
745
const unsubscribe = async ( ) => {
735
746
await redis . punsubscribe ( channel ) ;
747
+ redis . quit ( ) ;
748
+ this . _subscriberCount -- ;
736
749
} ;
737
750
738
751
return {
@@ -796,19 +809,34 @@ export class EventRepository {
796
809
}
797
810
}
798
811
799
- export const eventRepository = new EventRepository ( prisma , {
800
- batchSize : env . EVENTS_BATCH_SIZE ,
801
- batchInterval : env . EVENTS_BATCH_INTERVAL ,
802
- retentionInDays : env . EVENTS_DEFAULT_LOG_RETENTION ,
803
- redis : {
804
- port : env . REDIS_PORT ,
805
- host : env . REDIS_HOST ,
806
- username : env . REDIS_USERNAME ,
807
- password : env . REDIS_PASSWORD ,
808
- enableAutoPipelining : true ,
809
- ...( env . REDIS_TLS_DISABLED === "true" ? { } : { tls : { } } ) ,
810
- } ,
811
- } ) ;
812
+ export const eventRepository = singleton ( "eventRepo" , initializeEventRepo ) ;
813
+
814
+ function initializeEventRepo ( ) {
815
+ const repo = new EventRepository ( prisma , {
816
+ batchSize : env . EVENTS_BATCH_SIZE ,
817
+ batchInterval : env . EVENTS_BATCH_INTERVAL ,
818
+ retentionInDays : env . EVENTS_DEFAULT_LOG_RETENTION ,
819
+ redis : {
820
+ port : env . REDIS_PORT ,
821
+ host : env . REDIS_HOST ,
822
+ username : env . REDIS_USERNAME ,
823
+ password : env . REDIS_PASSWORD ,
824
+ enableAutoPipelining : true ,
825
+ ...( env . REDIS_TLS_DISABLED === "true" ? { } : { tls : { } } ) ,
826
+ } ,
827
+ } ) ;
828
+
829
+ new Gauge ( {
830
+ name : "event_repository_subscriber_count" ,
831
+ help : "Number of event repository subscribers" ,
832
+ collect ( ) {
833
+ this . set ( repo . subscriberCount ) ;
834
+ } ,
835
+ registers : [ metricsRegister ] ,
836
+ } ) ;
837
+
838
+ return repo ;
839
+ }
812
840
813
841
export function stripAttributePrefix ( attributes : Attributes , prefix : string ) {
814
842
const result : Attributes = { } ;
0 commit comments