prometheus not saving data server by node.js prom-client

377 Views Asked by At

i have a node.js project running that will restart every so often. when it restarts the metrics served with express get reset. when checking in prometheus none of the data is retained. full code from the node.js app can be found here but here are a few snipets.

const prom = require('prom-client');
const express = require('express');
const fetch = require('node-fetch');
const app = express();


const register = new prom.Registry();
const collectDefaultMetrics = prom.collectDefaultMetrics;
const tags_default = [ "guild", "channel", "user" ];
const tags_names = [ "guild", "channel", "channelName", "user", "name" ];
const track_names = true;
const prefix = "author_"
collectDefaultMetrics({ register });

$

    // Create a registry and pull default metrics
app.get('/metrics', function (req, res) {
    res.set('Content-Type', prom.register.contentType);
    res.end(prom.register.metrics());
    });

// Start the server to expose the metrics.
// 0.0.0.0:3001/metrics
app.listen(3001, () => {
    console.log('Server is running on port 3001');
});

my prometheus startup command looks like

./prometheus --web.listen-address=0.0.0.0:{{SERVER_PORT}} --config.file=/home/container/prometheus.yml --storage.tsdb.path=/home/container/data --web.console.templates=/home/container/consoles --web.console.libraries=/home/container/console_libraries --web.config.file=/home/container/prometheus.web.yml

my prometheus config is:

global:
  scrape_interval: 10s 
  evaluation_interval: 60s 

alerting:
  alertmanagers:
    - static_configs:
        - targets:
         
rule_files:
scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["0.0.0.0:9090"]

  - job_name: "discord"
    static_configs:
      - targets: ["192.168.1.107:3001"]

i need to have this data retained as its used for long term research.

1

There are 1 best solutions below

0
On

mine already working well but using fastify instead of express , i prefer try this:

app.get("/metrics", async (req, res) => {
   res.headers("Content-Type", register.contentType);
   res.send(await register.metrics());
});