Skip to content

Commit 6040134

Browse files
committed
add metrics for tracking pg_pool
1 parent c02cae1 commit 6040134

File tree

4 files changed

+112
-10
lines changed

4 files changed

+112
-10
lines changed

default.nix

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,73 @@
1515
}),
1616
system ? builtins.currentSystem,
1717
}: let
18+
inherit (pkgs) lib;
1819
nodejs = pkgs.nodejs_24;
1920
nodePackages = nodejs.pkgs;
2021
testing = import (pkgs.path + "/nixos/lib/testing-python.nix") {inherit system;};
2122
packageJSON = builtins.fromJSON (builtins.readFile ./package.json);
2223

24+
pm2-prom-module' = pkgs.fetchurl {
25+
url = "https://registry.npmjs.org/pm2-prom-module/-/pm2-prom-module-2.7.2.tgz";
26+
sha256 = "sha256-cFoYLuavuMoeZdMGWNHZggrUAHL6sUY6ANKblHrmSRE=";
27+
};
28+
29+
pmx = pkgs.fetchurl {
30+
url = "https://registry.npmjs.org/pmx/-/pmx-0.5.0.tgz";
31+
sha256 = "sha256-nX/UCajkOsresfft+0pMda54cJnz54OsQ/q+6S7IbaA=";
32+
};
33+
34+
pm2-deps = {
35+
"@opentelemetry/api"= { version = "1.7.0"; hash = "sha256-lZqeEPocOr0FlyV9Hf29YY/GiXT6PGhigZZiVkFI9j8="; tarName = "api"; };
36+
"@pm2/agent" = { version = "2.0.4"; hash = "sha256-hgyUnG8zu4Yf/rU/AXJEO4f55qmB5s+3RbaBeikHnsI="; tarName = "agent"; };
37+
amp = { version = "0.3.1"; hash = "sha256-Rc1vsyFTj8yCDdvRqmPRJXytr2DVxeNCOSc+/sc+qoA="; };
38+
amp-message = { version = "0.1.2"; hash = "sha256-hQIlUxEvta2Lxwwjx/c3RJyl+WIMUUJ631YPEWPO1l4="; };
39+
ansi-styles = { version = "4.3.0"; hash = "sha256-LFOaRthatgMxg5l0NNLZpcos7vwStNuQIvVkeEzXmH8="; };
40+
async = { version = "3.2.6"; hash = "sha256-a7Rc+6CM0u6jnrPObqqUETBw/hN+wL1KnxIr6aAlDm8="; };
41+
bintrees = { version = "1.0.1"; hash = "sha256-2AbqS+fyyf3qrhLl2wHwP95BiCKoLhIt70DVsykztpY="; };
42+
chalk = { version = "3.0.0"; hash = "sha256-HXXWjRKKVurr4la8y36qe9Lhumy+LIZp7dJjK16ocVs="; };
43+
cli-tableau = { version = "2.0.1"; hash = "sha256-eM6GuVO4buQ+LwbTcrXGtLlJ6XI4pD8yLVBJR0q2zYc="; };
44+
commander = { version = "2.15.1"; hash = "sha256-fdf5IQYmc11/MZXZAEcNaH4lXiFZRkFew7UY8T0qNFM="; };
45+
dayjs = { version = "1.8.36"; hash = "sha256-3LnPubqlOkBwHLqCs9+oL5rlIw/PEZSUPakya87MtN4="; };
46+
debug = { version = "4.4.3"; hash = "sha256-icGsnJRu6JBah1g3EUUo6X7q414DvjGQWEsiFq9D5Kc="; };
47+
escape-string-regexp= { version = "4.0.0"; hash = "sha256-S0ShTaaYejwFhcL/FrOABCVzL/NcrXEMZectf44zsRs="; };
48+
fclone = { version = "1.0.11"; hash = "sha256-lmsI93H/SMBfyygJGxDeJLtxc21bJ/Mc+VkLfutikYc="; };
49+
has-flag = { version = "4.0.0"; hash = "sha256-d6frFBHZJ7uKXKcGnb4WiIbWPIj0RvC4FQCizyPdtbE="; };
50+
json-stringify-safe = { version = "5.0.1"; hash = "sha256-t/u/ZcC/9tR/UWyYY4Ip3/DpgdDt//7Lz5cdf+Nhkoo="; };
51+
ms = { version = "2.1.3"; hash = "sha256-9mFuFeUw7VUvnaotPOcZY5R8a8fJjJtk/T5nP9AmIsY="; };
52+
pidusage = { version = "4.0.1"; hash = "sha256-u2jNQ3FQov7XPyVL0QNVYzhqV7vg0f1dkXMeey5J4gg="; };
53+
pm2 = { version = "5.4.2"; hash = "sha256-tGmAfEdVK7HVIeB6sE8dTyyaAJ04L3fmVN1luGuygBQ="; };
54+
pm2-axon = { version = "4.0.1"; hash = "sha256-7vTPYtGXn/PlYXOlyYh7WkyCImio1I4zSqP2v78s/oU="; };
55+
pm2-axon-rpc = { version = "0.7.1"; hash = "sha256-1LhIPIGpRnyRlX7UHr0xGOY73zay5SklCgbfHpiOVV0="; };
56+
pmx = { version = "0.5.0"; hash = "sha256-nX/UCajkOsresfft+0pMda54cJnz54OsQ/q+6S7IbaA="; };
57+
prom-client = { version = "15.1.3"; hash = "sha256-xFxsoQsDPILfHZSRUgxs/rvTBHpLSu+xoe0LNDHCeLQ="; };
58+
safe-buffer = { version = "5.2.1"; hash = "sha256-XRgYBFFsSmk6OEJyp70OQtF+DUswHM++QIZpzK/cs+g="; };
59+
semver = { version = "7.6.2"; hash = "sha256-3vHmSQrAx4ELC4Ff5lQvMhPUTJWwaAWdysL4HiwiN6Q="; };
60+
supports-color = { version = "8.1.1"; hash = "sha256-6mnZtYAeEJky+fErVJnAbeP96TYRmiHFlBsnWdQ2T7A="; };
61+
tdigest = { version = "0.1.1"; hash = "sha256-LVfWh8Wid9PTuJJ/5OxjAixzCbeUMAlsceEHeXhimtA="; };
62+
};
63+
64+
pm2-deps-tars = lib.mapAttrs (name: { version, hash ? lib.fakeHash, tarName ? name }: pkgs.fetchurl { url = "https://registry.npmjs.org/${name}/-/${tarName}-${version}.tgz"; sha256 = hash; }) pm2-deps;
65+
66+
pm2-prom-module = pkgs.runCommand "pm2-prom-module-2.7.2.tar.gz" {} ''
67+
tar -xf ${pm2-prom-module'}
68+
mv package module
69+
mkdir module/node_modules
70+
${lib.concatStringsSep "\n" (lib.mapAttrsToList (k: v: ''
71+
tar -xf ${v}
72+
mkdir -p $(dirname module/node_modules/${k})
73+
mv package module/node_modules/${k}
74+
'') pm2-deps-tars)}
75+
tar -czf $out module/
76+
'';
77+
78+
excludeNixFiles = path: type: let
79+
baseName = baseNameOf (toString path);
80+
in
81+
!(lib.hasSuffix ".nix" baseName);
82+
2383
blockfrost-backend-ryo = let
24-
src = pkgs.lib.cleanSource ./.;
84+
src = lib.cleanSourceWith { filter = excludeNixFiles; src = (lib.cleanSource ./.); };
2585
project =
2686
pkgs.callPackage ./yarn-project.nix
2787
{inherit nodejs;}
@@ -43,11 +103,12 @@
43103
mkdir -p $out/bin
44104
cat <<EOF > $out/bin/${name}
45105
#!${pkgs.runtimeShell}
106+
export PATH=${nodePackages.pm2}/bin:${pkgs.nodejs}/bin:\$PATH
107+
46108
echo "Starting ${name}...";
47-
${nodePackages.pm2}/bin/pm2 delete all; \
48-
${nodePackages.pm2}/bin/pm2 start \
49-
$out/libexec/source/dist/server.js \
50-
--interpreter=${pkgs.nodejs}/bin/node --node-args="\''${BLOCKFROST_NODE_ARGS:-"--max-http-header-size=32768"}" \
109+
pm2 delete all
110+
pm2 start $out/libexec/source/dist/server.js \
111+
--interpreter=${nodejs}/bin/node --node-args="\''${BLOCKFROST_NODE_ARGS:-"--max-http-header-size=32768"}" \
51112
--max-memory-restart \''${BLOCKFROST_MAX_MEMORY_RESTART:-"1500M"} \
52113
-i \''${BLOCKFROST_PM2_INSTANCE_COUNT:-"max"} --time --no-daemon
53114
EOF
@@ -57,6 +118,21 @@
57118
dontStrip = true;
58119
});
59120

