Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use #1

Open
ghost opened this issue Jan 9, 2019 · 2 comments
Open

How to use #1

ghost opened this issue Jan 9, 2019 · 2 comments

Comments

@ghost
Copy link

ghost commented Jan 9, 2019

Hi

Its been mentioned that pm2 cannot work with prometheus because we cannot run our own code in the master process. So im curious to know how does this work?

I am trying to test this package but struggling to find out how to set it up.

Does this config go in my pm2 ecosystem file?

 "config": {
    "app_name": "api",             //需要拉取监控数据的 APP 名称
    "app_group": "book",           //consul 服务分组 
    "port": 3000,                  //http服务默认端口,提供 /metrics 和 /online 接口
    "reigster_disabled": false,    //是否禁止服务注册到 consul
    "consul_host": "127.0.0.1",    
    "consul_port": "8500",
    "register_mode": "cluster"            // default:cluster   other: worker
  }

--
where should i put this code in my node app


const promClient = require('prom-client')

process.on('message', function (message) {
    if (message.type === 'prom:getMetricsReq') {
        process.send({
            type: 'prom:getMetricsRes',
            data: {
                requestId: message.data.requestId,
                metrics: promClient.register.getMetricsAsJSON()
            }
        })
    }
})

Do i need to declare a new route to get metrics from port 3000/metrics or will this automatically be available ?

@gatispriede
Copy link

Documentation is missing only 1 important thing, the collector needs to be triggered:


const collectDefaultMetrics = promClient.collectDefaultMetrics;

collectDefaultMetrics({ timeout: 5000 });

process.on('message', function (message) {
    if (message.type === 'prom:getMetricsReq') {
        process.send({
            type: 'prom:getMetricsRes',
            data: {
                requestId: message.data.requestId,
                metrics: promClient.register.getMetricsAsJSON()
            }
        })
    }
})```
copy this in you main entry file of nodejs at the bottom (not in any routing mechanism)

then setup variables for the tool, for me works:
[PM2] Module pm2-cluster-prometheus restarted
== pm2-cluster-prometheus ==
┌─────────────────────┬───────────┐
│ key                 │ value     │
├─────────────────────┼───────────┤
│ app_name            │ server    │
│ port                │ 3000      │
│ reigster_disabled   │ false     │
│ consul_service_name │ server    │
│ consul_host         │ 127.0.0.1 │
│ consul_port         │ 80        │
│ register_mode       │ worker    │
└─────────────────────┴───────────┘
where the consul_service_name is the name of your app when you run it with pm2
check with `pm2 list`

@Dream-MG
Copy link

@gatispriede tried this but getting error
0|pm2-cluster-prometheus | consul register failed Error: not found
0|pm2-cluster-prometheus | at create (/Users/manish.gupta1/.pm2/modules/pm2-cluster-prometheus/node_modules/papi/lib/errors.js:14:5)
0|pm2-cluster-prometheus | at Object.response [as Response] (/.pm2/modules/pm2-cluster-prometheus/node_modules/papi/lib/errors.js:38:15)
0|pm2-cluster-prometheus | at IncomingMessage. (/.pm2/modules/pm2-cluster-prometheus/node_modules/papi/lib/client.js:592:26)
0|pm2-cluster-prometheus | at IncomingMessage.emit (node:events:402:35)
0|pm2-cluster-prometheus | at IncomingMessage.emit (node:domain:475:12)
0|pm2-cluster-prometheus | at endReadableNT (node:internal/streams/readable:1343:12)
0|pm2-cluster-prometheus | at processTicksAndRejections (node:internal/process/task_queues:83:21)

i've changed the consul port and service name to the app name . Do i've to change any other property ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants