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

refactor(metrics): consolidate metric definitions into metrics.ts #39

Merged
merged 6 commits into from
Aug 30, 2023
Merged
25 changes: 3 additions & 22 deletions src/arweave/composite-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import type { queueAsPromised } from 'fastq';
import { default as fastq } from 'fastq';
import { default as NodeCache } from 'node-cache';
import { Readable } from 'node:stream';
import * as promClient from 'prom-client';
import * as rax from 'retry-axios';
import { default as wait } from 'wait';
import * as winston from 'winston';
Expand All @@ -34,6 +33,7 @@ import {
sanityCheckTx,
validateChunk,
} from '../lib/validation.js';
import * as metrics from '../metrics.js';
import {
ChainSource,
Chunk,
Expand Down Expand Up @@ -117,13 +117,8 @@ export class ArweaveCompositeClient
private blockTxPrefetchCount;
private maxPrefetchHeight = -1;

// Metrics
private arweavePeerInfoErrorCounter: promClient.Counter<string>;
private arweavePeerRefreshErrorCounter: promClient.Counter<string>;

constructor({
log,
metricsRegistry,
arweave,
trustedNodeUrl,
blockStore,
Expand All @@ -139,7 +134,6 @@ export class ArweaveCompositeClient
skipCache = false,
}: {
log: winston.Logger;
metricsRegistry: promClient.Registry;
arweave: Arweave;
trustedNodeUrl: string;
blockStore: PartialJsonBlockStore;
Expand Down Expand Up @@ -201,19 +195,6 @@ export class ArweaveCompositeClient
// Initialize prefetch settings
this.blockPrefetchCount = blockPrefetchCount;
this.blockTxPrefetchCount = blockTxPrefetchCount;

// Metrics
this.arweavePeerInfoErrorCounter = new promClient.Counter({
name: 'arweave_peer_info_errors_total',
help: 'Count of failed Arweave peer info requests',
});
metricsRegistry.registerMetric(this.arweavePeerInfoErrorCounter);

this.arweavePeerRefreshErrorCounter = new promClient.Counter({
name: 'arweave_peer_referesh_errors_total',
help: 'Count of errors refreshing the Arweave peers list',
});
metricsRegistry.registerMetric(this.arweavePeerRefreshErrorCounter);
}

async refreshPeers(): Promise<void> {
Expand Down Expand Up @@ -242,13 +223,13 @@ export class ArweaveCompositeClient
this.preferredPeers.add(this.peers[peerHost]);
}
} catch (error) {
this.arweavePeerInfoErrorCounter.inc();
metrics.arweavePeerInfoErrorCounter.inc();
}
return;
}),
);
} catch (error) {
this.arweavePeerRefreshErrorCounter.inc();
metrics.arweavePeerRefreshErrorCounter.inc();
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/database/standalone-sqlite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import arbundles from 'arbundles/stream/index.js';
import { expect } from 'chai';
import crypto from 'node:crypto';
import fs from 'node:fs';
import * as promClient from 'prom-client';

import {
StandaloneSqliteDatabase,
Expand Down Expand Up @@ -160,17 +159,13 @@ describe('SQLite data conversion functions', () => {
});

describe('StandaloneSqliteDatabase', () => {
let metricsRegistry: promClient.Registry;
let chainSource: ArweaveChainSourceStub;
let db: StandaloneSqliteDatabase;
let dbWorker: StandaloneSqliteDatabaseWorker;

before(() => {
metricsRegistry = promClient.register;
metricsRegistry.clear();
db = new StandaloneSqliteDatabase({
log,
metricsRegistry,
coreDbPath,
dataDbPath,
moderationDbPath,
Expand Down
15 changes: 2 additions & 13 deletions src/database/standalone-sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
parentPort,
workerData,
} from 'node:worker_threads';
import * as promClient from 'prom-client';
import * as R from 'ramda';
import sql from 'sql-bricks';
import * as winston from 'winston';
Expand All @@ -45,6 +44,7 @@ import {
import { MANIFEST_CONTENT_TYPE } from '../lib/encoding.js';
import { currentUnixTimestamp } from '../lib/time.js';
import log from '../log.js';
import * as metrics from '../metrics.js';
import {
BlockListValidator,
BundleIndex,
Expand Down Expand Up @@ -2152,7 +2152,6 @@ export class StandaloneSqliteDatabase
NestedDataIndexWriter
{
log: winston.Logger;
methodDurationSummary: promClient.Summary<string>;
private workers: {
core: { read: any[]; write: any[] };
data: { read: any[]; write: any[] };
Expand Down Expand Up @@ -2185,29 +2184,19 @@ export class StandaloneSqliteDatabase
};
constructor({
log,
metricsRegistry,
coreDbPath,
dataDbPath,
moderationDbPath,
bundlesDbPath,
}: {
log: winston.Logger;
metricsRegistry: promClient.Registry;
coreDbPath: string;
dataDbPath: string;
moderationDbPath: string;
bundlesDbPath: string;
}) {
this.log = log.child({ class: 'StandaloneSqliteDatabase' });

// Metrics
this.methodDurationSummary = new promClient.Summary({
name: 'standalone_sqlite_method_duration_seconds',
help: 'Count of failed Arweave peer info requests',
labelNames: ['worker', 'role', 'method'],
});
metricsRegistry.registerMetric(this.methodDurationSummary);

const self = this;

function spawn(pool: WorkerPoolName, role: WorkerRoleName) {
Expand Down Expand Up @@ -2314,7 +2303,7 @@ export class StandaloneSqliteDatabase
method: string,
args: any,
): Promise<any> {
const end = this.methodDurationSummary.startTimer({
const end = metrics.methodDurationSummary.startTimer({
worker: workerName,
role,
method,
Expand Down
134 changes: 134 additions & 0 deletions src/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* AR.IO Gateway
* Copyright (C) 2022-2023 Permanent Data Solutions, Inc. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import * as promClient from 'prom-client';

//
// Global error metrics
//

export const errorsCounter = new promClient.Counter({
name: 'errors_total',
help: 'Total error count',
});

export const uncaughtExceptionCounter = new promClient.Counter({
name: 'uncaught_exceptions_total',
help: 'Count of uncaught exceptions',
});

//
// Global bundle metrics
//

export const bundlesCounter = new promClient.Counter({
name: 'bundles_total',
help: 'Count of all bundles seen',
labelNames: ['bundle_format', 'parent_type'],
});

export const bundlesMatchedCounter = new promClient.Counter({
name: 'bundles_matched_total',
help: 'Count of bundles matched for unbundling',
labelNames: ['bundle_format'],
});

export const bundlesQueuedCounter = new promClient.Counter({
name: 'bundles_queued_total',
help: 'Count of bundles queued for unbundling',
labelNames: ['bundle_format'],
});

export const bundlesUnbundledCounter = new promClient.Counter({
name: 'bundles_unbundled_total',
help: 'Count of bundles unbundled',
labelNames: ['bundle_format'],
});

export const dataItemsQueuedCounter = new promClient.Counter({
name: 'data_items_queued_total',
help: 'Count of data items queued for indexing',
labelNames: ['bundle_format'],
});

//
// Arweave client metrics
//

export const arweavePeerInfoErrorCounter = new promClient.Counter({
name: 'arweave_peer_info_errors_total',
help: 'Count of failed Arweave peer info requests',
});

export const arweavePeerRefreshErrorCounter = new promClient.Counter({
name: 'arweave_peer_referesh_errors_total',
help: 'Count of errors refreshing the Arweave peers list',
});

//
// SQLite metrics
//

export const methodDurationSummary = new promClient.Summary({
name: 'standalone_sqlite_method_duration_seconds',
help: 'Count of failed Arweave peer info requests',
labelNames: ['worker', 'role', 'method'],
});

//
// Block importer metrics
//

export const blockImporterRunningGauge = new promClient.Gauge({
name: 'block_importer_running',
help: 'Depth of the last observed chain fork',
});

export const forksCounter = new promClient.Counter({
name: 'forks_total',
help: 'Count of chain forks observed',
});

export const lastForkDepthGauge = new promClient.Gauge({
name: 'last_fork_depth',
help: 'Depth of the last observed chain fork',
});

export const blocksImportedCounter = new promClient.Counter({
name: 'blocks_imported_total',
help: 'Count of blocks imported',
});

export const transactionsImportedCounter = new promClient.Counter({
name: 'block_transactions_imported_total',
help: 'Count of transactions imported',
});

export const missingTransactionsCounter = new promClient.Counter({
name: 'missing_block_transactions_total',
help: 'Count of block transactions that could not be immediately fetched',
});

export const blockImportErrorsCounter = new promClient.Counter({
name: 'block_import_errors_total',
help: 'Count of block import errors',
});

export const lastHeightImported = new promClient.Gauge({
name: 'last_height_imported',
help: 'Height of the last block imported',
});
Loading