Expose combination of provider + core stats (#5696)

* Expose combination of provider + core stats

* Non-existing core stats
This commit is contained in:
Jaco
2023-08-30 08:25:51 +03:00
committed by GitHub
parent ab3b162d7e
commit 7cae843e9b
4 changed files with 51 additions and 5 deletions
+3 -4
View File
@@ -1,8 +1,7 @@
// Copyright 2017-2023 @polkadot/api authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { RpcInterface } from '@polkadot/rpc-core/types';
import type { ProviderInterface } from '@polkadot/rpc-provider/types';
import type { RpcCoreStats, RpcInterface } from '@polkadot/rpc-core/types';
import type { Text } from '@polkadot/types';
import type { Hash, RuntimeVersion } from '@polkadot/types/interfaces';
import type { Metadata } from '@polkadot/types/metadata';
@@ -201,8 +200,8 @@ export abstract class Getters<ApiType extends ApiTypes> extends Init<ApiType> im
/**
* @description Returns the underlying provider stats
*/
public get stats (): ProviderInterface['stats'] | undefined {
return this._rpcCore.provider.stats;
public get stats (): RpcCoreStats | undefined {
return this._rpcCore.stats;
}
/**
+23 -1
View File
@@ -7,7 +7,7 @@ import type { StorageKey, Vec } from '@polkadot/types';
import type { Hash } from '@polkadot/types/interfaces';
import type { AnyJson, AnyNumber, Codec, DefinitionRpc, DefinitionRpcExt, DefinitionRpcSub, Registry } from '@polkadot/types/types';
import type { Memoized } from '@polkadot/util/types';
import type { RpcInterfaceMethod } from './types/index.js';
import type { RpcCoreStats, RpcInterfaceMethod } from './types/index.js';
import { Observable, publishReplay, refCount } from 'rxjs';
@@ -94,6 +94,8 @@ export class RpcCore {
readonly #isPedantic: boolean;
readonly #registryDefault: Registry;
readonly #storageCache = new Map<string, Codec>();
#storageCacheHits = 0;
#storageCacheSize = 0;
#getBlockRegistry?: (blockHash: Uint8Array) => Promise<{ registry: Registry }>;
#getBlockHash?: (blockNumber: AnyNumber) => Promise<Uint8Array>;
@@ -147,6 +149,23 @@ export class RpcCore {
return this.provider.disconnect();
}
/**
* @description Returns the underlying core stats, including those from teh provider
*/
public get stats (): RpcCoreStats | undefined {
const stats = this.provider.stats;
return stats
? {
...stats,
core: {
cacheHits: this.#storageCacheHits,
cacheSize: this.#storageCacheSize
}
}
: undefined;
}
/**
* @description Sets a registry swap (typically from Api)
*/
@@ -453,6 +472,8 @@ export class RpcCore {
const cached = this.#storageCache.get(hexKey);
if (cached) {
this.#storageCacheHits++;
return cached;
}
}
@@ -470,6 +491,7 @@ export class RpcCore {
// clearing of it, so very long running processes (not just a couple of hours, longer)
// will increase memory beyond what is allowed.
this.#storageCache.set(hexKey, codec);
this.#storageCacheSize++;
return codec;
}
+12
View File
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { Observable } from 'rxjs';
import type { ProviderInterface } from '@polkadot/rpc-provider/types';
import type { AnyFunction, Codec, DefinitionRpc } from '@polkadot/types/types';
export interface RpcInterfaceMethod {
@@ -14,3 +15,14 @@ export type AugmentedRpc<F extends AnyFunction> = F & {
raw: <T> (...params: Parameters<F>) => Observable<T>;
meta: DefinitionRpc;
};
/** Stats from the rpc-core layer, including the provider stats */
export interface RpcCoreStats extends NonNullable<ProviderInterface['stats']> {
/** Internal stats for the rpc-core layer */
core: {
/** The number of values retrieved from the core cache */
cacheHits: number;
/** The number of entries in the core cache */
cacheSize: number;
}
}
+13
View File
@@ -67,20 +67,33 @@ export interface ProviderInterface {
unsubscribe (type: string, method: string, id: number | string): Promise<boolean>;
}
/** Stats for a specific endpoint */
export interface EndpointStats {
/** The total number of bytes sent */
bytesRecv: number;
/** The total number of bytes received */
bytesSent: number;
/** The number of cached/in-progress requests made */
cached: number;
/** The number of errors found */
errors: number;
/** The number of requests */
requests: number;
/** The number of subscriptions */
subscriptions: number;
/** The number of request timeouts */
timeout: number;
}
/** Overall stats for the provider */
export interface ProviderStats {
/** Details for the active/open requests */
active: {
/** Number of active requests */
requests: number;
/** Number of active subscriptions */
subscriptions: number;
};
/** The total requests that have been made */
total: EndpointStats;
}