11import { Injectable , OnModuleInit } from '@nestjs/common' ;
2- import { Registry , collectDefaultMetrics , Histogram , Gauge } from 'prom-client' ;
2+ import { Registry , collectDefaultMetrics , Histogram , Gauge , Counter } from 'prom-client' ;
33
44@Injectable ( )
55export class MetricsCollectionService implements OnModuleInit {
66 private registry : Registry ;
77 public httpRequestDuration : Histogram ;
88 public dbQueryDuration : Histogram ;
99 public activeConnections : Gauge ;
10+ public userRegistrations : Counter ;
11+ public assessmentCompletions : Counter ;
12+ public learningPathProgress : Gauge ;
13+ public cacheHitRate : Gauge ;
14+ public queueProcessingTime : Histogram ;
15+ public emailCampaignsSent : Counter ;
16+ public backupOperations : Counter ;
1017
1118 constructor ( ) {
1219 this . registry = new Registry ( ) ;
@@ -35,6 +42,63 @@ export class MetricsCollectionService implements OnModuleInit {
3542 help : 'Number of active connections' ,
3643 registers : [ this . registry ] ,
3744 } ) ;
45+
46+ // User Registrations Counter
47+ this . userRegistrations = new Counter ( {
48+ name : 'user_registrations_total' ,
49+ help : 'Total number of user registrations' ,
50+ labelNames : [ 'user_type' , 'source' ] ,
51+ registers : [ this . registry ] ,
52+ } ) ;
53+
54+ // Assessment Completions Counter
55+ this . assessmentCompletions = new Counter ( {
56+ name : 'assessment_completions_total' ,
57+ help : 'Total number of assessment completions' ,
58+ labelNames : [ 'assessment_type' , 'difficulty' ] ,
59+ registers : [ this . registry ] ,
60+ } ) ;
61+
62+ // Learning Path Progress Gauge
63+ this . learningPathProgress = new Gauge ( {
64+ name : 'learning_path_progress_percentage' ,
65+ help : 'Average learning path progress percentage' ,
66+ labelNames : [ 'path_id' , 'user_id' ] ,
67+ registers : [ this . registry ] ,
68+ } ) ;
69+
70+ // Cache Hit Rate Gauge
71+ this . cacheHitRate = new Gauge ( {
72+ name : 'cache_hit_rate_percentage' ,
73+ help : 'Cache hit rate percentage' ,
74+ labelNames : [ 'cache_type' ] ,
75+ registers : [ this . registry ] ,
76+ } ) ;
77+
78+ // Queue Processing Time Histogram
79+ this . queueProcessingTime = new Histogram ( {
80+ name : 'queue_processing_duration_seconds' ,
81+ help : 'Duration of queue job processing in seconds' ,
82+ labelNames : [ 'queue_name' , 'job_type' ] ,
83+ buckets : [ 0.1 , 0.5 , 1 , 2 , 5 , 10 , 30 ] ,
84+ registers : [ this . registry ] ,
85+ } ) ;
86+
87+ // Email Campaigns Sent Counter
88+ this . emailCampaignsSent = new Counter ( {
89+ name : 'email_campaigns_sent_total' ,
90+ help : 'Total number of email campaigns sent' ,
91+ labelNames : [ 'campaign_type' , 'status' ] ,
92+ registers : [ this . registry ] ,
93+ } ) ;
94+
95+ // Backup Operations Counter
96+ this . backupOperations = new Counter ( {
97+ name : 'backup_operations_total' ,
98+ help : 'Total number of backup operations' ,
99+ labelNames : [ 'operation_type' , 'status' ] ,
100+ registers : [ this . registry ] ,
101+ } ) ;
38102 }
39103
40104 onModuleInit ( ) {
@@ -57,4 +121,33 @@ export class MetricsCollectionService implements OnModuleInit {
57121 recordDbQuery ( queryType : string , table : string , duration : number ) {
58122 this . dbQueryDuration . observe ( { query_type : queryType , table } , duration ) ;
59123 }
124+
125+ // Custom business metrics methods
126+ recordUserRegistration ( userType : string , source : string ) {
127+ this . userRegistrations . inc ( { user_type : userType , source } ) ;
128+ }
129+
130+ recordAssessmentCompletion ( assessmentType : string , difficulty : string ) {
131+ this . assessmentCompletions . inc ( { assessment_type : assessmentType , difficulty } ) ;
132+ }
133+
134+ updateLearningPathProgress ( pathId : string , userId : string , progress : number ) {
135+ this . learningPathProgress . set ( { path_id : pathId , user_id : userId } , progress ) ;
136+ }
137+
138+ updateCacheHitRate ( cacheType : string , hitRate : number ) {
139+ this . cacheHitRate . set ( { cache_type : cacheType } , hitRate ) ;
140+ }
141+
142+ recordQueueProcessingTime ( queueName : string , jobType : string , duration : number ) {
143+ this . queueProcessingTime . observe ( { queue_name : queueName , job_type : jobType } , duration ) ;
144+ }
145+
146+ recordEmailCampaignSent ( campaignType : string , status : string ) {
147+ this . emailCampaignsSent . inc ( { campaign_type : campaignType , status } ) ;
148+ }
149+
150+ recordBackupOperation ( operationType : string , status : string ) {
151+ this . backupOperations . inc ( { operation_type : operationType , status } ) ;
152+ }
60153}
0 commit comments