Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 94 additions & 1 deletion src/monitoring/metrics/metrics-collection.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { Injectable, OnModuleInit } from '@nestjs/common';
import { Registry, collectDefaultMetrics, Histogram, Gauge } from 'prom-client';
import { Registry, collectDefaultMetrics, Histogram, Gauge, Counter } from 'prom-client';

@Injectable()
export class MetricsCollectionService implements OnModuleInit {
private registry: Registry;
public httpRequestDuration: Histogram;
public dbQueryDuration: Histogram;
public activeConnections: Gauge;
public userRegistrations: Counter;
public assessmentCompletions: Counter;
public learningPathProgress: Gauge;
public cacheHitRate: Gauge;
public queueProcessingTime: Histogram;
public emailCampaignsSent: Counter;
public backupOperations: Counter;

constructor() {
this.registry = new Registry();
Expand Down Expand Up @@ -35,6 +42,63 @@ export class MetricsCollectionService implements OnModuleInit {
help: 'Number of active connections',
registers: [this.registry],
});

// User Registrations Counter
this.userRegistrations = new Counter({
name: 'user_registrations_total',
help: 'Total number of user registrations',
labelNames: ['user_type', 'source'],
registers: [this.registry],
});

// Assessment Completions Counter
this.assessmentCompletions = new Counter({
name: 'assessment_completions_total',
help: 'Total number of assessment completions',
labelNames: ['assessment_type', 'difficulty'],
registers: [this.registry],
});

// Learning Path Progress Gauge
this.learningPathProgress = new Gauge({
name: 'learning_path_progress_percentage',
help: 'Average learning path progress percentage',
labelNames: ['path_id', 'user_id'],
registers: [this.registry],
});

// Cache Hit Rate Gauge
this.cacheHitRate = new Gauge({
name: 'cache_hit_rate_percentage',
help: 'Cache hit rate percentage',
labelNames: ['cache_type'],
registers: [this.registry],
});

// Queue Processing Time Histogram
this.queueProcessingTime = new Histogram({
name: 'queue_processing_duration_seconds',
help: 'Duration of queue job processing in seconds',
labelNames: ['queue_name', 'job_type'],
buckets: [0.1, 0.5, 1, 2, 5, 10, 30],
registers: [this.registry],
});

// Email Campaigns Sent Counter
this.emailCampaignsSent = new Counter({
name: 'email_campaigns_sent_total',
help: 'Total number of email campaigns sent',
labelNames: ['campaign_type', 'status'],
registers: [this.registry],
});

// Backup Operations Counter
this.backupOperations = new Counter({
name: 'backup_operations_total',
help: 'Total number of backup operations',
labelNames: ['operation_type', 'status'],
registers: [this.registry],
});
}

onModuleInit() {
Expand All @@ -57,4 +121,33 @@ export class MetricsCollectionService implements OnModuleInit {
recordDbQuery(queryType: string, table: string, duration: number) {
this.dbQueryDuration.observe({ query_type: queryType, table }, duration);
}

// Custom business metrics methods
recordUserRegistration(userType: string, source: string) {
this.userRegistrations.inc({ user_type: userType, source });
}

recordAssessmentCompletion(assessmentType: string, difficulty: string) {
this.assessmentCompletions.inc({ assessment_type: assessmentType, difficulty });
}

updateLearningPathProgress(pathId: string, userId: string, progress: number) {
this.learningPathProgress.set({ path_id: pathId, user_id: userId }, progress);
}

updateCacheHitRate(cacheType: string, hitRate: number) {
this.cacheHitRate.set({ cache_type: cacheType }, hitRate);
}

recordQueueProcessingTime(queueName: string, jobType: string, duration: number) {
this.queueProcessingTime.observe({ queue_name: queueName, job_type: jobType }, duration);
}

recordEmailCampaignSent(campaignType: string, status: string) {
this.emailCampaignsSent.inc({ campaign_type: campaignType, status });
}

recordBackupOperation(operationType: string, status: string) {
this.backupOperations.inc({ operation_type: operationType, status });
}
}
102 changes: 101 additions & 1 deletion src/monitoring/monitoring.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Controller, Get, Res, Query } from '@nestjs/common';

Check failure on line 1 in src/monitoring/monitoring.controller.ts

View workflow job for this annotation

GitHub Actions / TypeScript Type Check

Duplicate identifier 'Res'.

Check failure on line 1 in src/monitoring/monitoring.controller.ts

View workflow job for this annotation

GitHub Actions / TypeScript Type Check

Duplicate identifier 'Get'.

Check failure on line 1 in src/monitoring/monitoring.controller.ts

View workflow job for this annotation

GitHub Actions / TypeScript Type Check

Duplicate identifier 'Controller'.
import { Controller, Get, Res, VERSION_NEUTRAL, Version } from '@nestjs/common';

Check failure on line 2 in src/monitoring/monitoring.controller.ts

View workflow job for this annotation

GitHub Actions / TypeScript Type Check

Duplicate identifier 'Res'.

