Skip to content

Commit

Permalink
route specific metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
filvecchiato committed Aug 6, 2024
1 parent 4129c1e commit aba2e54
Show file tree
Hide file tree
Showing 5 changed files with 1,183 additions and 1,238 deletions.
3 changes: 2 additions & 1 deletion .env.local
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
# For more information on how to use .env files and environment variables
# consult the Configuration section in the README.

SAS_SUBSTRATE_URL=ws://127.0.0.1:9944
SAS_SUBSTRATE_URL=wss://polkadot-rpc.dwellir.com
SAS_LOG_LEVEL=http
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/target
yarn-error.log
**/*.rs.bk
.env
.env.*
!.env.local
!.env.docker
Expand All @@ -18,6 +19,7 @@ yarn-error.log
!.yarn/releases
!.yarn/plugins
/docs/.yarn
.yarn.lock

# Tests
/coverage
Expand Down
2 changes: 2 additions & 0 deletions docs/src/openapi-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ servers:
description: Polkadot Asset Hub Parity public sidecar
- url: https://kusama-asset-hub-public-sidecar.parity-chains.parity.io/
description: Kusama Asset Hub Parity public sidecar
- url: http://localhost:8080
description: Localhost
tags:
- name: accounts
- name: blocks
Expand Down
55 changes: 27 additions & 28 deletions src/util/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import client from 'prom-client';
import { Log } from '../logging/Log';
import { parseArgs } from '../parseArgs';

// TODO: cleanup code regarding route specific metrics (possibly implement a more generic solution)

interface IAppConfiguration {
port: number;
host: string;
Expand Down Expand Up @@ -94,6 +92,18 @@ const metrics: IMetric[] = [
},
];

type Body = {
extrinsics?: Record<string, unknown>[];
[key: string]: unknown;
};

interface Query extends Request {
route: {
path: string;
[key: string]: unknown;
};
}

export default class Metrics_App {
private app: Application;
private registry: client.Registry;
Expand Down Expand Up @@ -164,12 +174,10 @@ export default class Metrics_App {
}
}

private getRoute(req: Request) {
private getRoute(req: Query) {
let route = req.baseUrl;
if (req.route) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (req.route?.path !== '/') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
if (req.route.path !== '/') {
route = route ? route + req.route?.path : req.route?.path;
}

Expand Down Expand Up @@ -206,24 +214,21 @@ export default class Metrics_App {
return route;
}

private blocksControllerMetrics(req: Request, res: Response, end: () => number) {
if (req.params?.number) {
const response_size_bytes = this.metrics['sas_extrinsics_per_request'] as client.Histogram;
response_size_bytes
.labels({ method: req.method, route: this.getRoute(req), status_code: res.statusCode })
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
.observe(res.locals.body['extrinsics'] ? res.locals.body['extrinsics'].length || 0 : 0);
private blocksControllerMetrics(req: Query, res: Response, end: () => number) {
const body = res.locals.body as Body | Body[];

if (req.params?.number && !Array.isArray(body)) {
const extrinscs = body.extrinsics ? body.extrinsics.length : 0;

const extrinsics_per_second = this.metrics['sas_extrinsics_per_second'] as client.Histogram;
const seconds = end();
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
const extrinscs = res.locals.body['extrinsics'] ? res.locals.body['extrinsics'].length || 0 : 0;

extrinsics_per_second
.labels({ method: req.method, route: this.getRoute(req), status_code: res.statusCode })
.observe(extrinscs / seconds);

const extrinsics_per_block = this.metrics['sas_extrinsics_per_block'] as client.Histogram;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment

const blocks = 1;
extrinsics_per_block
.labels({ method: req.method, route: this.getRoute(req), status_code: res.statusCode })
Expand All @@ -237,9 +242,8 @@ export default class Metrics_App {

if (req.query?.range) {
let totExtrinsics = 0;
if (Array.isArray(res.locals.body)) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
totExtrinsics = res.locals.body.reduce((current: number, block: { [x: string]: unknown }) => {
if (Array.isArray(body)) {
totExtrinsics = body.reduce((current: number, block: { [x: string]: unknown }) => {
const extrinsics = block['extrinsics'];

if (Array.isArray(extrinsics)) {
Expand All @@ -249,8 +253,7 @@ export default class Metrics_App {
return current;
}, 0);
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
const extrinsics = res.locals.body['extrinsics'];
const extrinsics = body['extrinsics'];
if (Array.isArray(extrinsics)) {
totExtrinsics = extrinsics.length;
}
Expand All @@ -263,15 +266,12 @@ export default class Metrics_App {

const extrinsics_per_second = this.metrics['sas_extrinsics_per_second'] as client.Histogram;
const seconds = end();
// // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
// console.log({ totExtrinsics });
extrinsics_per_second
.labels({ method: req.method, route: this.getRoute(req), status_code: res.statusCode })
.observe(totExtrinsics / seconds);

const extrinsics_per_block = this.metrics['sas_extrinsics_per_block'] as client.Histogram;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const blocks = Array.isArray(res.locals.body) ? res.locals.body.length : 1;
const blocks = Array.isArray(body) ? body.length : 1;
extrinsics_per_block
.labels({ method: req.method, route: this.getRoute(req), status_code: res.statusCode })
.observe(totExtrinsics / blocks);
Expand All @@ -284,7 +284,7 @@ export default class Metrics_App {
}

preMiddleware() {
return (req: Request, res: Response, next: () => void) => {
return (req: Query, res: Response, next: () => void) => {
const tot_requests = this.metrics['sas_total_requests'] as client.Counter;

// request count metrics
Expand All @@ -293,8 +293,7 @@ export default class Metrics_App {
const end = request_duration_seconds.startTimer();

const oldJson = res.json;
res.json = (body) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
res.json = (body: Body) => {
res.locals.body = body;
return oldJson.call(res, body);
};
Expand Down
Loading

0 comments on commit aba2e54

Please sign in to comment.