diff --git a/CHANGELOG.md b/CHANGELOG.md index 844e0e0db0..7fca88c063 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # CHANGELOG +## master + +Changes: + +- Expose provider stats on API intrerface via `.stats` + + ## 8.6.2 May 30, 2022 Changes: diff --git a/packages/api/src/base/Getters.ts b/packages/api/src/base/Getters.ts index a12730fdf8..cb309e4aaf 100644 --- a/packages/api/src/base/Getters.ts +++ b/packages/api/src/base/Getters.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { RpcInterface } from '@polkadot/rpc-core/types'; +import type { ProviderInterface } from '@polkadot/rpc-provider/types'; import type { Text } from '@polkadot/types'; import type { Hash, RuntimeVersion } from '@polkadot/types/interfaces'; import type { Metadata } from '@polkadot/types/metadata'; @@ -188,6 +189,13 @@ export abstract class Getters extends Init im return assertResult(this._rx as Pick); } + /** + * @description Returns the underlying provider stats + */ + public get stats (): ProviderInterface['stats'] | undefined { + return this._rpcCore.provider.stats; + } + /** * @description The type of this API instance, either 'rxjs' or 'promise' */ diff --git a/packages/rpc-provider/src/http/index.ts b/packages/rpc-provider/src/http/index.ts index e51bd39931..5edc02f9f1 100644 --- a/packages/rpc-provider/src/http/index.ts +++ b/packages/rpc-provider/src/http/index.ts @@ -56,7 +56,7 @@ export class HttpProvider implements ProviderInterface { this.#headers = headers; this.#stats = { active: { requests: 0, subscriptions: 0 }, - total: { bytesRecv: 0, bytesSent: 0, cached: 0, requests: 0, subscriptions: 0, timeout: 0 } + total: { bytesRecv: 0, bytesSent: 0, cached: 0, errors: 0, requests: 0, subscriptions: 0, timeout: 0 } }; } @@ -169,6 +169,7 @@ export class HttpProvider implements ProviderInterface { return decoded; } catch (e) { this.#stats.active.requests--; + this.#stats.total.errors++; throw e; } diff --git a/packages/rpc-provider/src/types.ts b/packages/rpc-provider/src/types.ts index 09aa91a11c..f6d71bf118 100644 --- a/packages/rpc-provider/src/types.ts +++ b/packages/rpc-provider/src/types.ts @@ -71,6 +71,7 @@ export interface ProviderStats { bytesRecv: number; bytesSent: number; cached: number; + errors: number; requests: number; subscriptions: number; timeout: number; diff --git a/packages/rpc-provider/src/ws/index.ts b/packages/rpc-provider/src/ws/index.ts index abef9bf001..a29befa34b 100644 --- a/packages/rpc-provider/src/ws/index.ts +++ b/packages/rpc-provider/src/ws/index.ts @@ -137,7 +137,7 @@ export class WsProvider implements ProviderInterface { this.#websocket = null; this.#stats = { active: { requests: 0, subscriptions: 0 }, - total: { bytesRecv: 0, bytesSent: 0, cached: 0, requests: 0, subscriptions: 0, timeout: 0 } + total: { bytesRecv: 0, bytesSent: 0, cached: 0, errors: 0, requests: 0, subscriptions: 0, timeout: 0 } }; this.#timeout = timeout || DEFAULT_TIMEOUT_MS; @@ -339,6 +339,8 @@ export class WsProvider implements ProviderInterface { this.#stats.total.bytesSent += body.length; this.#websocket.send(body); } catch (error) { + this.#stats.total.errors++; + reject(error); } }); @@ -492,6 +494,8 @@ export class WsProvider implements ProviderInterface { } } } catch (error) { + this.#stats.total.errors++; + handler.callback(error as Error, undefined); } @@ -520,6 +524,8 @@ export class WsProvider implements ProviderInterface { handler.callback(null, result); } catch (error) { + this.#stats.total.errors++; + handler.callback(error as Error, undefined); } };