Skip to content

Commit

Permalink
feat: pm2 metrics (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirvolek authored Jun 12, 2023
1 parent d0458df commit 5ed6e16
Show file tree
Hide file tree
Showing 138 changed files with 2,870 additions and 50 deletions.
1,460 changes: 1,435 additions & 25 deletions .pnp.cjs

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
31 changes: 31 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,93 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- pm2 metrics

## 2.5.0 - 2023-05-17

### Added

- CIP68 RFT 444 support

## 2.4.0 - 2023-03-15

### Removed

- `addr_vk` support in `paymentCredToBech32Address`

## 2.3.2 - 2023-03-07

### Added

- Support for ScriptHash payment credential using `script` addresses.

### Changed

- do not leak framework in errors

## 2.3.1 - 2023-02-06

### Removed

- `addr_vk` is no longer valid address type as returned by `detectAndValidateAddressType`

### Fixed

- CIP68 `getMetadataFromOutputDatum` parsing of `files` and custom fields

## 2.3.0 - 2023-02-02

### Added

- support for `addr_vk` addresses in `paymentCredFromBech32Address`
- `getPaymentPartBech32` for generating partial bech32 address consisting of bech32 prefix, addr header and payment part

### Changed

- fastify dependencies

## 2.2.0 - 2022-12-30

### Added

- Missing custom 400s

## 2.1.1 - 2022-12-30

### Changed

- js implementation of utf-8-validate, no longer requiring node-gyp

## 2.1.0 - 2022-12-28

### Added

- CIP68 utils

## 2.0.0 - 2022-11-27

### Removed

- BREAKING CHANGE: `getSchemaForEndpoint` is now part of the `@blockfrost/openapi` package
- `@blockfrost/openapi` dependency

## 1.1.0 - 2022-11-02

### Added

- `validatePolicy` and `validateAsset` to validation functions
- invalid asset and invalid policy 400 handlers

## 1.0.1 - 2022-10-24

### Changed

- `getAddressTypeAndPaymentCred` to return undefined instead of `''` when there is not valid `paymentCred`

## 1.0.0 - 2022-09-23

### Added

- Initial release
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"bech32": "^2.0.0",
"cbor": "^8.1.0",
"crc": "^4.2.0",
"pm2": "^5.3.0",
"prom-client": "^14.2.0",
"yaml": "^2.1.1"
},
"devDependencies": {
Expand Down
104 changes: 104 additions & 0 deletions src/metrics-collector/pm2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { register, Gauge } from 'prom-client';
import pm2, { ProcessDescription } from 'pm2';

export const pm2Metrics = async () => {
const memoryGauge = new Gauge({
name: 'worker_memory_bytes',
help: 'Worker memory usage in bytes',
labelNames: ['name', 'instance'],
});

const cpuGauge = new Gauge({
name: 'worker_cpu_percentage',
help: 'Worker CPU usage in percentage',
labelNames: ['name', 'instance'],
});

const uptimeGauge = new Gauge({
name: 'worker_uptime_seconds',
help: 'Worker uptime in seconds',
labelNames: ['name', 'instance'],
});

const restartsGauge = new Gauge({
name: 'worker_restarts_total',
help: 'Total number of process restarts',
labelNames: ['name', 'instance'],
});

const statusGauge = new Gauge({
name: 'worker_status',
help: 'Is the process running',
labelNames: ['name', 'instance'],
});

const instancesGauge = new Gauge({
name: 'worker_instances',
help: 'Process instances',
labelNames: ['name', 'instance'],
});

try {
await new Promise((resolve, reject) => {
pm2.connect(async error => {
if (error) {
console.error(error);
reject(new Error('Error connecting to PM2'));
}

try {
const processDescriptionList: ProcessDescription[] =
await new Promise((resolve, reject) => {
pm2.list((error, list) => {
if (error) {
reject(error);
} else {
resolve(list);
}
});
});

for (const proc of processDescriptionList) {
const { name, pm_id: instance, monit, pm2_env } = proc;
const labels = { name, instance };

uptimeGauge.set(
labels,
pm2_env
? Math.round((Date.now() - (pm2_env.pm_uptime || 0)) / 1000)
: 0,
);
statusGauge.set(
labels,
pm2_env ? (pm2_env.status === 'online' ? 1 : 0) : 0,
);
instancesGauge.set(
labels,
pm2_env ? Number(pm2_env.instances) || 1 : 0,
);
memoryGauge.set(labels, monit ? monit.memory || 0 : 0);
cpuGauge.set(labels, monit ? monit.cpu || 0 : 0);
restartsGauge.set(
labels,
pm2_env
? pm2_env.unstable_restarts || 0 + (pm2_env.restart_time || 0)
: 0,
);
}
} catch (error) {
console.error(error);
reject(new Error('Error fetching PM2 metrics'));
} finally {
pm2.disconnect();
resolve(true);
}
});
});

const metrics = await register.metrics();

return metrics;
} catch {
throw new Error('Error fetching PM2 metrics');
}
};
Loading

0 comments on commit 5ed6e16

Please sign in to comment.