Check failure on line 2 in src/monitoring/monitoring.controller.ts

View workflow job for this annotation

GitHub Actions / TypeScript Type Check

Duplicate identifier 'Get'.

Check failure on line 2 in src/monitoring/monitoring.controller.ts

View workflow job for this annotation

GitHub Actions / TypeScript Type Check

Duplicate identifier 'Controller'.

Check failure on line 2 in src/monitoring/monitoring.controller.ts

View workflow job for this annotation

GitHub Actions / ESLint

'@nestjs/common' import is duplicated
import { MetricsCollectionService } from './metrics/metrics-collection.service';
import { Response } from 'express';
import { ScheduledTaskMonitoringService } from './scheduled-task-monitoring.service';
Expand All @@ -9,7 +10,7 @@
constructor(
private readonly metricsService: MetricsCollectionService,
private readonly scheduledTaskMonitoringService: ScheduledTaskMonitoringService,
) {}
) { }

Check failure on line 13 in src/monitoring/monitoring.controller.ts

View workflow job for this annotation

GitHub Actions / ESLint

Delete `·`

@Get()
async getMetrics(@Res() res: Response) {
Expand All @@ -18,6 +19,105 @@
res.send(metrics);
}

@Get('unified')
async getUnifiedMetrics(
@Query('format') format?: string,
@Query('include') include?: string,
@Query('exclude') exclude?: string,
) {
const includeTypes = include?.split(',').map(s => s.trim()) || [];

Check failure on line 28 in src/monitoring/monitoring.controller.ts

View workflow job for this annotation

GitHub Actions / ESLint

Replace `s` with `(s)`
const excludeTypes = exclude?.split(',').map(s => s.trim()) || [];

Check failure on line 29 in src/monitoring/monitoring.controller.ts

View workflow job for this annotation

GitHub Actions / ESLint

Replace `s` with `(s)`

// Get base Prometheus metrics
const prometheusMetrics = await this.metricsService.getMetrics();

// Get scheduled tasks dashboard
const scheduledTasksMetrics = this.scheduledTaskMonitoringService.getDashboard();

// Aggregate metrics from different sources
const unifiedMetrics = {
prometheus: prometheusMetrics,
scheduledTasks: scheduledTasksMetrics,
timestamp: new Date().toISOString(),
metadata: {
totalMetrics: prometheusMetrics.split('\n').filter(line => line && !line.startsWith('#')).length,

Check failure on line 43 in src/monitoring/monitoring.controller.ts

View workflow job for this annotation

GitHub Actions / ESLint

Replace `(line·=>·line·&&·!line.startsWith('#'))` with `((line)·=>·line·&&·!line.startsWith('#'))⏎··········`
includeTypes,
excludeTypes,
}

Check failure on line 46 in src/monitoring/monitoring.controller.ts

View workflow job for this annotation

GitHub Actions / ESLint

Insert `,`
};

// Return in requested format
if (format === 'json') {
return unifiedMetrics;
}

// Default to Prometheus format
const metrics = await this.metricsService.getMetrics();
return metrics;
}

@Get('health')
async getMetricsHealth() {
return {
status: 'healthy',
timestamp: new Date().toISOString(),
services: {
metricsCollection: 'active',
scheduledTasks: 'active',
},
registry: {
metricsCount: (await this.metricsService.getMetrics()).split('\n').filter(line => line && !line.startsWith('#')).length,

Check failure on line 69 in src/monitoring/monitoring.controller.ts

View workflow job for this annotation

GitHub Actions / ESLint

Replace `.split('\n').filter(line` with `⏎··········.split('\n')⏎··········.filter((line)`
}

Check failure on line 70 in src/monitoring/monitoring.controller.ts

View workflow job for this annotation

GitHub Actions / ESLint

Insert `,`
};
}

@Get('custom')
async getCustomMetrics(@Query('type') type?: string) {
const customMetrics = {
user_registrations: {
name: 'user_registrations_total',
help: 'Total number of user registrations',
type: 'counter',
},
assessment_completions: {
name: 'assessment_completions_total',
help: 'Total number of assessment completions',
type: 'counter',
},
learning_path_progress: {
name: 'learning_path_progress_percentage',
help: 'Average learning path progress percentage',
type: 'gauge',
},
cache_hit_rate: {
name: 'cache_hit_rate_percentage',
help: 'Cache hit rate percentage',
type: 'gauge',
},
queue_processing_time: {
name: 'queue_processing_duration_seconds',
help: 'Duration of queue job processing in seconds',
type: 'histogram',
},
email_campaigns_sent: {
name: 'email_campaigns_sent_total',
help: 'Total number of email campaigns sent',
type: 'counter',
},
backup_operations: {
name: 'backup_operations_total',
help: 'Total number of backup operations',
type: 'counter',
},
};

if (type) {
return customMetrics[type] || { error: 'Metric type not found' };
}

return customMetrics;
}

@Get('scheduled-tasks/dashboard')
getScheduledTasksDashboard() {
return this.scheduledTaskMonitoringService.getDashboard();
Expand Down
Loading