From 4e4b94079dbdc950a8d8c9657309c57db170eec2 Mon Sep 17 00:00:00 2001 From: Jagpreet Singh Sasan Date: Wed, 10 Mar 2021 15:17:56 +0530 Subject: [PATCH] feat(besu): add prometheus exporter Primary Change -------------- 1. The besu ledger connector plugin now includes the prometheus metrics exporter integration 2. OpenAPI spec now has api endpoint for the getting the prometheus metrics Refactorings that were also necessary to accomodate 1) and 2) ------------------------------------------------------------ 3. GetPrometheusMetricsV1 class is created to handle the corresponding api endpoint 4. IPluginLedgerConnectorBesuOptions interface in PluginLedgerConnectorBesu class now has a prometheusExporter optional field 5. The PluginLedgerConnectorBesu class has relevant functions and codes to incorporate prometheus exporter 6. deploy-contract-from-json.test.ts is changed to incorporate the prometheus exporter 7. Added Readme.md on the prometheus exporter usage Fixes #533 Signed-off-by: Jagpreet Singh Sasan --- .../README.md | 44 +++++++++- .../package-lock.json | 21 +++++ .../package.json | 6 +- .../src/main/json/openapi.json | 29 +++++++ .../generated/openapi/typescript-axios/api.ts | 69 ++++++++++++++++ .../plugin-ledger-connector-besu.ts | 37 +++++++++ .../prometheus-exporter/data-fetcher.ts | 7 ++ .../typescript/prometheus-exporter/metrics.ts | 7 ++ .../prometheus-exporter.ts | 41 ++++++++++ .../prometheus-exporter/response.type.ts | 3 + ...prometheus-exporter-metrics-endpoint-v1.ts | 80 +++++++++++++++++++ .../deploy-contract-from-json.test.ts | 46 ++++++++++- .../main/typescript/besu/besu-test-ledger.ts | 2 +- .../fabric/fabric-test-ledger-v1.ts | 2 +- .../typescript/quorum/quorum-test-ledger.ts | 2 +- 15 files changed, 389 insertions(+), 7 deletions(-) create mode 100644 packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/data-fetcher.ts create mode 100644 packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/metrics.ts create mode 100644 packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/prometheus-exporter.ts create mode 100644 packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/response.type.ts create mode 100644 packages/cactus-plugin-ledger-connector-besu/src/main/typescript/web-services/get-prometheus-exporter-metrics-endpoint-v1.ts diff --git a/packages/cactus-plugin-ledger-connector-besu/README.md b/packages/cactus-plugin-ledger-connector-besu/README.md index 78cbcb9548..c4026b06f8 100644 --- a/packages/cactus-plugin-ledger-connector-besu/README.md +++ b/packages/cactus-plugin-ledger-connector-besu/README.md @@ -8,6 +8,7 @@ This plugin provides `Cactus` a way to interact with Besu networks. Using this w - [Getting Started](#getting-started) - [Usage](#usage) + - [Prometheus Exporter](#prometheus-exporter) - [Runing the tests](#running-the-tests) - [Built With](#built-with) - [Contributing](#contributing) @@ -33,7 +34,7 @@ In the project root folder, run this command to compile the plugin and create th npm run tsc ``` -## Usage +This class creates a prometheus exporter, which scraps the transactions (total transaction count) for the use cases incorporating the use of Besu connector plugin. To use this import public-api and create new **PluginFactoryLedgerConnector**. Then use it to create a connector. ```typescript @@ -80,6 +81,45 @@ enum Web3SigningCredentialType { ``` > Extensive documentation and examples in the [readthedocs](https://readthedocs.org/projects/hyperledger-cactus/) (WIP) +## Prometheus Exporter + +This class creates a prometheus exporter, which scraps the transactions (total transaction count) for the use cases incorporating the use of Besu connector plugin. + +### Usage +The prometheus exporter object is initialized in the `PluginLedgerConnectorBesu` class constructor itself, so instantiating the object of the `PluginLedgerConnectorBesu` class, gives access to the exporter object. +You can also initialize the prometheus exporter object seperately and then pass it to the `IPluginLedgerConnectorBesuOptions` interface for `PluginLedgerConnectoBesu` constructor. + +`getPrometheusExporterMetricsEndpointV1` function returns the prometheus exporter metrics, currently displaying the total transaction count, which currently increments everytime the `transact()` method of the `PluginLedgerConnectorBesu` class is called. + +### Prometheus Integration +To use Prometheus with this exporter make sure to install [Prometheus main component](https://prometheus.io/download/). +Once Prometheus is setup, the corresponding scrape_config needs to be added to the prometheus.yml + +```(yaml) +- job_name: 'besu_ledger_connector_exporter' + metrics_path: api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-prometheus-exporter-metrics + scrape_interval: 5s + static_configs: + - targets: ['{host}:{port}'] +``` + +Here the `host:port` is where the prometheus exporter metrics are exposed. The test cases (For example, packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/deploy-contract-from-json.test.ts) exposes it over `0.0.0.0` and a random port(). The random port can be found in the running logs of the test case and looks like (42379 in the below mentioned URL) +`Metrics URL: http://0.0.0.0:42379/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-prometheus-exporter-metrics` + +Once edited, you can start the prometheus service by referencing the above edited prometheus.yml file. +On the prometheus graphical interface (defaulted to http://localhost:9090), choose **Graph** from the menu bar, then select the **Console** tab. From the **Insert metric at cursor** drop down, select **cactus_besu_total_tx_count** and click **execute** + +### Helper code + +###### response.type.ts +This file contains the various responses of the metrics. + +###### data-fetcher.ts +This file contains functions encasing the logic to process the data points + +###### metrics.ts +This file lists all the prometheus metrics and what they are used for. + ## Running the tests To check that all has been installed correctly and that the pugin has no errors run the tests: @@ -99,4 +139,4 @@ Please review [CONTIRBUTING.md](../../CONTRIBUTING.md) to get started. This distribution is published under the Apache License Version 2.0 found in the [LICENSE](../../LICENSE) file. -## Acknowledgments \ No newline at end of file +## Acknowledgments diff --git a/packages/cactus-plugin-ledger-connector-besu/package-lock.json b/packages/cactus-plugin-ledger-connector-besu/package-lock.json index fcd85d4591..3ea87d1444 100644 --- a/packages/cactus-plugin-ledger-connector-besu/package-lock.json +++ b/packages/cactus-plugin-ledger-connector-besu/package-lock.json @@ -280,6 +280,11 @@ "file-uri-to-path": "1.0.0" } }, + "bintrees": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/bintrees/-/bintrees-1.0.1.tgz", + "integrity": "sha1-DmVcm5wkNeqraL9AJyJtK1WjRSQ=" + }, "bip66": { "version": "1.1.5", "resolved": "https://registry.npmjs.org/bip66/-/bip66-1.1.5.tgz", @@ -1961,6 +1966,14 @@ "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" }, + "prom-client": { + "version": "13.1.0", + "resolved": "https://registry.npmjs.org/prom-client/-/prom-client-13.1.0.tgz", + "integrity": "sha512-jT9VccZCWrJWXdyEtQddCDszYsiuWj5T0ekrPszi/WEegj3IZy6Mm09iOOVM86A4IKMWq8hZkT2dD9MaSe+sng==", + "requires": { + "tdigest": "^0.1.1" + } + }, "proxy-addr": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.6.tgz", @@ -2439,6 +2452,14 @@ } } }, + "tdigest": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/tdigest/-/tdigest-0.1.1.tgz", + "integrity": "sha1-Ljyyw56kSeVdHmzZEReszKRYgCE=", + "requires": { + "bintrees": "1.0.1" + } + }, "through": { "version": "2.3.8", "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", diff --git a/packages/cactus-plugin-ledger-connector-besu/package.json b/packages/cactus-plugin-ledger-connector-besu/package.json index 532485cb90..898eb077ff 100644 --- a/packages/cactus-plugin-ledger-connector-besu/package.json +++ b/packages/cactus-plugin-ledger-connector-besu/package.json @@ -33,7 +33,10 @@ "ignore": [ "src/**/generated/*" ], - "extensions": ["ts", "json"], + "extensions": [ + "ts", + "json" + ], "quiet": true, "verbose": false, "runOnChangeOnly": true @@ -87,6 +90,7 @@ "express": "4.17.1", "joi": "14.3.1", "openapi-types": "7.0.1", + "prom-client": "13.1.0", "typescript-optional": "2.0.1", "web3": "1.2.7", "web3-eea": "0.10.0" diff --git a/packages/cactus-plugin-ledger-connector-besu/src/main/json/openapi.json b/packages/cactus-plugin-ledger-connector-besu/src/main/json/openapi.json index be44eb67c3..9f16d973ac 100644 --- a/packages/cactus-plugin-ledger-connector-besu/src/main/json/openapi.json +++ b/packages/cactus-plugin-ledger-connector-besu/src/main/json/openapi.json @@ -552,6 +552,10 @@ "nullable": false } } + }, + "PrometheusExporterMetricsResponse": { + "type": "string", + "nullable": false } } }, @@ -696,6 +700,31 @@ } } } + }, + "/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-prometheus-exporter-metrics": { + "get": { + "x-hyperledger-cactus": { + "http": { + "verbLowerCase": "get", + "path": "/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-prometheus-exporter-metrics" + } + }, + "operationId": "getPrometheusExporterMetricsV1", + "summary": "Get the Prometheus Metrics", + "parameters": [], + "responses": { + "200": { + "description": "OK", + "content": { + "text/plain": { + "schema": { + "$ref": "#/components/schemas/PrometheusExporterMetricsResponse" + } + } + } + } + } + } } } } \ No newline at end of file diff --git a/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/generated/openapi/typescript-axios/api.ts b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/generated/openapi/typescript-axios/api.ts index 55844bd813..507b883ad8 100644 --- a/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/generated/openapi/typescript-axios/api.ts +++ b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/generated/openapi/typescript-axios/api.ts @@ -639,6 +639,42 @@ export const DefaultApiAxiosParamCreator = function (configuration?: Configurati options: localVarRequestOptions, }; }, + /** + * + * @summary Get the Prometheus Metrics + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + getPrometheusExporterMetricsV1: async (options: any = {}): Promise => { + const localVarPath = `/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-prometheus-exporter-metrics`; + // use dummy base URL string because the URL constructor only accepts absolute URLs. + const localVarUrlObj = new URL(localVarPath, 'https://example.com'); + let baseOptions; + if (configuration) { + baseOptions = configuration.baseOptions; + } + const localVarRequestOptions = { method: 'GET', ...baseOptions, ...options}; + const localVarHeaderParameter = {} as any; + const localVarQueryParameter = {} as any; + + + + const query = new URLSearchParams(localVarUrlObj.search); + for (const key in localVarQueryParameter) { + query.set(key, localVarQueryParameter[key]); + } + for (const key in options.query) { + query.set(key, options.query[key]); + } + localVarUrlObj.search = (new URLSearchParams(query)).toString(); + let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {}; + localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers}; + + return { + url: localVarUrlObj.pathname + localVarUrlObj.search + localVarUrlObj.hash, + options: localVarRequestOptions, + }; + }, /** * Obtain signatures of ledger from the corresponding transaction hash. * @summary Obtain signatures of ledger from the corresponding transaction hash. @@ -735,6 +771,19 @@ export const DefaultApiFp = function(configuration?: Configuration) { return axios.request(axiosRequestArgs); }; }, + /** + * + * @summary Get the Prometheus Metrics + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + async getPrometheusExporterMetricsV1(options?: any): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { + const localVarAxiosArgs = await DefaultApiAxiosParamCreator(configuration).getPrometheusExporterMetricsV1(options); + return (axios: AxiosInstance = globalAxios, basePath: string = BASE_PATH) => { + const axiosRequestArgs = {...localVarAxiosArgs.options, url: basePath + localVarAxiosArgs.url}; + return axios.request(axiosRequestArgs); + }; + }, /** * Obtain signatures of ledger from the corresponding transaction hash. * @summary Obtain signatures of ledger from the corresponding transaction hash. @@ -788,6 +837,15 @@ export const DefaultApiFactory = function (configuration?: Configuration, basePa apiV1BesuRunTransaction(runTransactionRequest?: RunTransactionRequest, options?: any): AxiosPromise { return DefaultApiFp(configuration).apiV1BesuRunTransaction(runTransactionRequest, options).then((request) => request(axios, basePath)); }, + /** + * + * @summary Get the Prometheus Metrics + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + getPrometheusExporterMetricsV1(options?: any): AxiosPromise { + return DefaultApiFp(configuration).getPrometheusExporterMetricsV1(options).then((request) => request(axios, basePath)); + }, /** * Obtain signatures of ledger from the corresponding transaction hash. * @summary Obtain signatures of ledger from the corresponding transaction hash. @@ -844,6 +902,17 @@ export class DefaultApi extends BaseAPI { return DefaultApiFp(this.configuration).apiV1BesuRunTransaction(runTransactionRequest, options).then((request) => request(this.axios, this.basePath)); } + /** + * + * @summary Get the Prometheus Metrics + * @param {*} [options] Override http request option. + * @throws {RequiredError} + * @memberof DefaultApi + */ + public getPrometheusExporterMetricsV1(options?: any) { + return DefaultApiFp(this.configuration).getPrometheusExporterMetricsV1(options).then((request) => request(this.axios, this.basePath)); + } + /** * Obtain signatures of ledger from the corresponding transaction hash. * @summary Obtain signatures of ledger from the corresponding transaction hash. diff --git a/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/plugin-ledger-connector-besu.ts b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/plugin-ledger-connector-besu.ts index 7b90edb163..371023d59b 100644 --- a/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/plugin-ledger-connector-besu.ts +++ b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/plugin-ledger-connector-besu.ts @@ -55,6 +55,11 @@ import { RunTransactionEndpoint } from "./web-services/run-transaction-endpoint" import { InvokeContractEndpoint } from "./web-services/invoke-contract-endpoint"; import { isWeb3SigningCredentialNone } from "./model-type-guards"; import { BesuSignTransactionEndpointV1 } from "./web-services/sign-transaction-endpoint-v1"; +import { PrometheusExporter } from "./prometheus-exporter/prometheus-exporter"; +import { + GetPrometheusExporterMetricsEndpointV1, + IGetPrometheusExporterMetricsEndpointV1Options, +} from "./web-services/get-prometheus-exporter-metrics-endpoint-v1"; export const E_KEYCHAIN_NOT_FOUND = "cactus.connector.besu.keychain_not_found"; @@ -62,6 +67,7 @@ export interface IPluginLedgerConnectorBesuOptions extends ICactusPluginOptions { rpcApiHttpHost: string; pluginRegistry: PluginRegistry; + prometheusExporter?: PrometheusExporter; logLevel?: LogLevelDesc; } @@ -76,6 +82,7 @@ export class PluginLedgerConnectorBesu ICactusPlugin, IPluginWebService { private readonly instanceId: string; + public prometheusExporter: PrometheusExporter; private readonly log: Logger; private readonly web3: Web3; private readonly pluginRegistry: PluginRegistry; @@ -104,6 +111,25 @@ export class PluginLedgerConnectorBesu this.web3 = new Web3(web3Provider); this.instanceId = options.instanceId; this.pluginRegistry = options.pluginRegistry; + this.prometheusExporter = + options.prometheusExporter || + new PrometheusExporter({ pollingIntervalInMin: 1 }); + Checks.truthy( + this.prometheusExporter, + `${fnTag} options.prometheusExporter`, + ); + + this.prometheusExporter.startMetricsCollection(); + } + + public getPrometheusExporter(): PrometheusExporter { + return this.prometheusExporter; + } + + public async getPrometheusExporterMetrics(): Promise { + const res: string = await this.prometheusExporter.getPrometheusMetrics(); + this.log.debug(`getPrometheusExporterMetrics() response: %o`, res); + return res; } public getInstanceId(): string { @@ -158,6 +184,16 @@ export class PluginLedgerConnectorBesu endpoint.registerExpress(expressApp); endpoints.push(endpoint); } + + { + const opts: IGetPrometheusExporterMetricsEndpointV1Options = { + connector: this, + logLevel: this.options.logLevel, + }; + const endpoint = new GetPrometheusExporterMetricsEndpointV1(opts); + endpoint.registerExpress(expressApp); + endpoints.push(endpoint); + } return endpoints; } @@ -274,6 +310,7 @@ export class PluginLedgerConnectorBesu this.log.debug(`${fnTag} Web3 sendSignedTransaction failed`, receipt); throw receipt; } else { + this.prometheusExporter.addCurrentTransaction(); return { transactionReceipt: receipt }; } } diff --git a/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/data-fetcher.ts b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/data-fetcher.ts new file mode 100644 index 0000000000..bef37517f6 --- /dev/null +++ b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/data-fetcher.ts @@ -0,0 +1,7 @@ +import { Transactions } from "./response.type"; + +import { totalTxCount } from "./metrics"; + +export async function collectMetrics(transactions: Transactions) { + totalTxCount.labels("cactus_besu_total_tx_count").set(transactions.counter); +} diff --git a/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/metrics.ts b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/metrics.ts new file mode 100644 index 0000000000..49612c8a19 --- /dev/null +++ b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/metrics.ts @@ -0,0 +1,7 @@ +import { Gauge } from "prom-client"; + +export const totalTxCount = new Gauge({ + name: "cactus_besu_total_tx_count", + help: "Total transactions executed", + labelNames: ["type"], +}); diff --git a/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/prometheus-exporter.ts b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/prometheus-exporter.ts new file mode 100644 index 0000000000..5ae1e7c690 --- /dev/null +++ b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/prometheus-exporter.ts @@ -0,0 +1,41 @@ +import promClient from "prom-client"; +import { Transactions } from "./response.type"; +import { totalTxCount } from "./metrics"; + +export const K_CACTUS_BESU_TOTAL_TX_COUNT = "cactus_besu_total_tx_count"; + +export interface IPrometheusExporterOptions { + pollingIntervalInMin?: number; +} + +export class PrometheusExporter { + public readonly metricsPollingIntervalInMin: number; + public readonly transactions: Transactions = { counter: 0 }; + + constructor( + public readonly prometheusExporterOptions: IPrometheusExporterOptions, + ) { + this.metricsPollingIntervalInMin = + prometheusExporterOptions.pollingIntervalInMin || 1; + } + + public addCurrentTransaction(): void { + this.transactions.counter++; + totalTxCount + .labels(K_CACTUS_BESU_TOTAL_TX_COUNT) + .set(this.transactions.counter); + } + + public async getPrometheusMetrics(): Promise { + const result = await promClient.register.getSingleMetricAsString( + K_CACTUS_BESU_TOTAL_TX_COUNT, + ); + return result; + } + + public startMetricsCollection(): void { + const Registry = promClient.Registry; + const register = new Registry(); + promClient.collectDefaultMetrics({ register }); + } +} diff --git a/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/response.type.ts b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/response.type.ts new file mode 100644 index 0000000000..3f1bc7f491 --- /dev/null +++ b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/response.type.ts @@ -0,0 +1,3 @@ +export type Transactions = { + counter: number; +}; diff --git a/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/web-services/get-prometheus-exporter-metrics-endpoint-v1.ts b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/web-services/get-prometheus-exporter-metrics-endpoint-v1.ts new file mode 100644 index 0000000000..bdc0757d94 --- /dev/null +++ b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/web-services/get-prometheus-exporter-metrics-endpoint-v1.ts @@ -0,0 +1,80 @@ +import { Express, Request, Response } from "express"; + +import { registerWebServiceEndpoint } from "@hyperledger/cactus-core"; + +import OAS from "../../json/openapi.json"; + +import { + IWebServiceEndpoint, + IExpressRequestHandler, +} from "@hyperledger/cactus-core-api"; + +import { + LogLevelDesc, + Logger, + LoggerProvider, + Checks, +} from "@hyperledger/cactus-common"; + +import { PluginLedgerConnectorBesu } from "../plugin-ledger-connector-besu"; + +export interface IGetPrometheusExporterMetricsEndpointV1Options { + connector: PluginLedgerConnectorBesu; + logLevel?: LogLevelDesc; +} + +export class GetPrometheusExporterMetricsEndpointV1 + implements IWebServiceEndpoint { + private readonly log: Logger; + + constructor( + public readonly options: IGetPrometheusExporterMetricsEndpointV1Options, + ) { + const fnTag = "GetPrometheusExporterMetricsEndpointV1#constructor()"; + + Checks.truthy(options, `${fnTag} options`); + Checks.truthy(options.connector, `${fnTag} options.connector`); + + const label = "get-prometheus-exporter-metrics-endpoint"; + const level = options.logLevel || "INFO"; + this.log = LoggerProvider.getOrCreate({ label, level }); + } + + public getExpressRequestHandler(): IExpressRequestHandler { + return this.handleRequest.bind(this); + } + + getPath(): string { + return OAS.paths[ + "/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-prometheus-exporter-metrics" + ].get["x-hyperledger-cactus"].http.path; + } + + getVerbLowerCase(): string { + return OAS.paths[ + "/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-prometheus-exporter-metrics" + ].get["x-hyperledger-cactus"].http.verbLowerCase; + } + + registerExpress(app: Express): IWebServiceEndpoint { + registerWebServiceEndpoint(app, this); + return this; + } + + async handleRequest(req: Request, res: Response): Promise { + const fnTag = "GetPrometheusExporterMetrics#handleRequest()"; + const verbUpper = this.getVerbLowerCase().toUpperCase(); + this.log.debug(`${verbUpper} ${this.getPath()}`); + + try { + const resBody = await this.options.connector.getPrometheusExporterMetrics(); + res.status(200); + res.send(resBody); + } catch (ex) { + this.log.error(`${fnTag} failed to serve request`, ex); + res.status(500); + res.statusMessage = ex.message; + res.json({ error: ex.stack }); + } + } +} diff --git a/packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/deploy-contract-from-json.test.ts b/packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/deploy-contract-from-json.test.ts index 720790d6d8..1418addbe1 100644 --- a/packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/deploy-contract-from-json.test.ts +++ b/packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/deploy-contract-from-json.test.ts @@ -7,13 +7,22 @@ import { PluginLedgerConnectorBesu, PluginFactoryLedgerConnector, Web3SigningCredentialCactusKeychainRef, + DefaultApi as BesuApi, } from "../../../../../main/typescript/public-api"; import { PluginKeychainMemory } from "@hyperledger/cactus-plugin-keychain-memory"; import { BesuTestLedger } from "@hyperledger/cactus-test-tooling"; -import { LogLevelDesc } from "@hyperledger/cactus-common"; +import { + LogLevelDesc, + IListenOptions, + Servers, +} from "@hyperledger/cactus-common"; import HelloWorldContractJson from "../../../../solidity/hello-world-contract/HelloWorld.json"; import Web3 from "web3"; import { PluginImportType } from "@hyperledger/cactus-core-api"; +import express from "express"; +import bodyParser from "body-parser"; +import http from "http"; +import { AddressInfo } from "net"; test("deploys contract via .json file", async (t: Test) => { const logLevel: LogLevelDesc = "TRACE"; @@ -62,6 +71,25 @@ test("deploys contract via .json file", async (t: Test) => { pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }), }); + const expressApp = express(); + expressApp.use(bodyParser.json({ limit: "250mb" })); + const server = http.createServer(expressApp); + const listenOptions: IListenOptions = { + hostname: "0.0.0.0", + port: 32845, + server, + }; + const addressInfo = (await Servers.listen(listenOptions)) as AddressInfo; + test.onFinish(async () => await Servers.shutdown(server)); + const { address, port } = addressInfo; + const apiHost = `http://${address}:${port}`; + t.comment( + `Metrics URL: ${apiHost}/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-prometheus-exporter-metrics`, + ); + const apiClient = new BesuApi({ basePath: apiHost }); + + await connector.installWebServices(expressApp); + await connector.transact({ web3SigningCredential: { ethAccount: firstHighNetWorthAccount, @@ -313,5 +341,21 @@ test("deploys contract via .json file", async (t: Test) => { t2.end(); }); + test("get prometheus exporter metrics", async (t2: Test) => { + const res = await apiClient.getPrometheusExporterMetricsV1(); + const promMetricsOutput = + "# HELP cactus_besu_total_tx_count Total transactions executed\n" + + "# TYPE cactus_besu_total_tx_count gauge\n" + + 'cactus_besu_total_tx_count{type="cactus_besu_total_tx_count"} 9'; + t2.ok(res); + t2.ok(res.data); + t2.equal(res.status, 200); + t2.true( + res.data.includes(promMetricsOutput), + "Total Transaction Count of 9 recorded as expected. RESULT OK.", + ); + t2.end(); + }); + t.end(); }); diff --git a/packages/cactus-test-tooling/src/main/typescript/besu/besu-test-ledger.ts b/packages/cactus-test-tooling/src/main/typescript/besu/besu-test-ledger.ts index aabc44d898..c935dfbf19 100644 --- a/packages/cactus-test-tooling/src/main/typescript/besu/besu-test-ledger.ts +++ b/packages/cactus-test-tooling/src/main/typescript/besu/besu-test-ledger.ts @@ -18,7 +18,7 @@ export interface IBesuTestLedgerConstructorOptions { } export const BESU_TEST_LEDGER_DEFAULT_OPTIONS = Object.freeze({ - containerImageVersion: "2021-01-08-7a055c3", + containerImageVersion: "latest", containerImageName: "hyperledger/cactus-besu-all-in-one", rpcApiHttpPort: 8545, envVars: ["BESU_NETWORK=dev"], diff --git a/packages/cactus-test-tooling/src/main/typescript/fabric/fabric-test-ledger-v1.ts b/packages/cactus-test-tooling/src/main/typescript/fabric/fabric-test-ledger-v1.ts index af15366445..1cbaa5a4dc 100644 --- a/packages/cactus-test-tooling/src/main/typescript/fabric/fabric-test-ledger-v1.ts +++ b/packages/cactus-test-tooling/src/main/typescript/fabric/fabric-test-ledger-v1.ts @@ -40,7 +40,7 @@ export interface IFabricTestLedgerV1ConstructorOptions { * Provides default options for Fabric container */ const DEFAULT_OPTS = Object.freeze({ - imageVersion: "2021-01-05-3400c06", + imageVersion: "latest", imageName: "hyperledger/cactus-fabric-all-in-one", envVars: new Map([["FABRIC_VERSION", "1.4.8"]]), }); diff --git a/packages/cactus-test-tooling/src/main/typescript/quorum/quorum-test-ledger.ts b/packages/cactus-test-tooling/src/main/typescript/quorum/quorum-test-ledger.ts index 90d75f9d82..7fa3e4061c 100644 --- a/packages/cactus-test-tooling/src/main/typescript/quorum/quorum-test-ledger.ts +++ b/packages/cactus-test-tooling/src/main/typescript/quorum/quorum-test-ledger.ts @@ -18,7 +18,7 @@ export interface IQuorumTestLedgerConstructorOptions { } export const QUORUM_TEST_LEDGER_DEFAULT_OPTIONS = Object.freeze({ - containerImageVersion: "2021-01-08-7a055c3", + containerImageVersion: "latest", containerImageName: "hyperledger/cactus-quorum-all-in-one", rpcApiHttpPort: 8545, });