cloudwatch-metrics icon indicating copy to clipboard operation
cloudwatch-metrics copied to clipboard

Help tracking down memory leak

Open addshore opened this issue 1 year ago • 0 comments

I started using this lib a week or so ago, and it seems to have introduced a memory leak into my application.

In about 24 hours the memory reaches 100% usage, processes fall over and restart

image

My usage is rather simple.

in my main.js I require and setup cloudmetrics

var cloudwatchMetrics = require('cloudwatch-metrics');
cloudwatchMetrics.initialize({
	region: 'XXX',
  credentials: {
    accessKeyId: 'XXX',
    secretAccessKey: 'XXX'
  }
});

And then also in main.js I register some middleware for my app which records metrics

app.middleware('routes:before', function(req, res, next) {
  let path = req.originalUrl
  let method = req.method;
  // Strip any query string
  path = path.split('?')[0];
  // Replace all digits with a placeholder to make the metric more readable
  path = path.replace(/\d+/g, '0');
  // Replace any GUIDs too
  path = path.replace(/[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}/g, '00000000-0000-0000-0000-000000000000');
  // Replace any UUIDs too
  path = path.replace(/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{16}/g, '00000000-0000-0000-0000-000000000000');

  let metric = new cloudwatchMetrics.Metric('api-metrics', 'Count', [
    { Name: 'method', Value: method },
    { Name: 'path', Value: path }
  ]);
  metric.put(1, method + ":" + path, "Count", { method: method, path: path})
  next();
})

Is there anything I am missing in this setup to avoid leaky memory? or any area I should be looking at to debug further?

addshore avatar Sep 13 '23 08:09 addshore