Skip to content

Instantly share code, notes, and snippets.

@guigaoliveira
Created July 25, 2024 17:05
Show Gist options
  • Save guigaoliveira/ee94056c547402a07657a2ca45d31f47 to your computer and use it in GitHub Desktop.
Save guigaoliveira/ee94056c547402a07657a2ca45d31f47 to your computer and use it in GitHub Desktop.
import { Histogram } from 'prom-client';
import { PrometheusDriver } from 'prometheus-query';
function buildMetricName() {
return `in_pix_decoded_pix_duration_milliseconds`;
}
function resetHistogram(name: string) {
const histogram = register.getSingleMetric(name) as Histogram;
if (!histogram) return;
histogram.reset();
}
const express = require('express');
const promClient = require('prom-client');
const app = express();
const port = 3000;
// Crie um registro para coletar métricas padrão
const collectDefaultMetrics = promClient.collectDefaultMetrics;
const Registry = promClient.Registry;
const register = new Registry();
collectDefaultMetrics({ register });
// Crie um contador personalizado
const histogram = new promClient.Histogram({
name: 'in_pix_decoded_pix_duration_milliseconds',
help: 'Total de duração pix decoded',
labelNames: ['appName'],
registers: [register],
});
// Rota principal
app.get('/', async (req, res) => {
histogram.observe(1);
histogram.observe(2);
histogram.observe(3);
histogram.observe(4);
histogram.observe(5);
try {
const prometheus = new PrometheusDriver({
endpoint: 'http://localhost:8002/',
});
const data = await prometheus.instantQuery(
'histogram_quantile(0.5, sum(in_pix_decoded_pix_duration_milliseconds_bucket) by (le))',
);
console.log(data.result[0].value);
} catch (err) {
console.error(err);
}
res.send('Olá, Prometheus!');
});
// Endpoint para expor métricas
app.get('/metrics', async (req, res) => {
res.set('Content-Type', register.contentType);
res.end(await register.metrics());
});
app.get('/reset', async (req, res) => {
resetHistogram(buildMetricName());
res.send('Reset!');
});
app.listen(port, () => {
console.log(`Aplicação rodando em http://localhost:${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment