dev: update/cleanup PerformanceMonitor

This commit is contained in:
KernelDeimos 2024-12-27 13:15:55 -05:00
parent 33d094322f
commit 5e844205b3
4 changed files with 22 additions and 75 deletions

View File

@ -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);
};

View File

@ -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 += '/';

View File

@ -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);

View File

@ -17,6 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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,
};