121+
blockfrost-backend-ryo-wrapper = pkgs.writeShellApplication {
122+
name = "blockfrost-backend-ryo";
123+
runtimeInputs = [ nodePackages.pm2 nodejs ];
124+
text = ''
125+
set -x
126+
echo "Starting blockfrost-backend-ryo...";
127+
pm2 delete all || true
128+
pm2 install ${pm2-prom-module}
129+
pm2 start ${blockfrost-backend-ryo}/libexec/source/dist/server.js \
130+
--interpreter=${nodejs}/bin/node --node-args="''${BLOCKFROST_NODE_ARGS:-"--max-http-header-size=32768"}" \
131+
--max-memory-restart "''${BLOCKFROST_MAX_MEMORY_RESTART:-"1500M"}" \
132+
-i "''${BLOCKFROST_PM2_INSTANCE_COUNT:-"max"}" --time --no-daemon
133+
'';
134+
};
135+
60136
commonTestConfig = {
61137
# We have to increase memsize, otherwise we will get error:
62138
# "Kernel panic - not syncing: Out of memory: compulsory panic_on_oom"
@@ -85,7 +161,7 @@
85161
)
86162
'';
87163
in {
88-
inherit blockfrost-backend-ryo;
164+
inherit blockfrost-backend-ryo blockfrost-backend-ryo-wrapper;
89165
blockfrost-backend-ryo-test-mainnet = testing.makeTest rec {
90166
name = "blockfrost-backend-ryo-test-mainnet";
91167

flake.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
in
2424
{
2525
packages = forAllSystems (system: {
26-
inherit (default.${system}) blockfrost-backend-ryo;
26+
inherit (default.${system}) blockfrost-backend-ryo blockfrost-backend-ryo-wrapper;
2727
dockerImage =
2828
let
2929
configs = legacyPkgs.${system}.runCommand "app-configs" { }
@@ -40,7 +40,7 @@
4040
WorkingDir = "/app";
4141
};
4242
};
43-
default = self.packages.${system}.blockfrost-backend-ryo;
43+
default = self.packages.${system}.blockfrost-backend-ryo-wrapper;
4444
});
4545
checks = forAllSystems (system: {
4646
inherit (self.packages.${system}) blockfrost-backend-ryo dockerImage;

src/app.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const start = (options = {}): FastifyInstance => {
4545

4646
if (config.server.prometheusMetrics) {
4747
app.register(fastifyMetrics, {
48-
endpoint: '/prometheus',
48+
endpoint: '/metrics',
4949
// routeMetrics: {
5050
// enabled: true,
5151
// registeredRoutesOnly: true,
@@ -67,6 +67,32 @@ const start = (options = {}): FastifyInstance => {
6767
// See: https://github.com/VeXell/pm2-prom-module
6868
if (config.server.prometheusMetrics) {
6969
initMetrics(app.metrics.client.register);
70+
// hack to deal with pm2 not doing metrics right
71+
app.metrics.client.register.setDefaultLabels({
72+
node_instance: process.env.NODE_APP_INSTANCE,
73+
});
74+
75+
new app.metrics.client.Gauge({
76+
name: 'pg_pool_total_count',
77+
help: 'total pg_pool connections',
78+
collect() {
79+
this.set(app.pg.dbSync.pool.totalCount);
80+
},
81+
});
82+
new app.metrics.client.Gauge({
83+
name: 'pg_pool_idle_count',
84+
help: 'idle pg_pool connections',
85+
collect() {
86+
this.set(app.pg.dbSync.pool.idleCount);
87+
},
88+
});
89+
new app.metrics.client.Gauge({
90+
name: 'pg_pool_waiting_count',
91+
help: 'waiting pg_pool connections',
92+
collect() {
93+
this.set(app.pg.dbSync.pool.waitingCount);
94+
},
95+
});
7096
}
7197
});
7298

src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ const logger =
1818

1919
const server = app({
2020
logger,
21-
ignoreTrailingSlash: true,
2221

2322
// https://www.fastify.io/docs/latest/Server/#maxparamlength
2423
// if the param overflows this number, 404 will be returned
2524
// currently the biggest param seems to be the address which can be 30864 (haxxxors having fun)
2625
// select tx_id,LENGTH(address) from tx_out where LENGTH(address) > 16000 ORDER BY LENGTH(address) desc LIMIT 10;
2726
routerOptions: {
27+
ignoreTrailingSlash: true,
2828
maxParamLength: 32_768,
2929
},
3030
});

0 commit comments

Comments
 (0)