Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Constants
const PORT = 5000;
const HOST = '0.0.0.0';
// App
const client = require('prom-client');
const collectDefaultMetrics = client.collectDefaultMetrics;
// Probe every 5th second.
collectDefaultMetrics({ timeout: 5000 });
const counter = new client.Counter({
name: 'node_request_operations_total',
help: 'The total number of processed requests'
});
const histogram = new client.Histogram({
name: 'node_request_duration_seconds',
help: 'Histogram for the duration in seconds.',
buckets: [1, 2, 5, 6, 10]
});
const app = express();
app.get('/', (req, res) => {
//Simulate a sleep
var start = new Date()
var simulateTime = 1000
setTimeout(function(argument) {
// execution time simulated with setTimeout function
var end = new Date() - start
histogram.observe(end / 1000); //convert to seconds
else
return match[1].toLowerCase();
}
const dbQueryTotal = new Prometheus.Counter({
name: 'db_queries_total',
help: 'Count the number of DB queries (grouped by db command (select, insert, update, etc.), unfilled db query)',
labelNames: ['command', 'query'],
});
const dbFailuresTotal = new Prometheus.Counter({
name: 'db_query_error_total',
help: 'Count the number of DB errors (grouped by db command (select, insert, update, etc.), unfilled db query)',
labelNames: ['command', 'query', 'sqlState'],
});
const dbQueryDuration = new Prometheus.Histogram({
name: 'db_query_duration_ms',
help: 'Log db query duration (grouped by db command (select, insert, update, etc.), unfilled db query)',
labelNames: ['command', 'query'],
buckets: [0.10, 5, 15, 50, 100, 200, 300, 400, 500, 1000, 2000, 3000, 4000, 5000] // buckets for query duration time from 0.1ms to 5s
});
const dbTransactionTotal = new Prometheus.Counter({
name: 'db_transactions_total',
help: 'Count the number of DB transactions',
labelNames: [],
});
const dbRollbackTotal = new Prometheus.Counter({
name: 'db_transaction_rollback_total',
help: 'Count the number of DB rollbacks',
labelNames: [],
});
const connectionsCounter = new Counter({
name: 'db_pool_connections',
help: 'Number of established connections in all time',
labelNames: ['poolName'],
});
const acquireDurationsHistogram = new Histogram({
name: 'db_pool_acquire_time',
help: 'Time required to acquire a connection',
labelNames: ['poolName'],
buckets: [0.003, 0.005, 0.01, 0.05, 0.1, 0.3]});
// const sqlExecutionDurationsHistogram = new Histogram({
// name: 'db_sql_execute_time',
// help: 'Time required to execute an SQL statement',
// labelNames: ['poolName', 'readonly'],
// buckets: [0.003, 0.005, 0.01, 0.05, 0.1, 0.3, 1, 5]});
const transactionExecutionDurationsHistogram = new Histogram({
name: 'db_transaction_execute_time',
help: 'Time required to execute a transaction',
labelNames: ['poolName', 'readonly'],
buckets: [0.003, 0.005, 0.01, 0.05, 0.1, 0.3, 1, 5]});
class MetricsAwareConnectionPoolWrapper implements ConnectionPool {
instance: mysql.IPool;
active: Gauge.Internal;
numConnections: Counter.Internal;
enqueueTimes: Array;
durationHistogram: Summary.Internal; // todo
config: MySQLPoolConfig;
constructor(instance: mysql.IPool, name: string) {
this.instance = instance;
help: 'How long channel initial data was loaded',
buckets: TIMES
})
let errorCount = new prometheus.Counter({
name: 'logux_errors_counter',
help: 'How many server errors was fired',
labelNames: ['name']
})
let clientCount = new prometheus.Gauge({
name: 'logux_clients_gauge',
help: 'How many clients are online'
})
let backendAccessTime = new prometheus.Histogram({
name: 'logux_backend_access_time_histogram',
help: 'How long it takes for backend to grant access',
buckets: TIMES
})
let backendProcessTime = new prometheus.Histogram({
name: 'logux_backend_responce_time_histogram',
help: 'How long it takes for backend to process action or subscriptions',
buckets: TIMES
})
function bindPrometheus (app) {
app.controls['/prometheus'] = {
request () {
return {
headers: {
_metricsOperationDurationSeconds () {
let operationDurationSeconds = this._registry.getSingleMetric(METRICS_NAME_OPERATION_DURATION_SECONDS)
if (!operationDurationSeconds) {
operationDurationSeconds = new Prometheus.Histogram({
name: METRICS_NAME_OPERATION_DURATION_SECONDS,
help: 'Duration of operations in second',
labelNames: ['parent_service', 'name'],
buckets: DURATION_HISTOGRAM_BUCKETS,
registers: [this._registry]
})
}
return operationDurationSeconds
}
_metricshttpRequestDurationSeconds () {
let httpRequestDurationSeconds = this._registry.getSingleMetric(METRICS_NAME_HTTP_REQUEST_HANDLER_DURATION_SECONDS)
if (!httpRequestDurationSeconds) {
httpRequestDurationSeconds = new Prometheus.Histogram({
name: METRICS_NAME_HTTP_REQUEST_HANDLER_DURATION_SECONDS,
help: 'Duration of HTTP requests in second',
labelNames: ['parent_service', 'method', 'code'],
buckets: DURATION_HISTOGRAM_BUCKETS,
registers: [this._registry]
})
}
return httpRequestDurationSeconds
}
}
case AggregationType.COUNT:
metric = new Counter(metricObj);
break;
case AggregationType.SUM:
case AggregationType.LAST_VALUE:
metric = new Gauge(metricObj);
break;
case AggregationType.DISTRIBUTION:
this.validateDisallowedLeLabelForHistogram(labelNames);
const distribution = {
name: metricName,
help: view.description,
labelNames,
buckets: this.getBoundaries(view, tags),
};
metric = new Histogram(distribution);
break;
default:
this.logger.error(`Aggregation ${view.aggregation} is not supported`);
}
this.registry.registerMetric(metric);
return metric;
}
labelNames: ['operation_type', 'name']
})
const resolverRequestsMetric = new Prometheus.Counter({
name: 'requests_resolved',
help: 'Number of requests resolved by server',
labelNames: ['operation_type', 'path', 'success', 'authenticated']
})
const resolverRequestsTotalMetric = new Prometheus.Counter({
name: 'requests_resolved_total',
help: 'Number of requests resolved by server in total',
labelNames: ['operation_type', 'path', 'success', 'authenticated']
})
const serverResponseMetric = new Prometheus.Histogram({
name: 'server_response_ms',
help: 'Server response time in milliseconds',
labelNames: ['request_type', 'error']
})
const conflictsMetric = new Prometheus.Counter({
name: 'conflicts',
help: 'Number of conflicts happened',
labelNames: ['operation_type', 'name']
})
const clientsMetric = new Prometheus.Counter({
name: 'clients',
help: 'Number of clients',
labelNames: ['client_id']
})