Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dnlup committed Nov 22, 2021
1 parent be95017 commit a1a5e2f
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const { hrtime } = require('process');
const fp = require('fastify-plugin');
const { default: Client } = require('@immobiliarelabs/dats');
const doc = require('@dnlup/doc');
Expand All @@ -20,6 +21,12 @@ function clientMock() {
return mock;
}

function validateString(value) {
if (typeof value !== 'string' || value === '') {
throw new Error('A string value is required to name the metric');
}
}

function sendHealthData(
{ eventLoopUtilization, eventLoopDelay, memory, cpu },
stats
Expand Down Expand Up @@ -55,6 +62,24 @@ function errorsCounter(request, reply, error, done) {
done();
}

function timeAsyncFunction(name, fn, context = null, ...args) {
validateString(name);
const start = hrtime.bigint();
const done = () => {
const end = hrtime.bigint();
const value = end - start;
sendTiming(name, value);
};

const result = fn.call(context, args);
if (result && typeof result.then === 'function') {
return result.then(done, done);
} else {
done();
return result;
}
}

/**
* Default metrics that are enabled
*/
Expand Down Expand Up @@ -166,6 +191,65 @@ module.exports = fp(
fastify.addHook('onError', errorsCounter);
}

function sendTiming(name, value) {
stats.timing(name, value);
}

function createAsyncTimedFunction(name, fn, context = null) {
return function asyncTimedFunction(...args) {};
}

function createCbTimedFunction() {
return function cbTimedFunction(...args) {};
}

function createSyncTimedFunction() {
return function syncTimedFunction(...args) {};
}

function timeAsyncFunction(name, fn, context = null, ...args) {
validateString(name);
const done = () => {
const end = hrtime.bigint();
const value = end - start;
sendTiming(name, value);
};
const result = fn.call(context, args);
if (result && typeof result.then === 'function') {
result.then(done, done);
} else {
return Promise.reject(
new Error('The timed function must return a Promise.')
);
}
return result;
}

function timeCbFunction(name, fn, context = null, ...args) {
validateString(name);
const cb = args[args.length - 1];
if (typeof cb !== 'function') {
throw new Error('cb must me a function');
}
const start = hrtime.bigint();
args[args.length - 1] = (err, ...v) => {
const end = hrtime.bigint();
const value = end - start;
sendTiming(name, value);
cb(err, ...v);
};
return fn.call(context, args);
}

function timeFunction(name, fn, context = null, ...args) {
validateString(name);
const start = hrtime.bigint();
const result = fn.call(context, args);
const end = hrtime.bigint();
const value = end - start;
sendTiming(name, value);
return result;
}
next();
},
{
Expand Down

0 comments on commit a1a5e2f

Please sign in to comment.