From 5e844205b30ff41681915437c6599a9be14607de Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Fri, 27 Dec 2024 13:15:55 -0500 Subject: [PATCH] dev: update/cleanup PerformanceMonitor --- src/backend/src/CoreModule.js | 8 +- .../src/filesystem/FilesystemService.js | 3 +- src/backend/src/helpers.js | 3 +- src/backend/src/monitor/PerformanceMonitor.js | 83 ++++--------------- 4 files changed, 22 insertions(+), 75 deletions(-) diff --git a/src/backend/src/CoreModule.js b/src/backend/src/CoreModule.js index c970d891..d921bca0 100644 --- a/src/backend/src/CoreModule.js +++ b/src/backend/src/CoreModule.js @@ -364,10 +364,12 @@ const install = async ({ services, app, useapi, modapi }) => { const { SNSService } = require('./services/SNSService'); services.registerService('sns', SNSService); + + const { PerformanceMonitor } = require('./monitor/PerformanceMonitor'); + services.registerService('performance-monitor', PerformanceMonitor); } const install_legacy = async ({ services }) => { - const PerformanceMonitor = require('./monitor/PerformanceMonitor'); const { OperationTraceService } = require('./services/OperationTraceService'); const { ClientOperationService } = require('./services/ClientOperationService'); const { EngPortalService } = require('./services/EngPortalService'); @@ -382,8 +384,4 @@ const install_legacy = async ({ services }) => { services.registerService('app-information', AppInformationService); services.registerService('engineering-portal', EngPortalService); - // This singleton was made before services existed, - // so we have to pass that to it manually - PerformanceMonitor.provideServices(services); - }; diff --git a/src/backend/src/filesystem/FilesystemService.js b/src/backend/src/filesystem/FilesystemService.js index 03548146..11ea3f11 100644 --- a/src/backend/src/filesystem/FilesystemService.js +++ b/src/backend/src/filesystem/FilesystemService.js @@ -341,7 +341,8 @@ class FilesystemService extends BaseService { } async update_child_paths (old_path, new_path, user_id) { - const monitor = PerformanceMonitor.createContext('update_child_paths'); + const svc_performanceMonitor = this.services.get('performance-monitor'); + const monitor = svc_performanceMonitor.createContext('update_child_paths'); if ( ! old_path.endsWith('/') ) old_path += '/'; if ( ! new_path.endsWith('/') ) new_path += '/'; diff --git a/src/backend/src/helpers.js b/src/backend/src/helpers.js index 53122d6c..6c405131 100644 --- a/src/backend/src/helpers.js +++ b/src/backend/src/helpers.js @@ -1279,7 +1279,8 @@ function seconds_to_string(seconds) { * @param {*} options */ async function suggest_app_for_fsentry(fsentry, options){ - const monitor = PerformanceMonitor.createContext("suggest_app_for_fsentry"); + const svc_performanceMonitor = services.get('performance-monitor'); + const monitor = svc_performanceMonitor.createContext("suggest_app_for_fsentry"); const suggested_apps = []; let content_type = mime.contentType(fsentry.name); diff --git a/src/backend/src/monitor/PerformanceMonitor.js b/src/backend/src/monitor/PerformanceMonitor.js index 2122f579..6042057d 100644 --- a/src/backend/src/monitor/PerformanceMonitor.js +++ b/src/backend/src/monitor/PerformanceMonitor.js @@ -17,6 +17,7 @@ * along with this program. If not, see . */ const config = require("../config"); +const BaseService = require("../services/BaseService"); class Metric { constructor (windowSize) { @@ -121,15 +122,15 @@ class PerformanceMonitorContext { } } -module.exports = class PerformanceMonitor { - static instance_ = null; - - constructor () { +class PerformanceMonitor extends BaseService { + _construct () { this.performanceMetrics = {}; this.operationCounts = {}; this.lastCountPollTS = Date.now(); + } + _init () { if ( config.cloudwatch ) { const AWS = require('aws-sdk'); this.cw = new AWS.CloudWatch(config.cloudwatch); @@ -146,26 +147,6 @@ module.exports = class PerformanceMonitor { } } - /* - PerformanceMonitor was written before the services container - existed, so we have to provide the services to it manually. - */ - static provideServices (services) { - this.services = services; - } - - static getInstance () { - if ( PerformanceMonitor.instance_ ) { - return PerformanceMonitor.instance_; - } - - return PerformanceMonitor.instance_ = new PerformanceMonitor(); - } - - static createContext (...a) { - return this.getInstance().createContext(...a); - } - createContext (name) { return new PerformanceMonitorContext({ performanceMonitor: this, @@ -228,6 +209,7 @@ module.exports = class PerformanceMonitor { } async recordMetrics_ () { + this.log.info('recording metrics'); // Only record metrics of CloudWatch is enabled if ( ! this.cw ) return; @@ -268,6 +250,11 @@ module.exports = class PerformanceMonitor { } this.lastCountPollTS = ts; + if ( MetricData.length === 0 ) { + this.log.info('no metrics to record'); + return; + } + const params = { Namespace: 'heyputer', MetricData, @@ -287,48 +274,8 @@ module.exports = class PerformanceMonitor { ) } } - async recordSingleMetric_ (name, value) { - if ( Number.isNaN(value) ) { - // occurs for averages of zero items - return; - } - - // Make names easier to read - name = name.replace(/\s+/g, '-'); - - const params = { - Namespace: "heyputer", - MetricData: [ - { - MetricName: name, - Value: value, - Timestamp: Math.floor(Date.now() / 1000), - Unit: "Milliseconds", - Dimensions: { - 'server-id': config.server_id, - 'environment': config.env, - } - }, - ], - } - try { - await this.cw.putMetricData(params).promise(); - } catch (e) { - if ( ! this.services ) { - console.error( - 'PerformanceMonitor had an error before services were available', - e, - ); - return; - } - - const log = this.services.get('log').create('PM'); - const errors = this.services.get('error-service').create(log); - errors.report('CloudWatch.putMetricData', { - source: e, - trace: true, - alarm: true, - }); - } - } } + +module.exports = { + PerformanceMonitor, +};