Skip to content

Commit

Permalink
feat(quorum): add prometheus exporter
Browse files Browse the repository at this point in the history
	Primary Change
	--------------

	1. The quorum 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. IPluginLedgerConnectorQuorumOptions interface in PluginLedgerConnectorQuorum class now has a prometheusExporter optional field
	5. The PluginLedgerConnectorQuorum 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 hyperledger-cacti#534

Signed-off-by: Jagpreet Singh Sasan <[email protected]>
  • Loading branch information
jagpreetsinghsasan committed Mar 10, 2021
1 parent 24d593d commit 2d671ec
Show file tree
Hide file tree
Showing 15 changed files with 410 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.0.0
5.0.0-beta2
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,6 @@ export const DefaultApiAxiosParamCreator = function (configuration?: Configurati
if (configuration) {
baseOptions = configuration.baseOptions;
}

const localVarRequestOptions = { method: 'POST', ...baseOptions, ...options};
const localVarHeaderParameter = {} as any;
const localVarQueryParameter = {} as any;
Expand All @@ -540,23 +539,18 @@ export const DefaultApiAxiosParamCreator = function (configuration?: Configurati

localVarHeaderParameter['Content-Type'] = 'application/json';

const queryParameters = new URLSearchParams(localVarUrlObj.search);
const query = new URLSearchParams(localVarUrlObj.search);
for (const key in localVarQueryParameter) {
queryParameters.set(key, localVarQueryParameter[key]);
query.set(key, localVarQueryParameter[key]);
}
for (const key in options.query) {
queryParameters.set(key, options.query[key]);
query.set(key, options.query[key]);
}
localVarUrlObj.search = (new URLSearchParams(queryParameters)).toString();
localVarUrlObj.search = (new URLSearchParams(query)).toString();
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers};
const nonString = typeof deployContractSolidityBytecodeV1Request !== 'string';
const needsSerialization = nonString && configuration && configuration.isJsonMime
? configuration.isJsonMime(localVarRequestOptions.headers['Content-Type'])
: nonString;
localVarRequestOptions.data = needsSerialization
? JSON.stringify(deployContractSolidityBytecodeV1Request !== undefined ? deployContractSolidityBytecodeV1Request : {})
: (deployContractSolidityBytecodeV1Request || "");
const needsSerialization = (typeof deployContractSolidityBytecodeV1Request !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(deployContractSolidityBytecodeV1Request !== undefined ? deployContractSolidityBytecodeV1Request : {}) : (deployContractSolidityBytecodeV1Request || "");

return {
url: localVarUrlObj.pathname + localVarUrlObj.search + localVarUrlObj.hash,
Expand All @@ -578,7 +572,6 @@ export const DefaultApiAxiosParamCreator = function (configuration?: Configurati
if (configuration) {
baseOptions = configuration.baseOptions;
}

const localVarRequestOptions = { method: 'POST', ...baseOptions, ...options};
const localVarHeaderParameter = {} as any;
const localVarQueryParameter = {} as any;
Expand All @@ -587,23 +580,18 @@ export const DefaultApiAxiosParamCreator = function (configuration?: Configurati

localVarHeaderParameter['Content-Type'] = 'application/json';

const queryParameters = new URLSearchParams(localVarUrlObj.search);
const query = new URLSearchParams(localVarUrlObj.search);
for (const key in localVarQueryParameter) {
queryParameters.set(key, localVarQueryParameter[key]);
query.set(key, localVarQueryParameter[key]);
}
for (const key in options.query) {
queryParameters.set(key, options.query[key]);
query.set(key, options.query[key]);
}
localVarUrlObj.search = (new URLSearchParams(queryParameters)).toString();
localVarUrlObj.search = (new URLSearchParams(query)).toString();
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers};
const nonString = typeof invokeContractV1Request !== 'string';
const needsSerialization = nonString && configuration && configuration.isJsonMime
? configuration.isJsonMime(localVarRequestOptions.headers['Content-Type'])
: nonString;
localVarRequestOptions.data = needsSerialization
? JSON.stringify(invokeContractV1Request !== undefined ? invokeContractV1Request : {})
: (invokeContractV1Request || "");
const needsSerialization = (typeof invokeContractV1Request !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(invokeContractV1Request !== undefined ? invokeContractV1Request : {}) : (invokeContractV1Request || "");

return {
url: localVarUrlObj.pathname + localVarUrlObj.search + localVarUrlObj.hash,
Expand All @@ -625,7 +613,6 @@ export const DefaultApiAxiosParamCreator = function (configuration?: Configurati
if (configuration) {
baseOptions = configuration.baseOptions;
}

const localVarRequestOptions = { method: 'POST', ...baseOptions, ...options};
const localVarHeaderParameter = {} as any;
const localVarQueryParameter = {} as any;
Expand All @@ -634,23 +621,18 @@ export const DefaultApiAxiosParamCreator = function (configuration?: Configurati

localVarHeaderParameter['Content-Type'] = 'application/json';

const queryParameters = new URLSearchParams(localVarUrlObj.search);
const query = new URLSearchParams(localVarUrlObj.search);
for (const key in localVarQueryParameter) {
queryParameters.set(key, localVarQueryParameter[key]);
query.set(key, localVarQueryParameter[key]);
}
for (const key in options.query) {
queryParameters.set(key, options.query[key]);
query.set(key, options.query[key]);
}
localVarUrlObj.search = (new URLSearchParams(queryParameters)).toString();
localVarUrlObj.search = (new URLSearchParams(query)).toString();
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers};
const nonString = typeof runTransactionRequest !== 'string';
const needsSerialization = nonString && configuration && configuration.isJsonMime
? configuration.isJsonMime(localVarRequestOptions.headers['Content-Type'])
: nonString;
localVarRequestOptions.data = needsSerialization
? JSON.stringify(runTransactionRequest !== undefined ? runTransactionRequest : {})
: (runTransactionRequest || "");
const needsSerialization = (typeof runTransactionRequest !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(runTransactionRequest !== undefined ? runTransactionRequest : {}) : (runTransactionRequest || "");

return {
url: localVarUrlObj.pathname + localVarUrlObj.search + localVarUrlObj.hash,
Expand All @@ -676,7 +658,6 @@ export const DefaultApiAxiosParamCreator = function (configuration?: Configurati
if (configuration) {
baseOptions = configuration.baseOptions;
}

const localVarRequestOptions = { method: 'POST', ...baseOptions, ...options};
const localVarHeaderParameter = {} as any;
const localVarQueryParameter = {} as any;
Expand All @@ -685,23 +666,18 @@ export const DefaultApiAxiosParamCreator = function (configuration?: Configurati

localVarHeaderParameter['Content-Type'] = 'application/json';

const queryParameters = new URLSearchParams(localVarUrlObj.search);
const query = new URLSearchParams(localVarUrlObj.search);
for (const key in localVarQueryParameter) {
queryParameters.set(key, localVarQueryParameter[key]);
query.set(key, localVarQueryParameter[key]);
}
for (const key in options.query) {
queryParameters.set(key, options.query[key]);
query.set(key, options.query[key]);
}
localVarUrlObj.search = (new URLSearchParams(queryParameters)).toString();
localVarUrlObj.search = (new URLSearchParams(query)).toString();
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers};
const nonString = typeof signTransactionRequest !== 'string';
const needsSerialization = nonString && configuration && configuration.isJsonMime
? configuration.isJsonMime(localVarRequestOptions.headers['Content-Type'])
: nonString;
localVarRequestOptions.data = needsSerialization
? JSON.stringify(signTransactionRequest !== undefined ? signTransactionRequest : {})
: (signTransactionRequest || "");
const needsSerialization = (typeof signTransactionRequest !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(signTransactionRequest !== undefined ? signTransactionRequest : {}) : (signTransactionRequest || "");

return {
url: localVarUrlObj.pathname + localVarUrlObj.search + localVarUrlObj.hash,
Expand All @@ -727,7 +703,7 @@ export const DefaultApiFp = function(configuration?: Configuration) {
async apiV1BesuDeployContractSolidityBytecode(deployContractSolidityBytecodeV1Request?: DeployContractSolidityBytecodeV1Request, options?: any): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<DeployContractSolidityBytecodeV1Response>> {
const localVarAxiosArgs = await DefaultApiAxiosParamCreator(configuration).apiV1BesuDeployContractSolidityBytecode(deployContractSolidityBytecodeV1Request, options);
return (axios: AxiosInstance = globalAxios, basePath: string = BASE_PATH) => {
const axiosRequestArgs = {...localVarAxiosArgs.options, url: (configuration?.basePath || basePath) + localVarAxiosArgs.url};
const axiosRequestArgs = {...localVarAxiosArgs.options, url: basePath + localVarAxiosArgs.url};
return axios.request(axiosRequestArgs);
};
},
Expand All @@ -741,7 +717,7 @@ export const DefaultApiFp = function(configuration?: Configuration) {
async apiV1BesuInvokeContract(invokeContractV1Request?: InvokeContractV1Request, options?: any): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<InvokeContractV1Response>> {
const localVarAxiosArgs = await DefaultApiAxiosParamCreator(configuration).apiV1BesuInvokeContract(invokeContractV1Request, options);
return (axios: AxiosInstance = globalAxios, basePath: string = BASE_PATH) => {
const axiosRequestArgs = {...localVarAxiosArgs.options, url: (configuration?.basePath || basePath) + localVarAxiosArgs.url};
const axiosRequestArgs = {...localVarAxiosArgs.options, url: basePath + localVarAxiosArgs.url};
return axios.request(axiosRequestArgs);
};
},
Expand All @@ -755,7 +731,7 @@ export const DefaultApiFp = function(configuration?: Configuration) {
async apiV1BesuRunTransaction(runTransactionRequest?: RunTransactionRequest, options?: any): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<RunTransactionResponse>> {
const localVarAxiosArgs = await DefaultApiAxiosParamCreator(configuration).apiV1BesuRunTransaction(runTransactionRequest, options);
return (axios: AxiosInstance = globalAxios, basePath: string = BASE_PATH) => {
const axiosRequestArgs = {...localVarAxiosArgs.options, url: (configuration?.basePath || basePath) + localVarAxiosArgs.url};
const axiosRequestArgs = {...localVarAxiosArgs.options, url: basePath + localVarAxiosArgs.url};
return axios.request(axiosRequestArgs);
};
},
Expand All @@ -769,7 +745,7 @@ export const DefaultApiFp = function(configuration?: Configuration) {
async signTransactionV1(signTransactionRequest: SignTransactionRequest, options?: any): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<SignTransactionResponse>> {
const localVarAxiosArgs = await DefaultApiAxiosParamCreator(configuration).signTransactionV1(signTransactionRequest, options);
return (axios: AxiosInstance = globalAxios, basePath: string = BASE_PATH) => {
const axiosRequestArgs = {...localVarAxiosArgs.options, url: (configuration?.basePath || basePath) + localVarAxiosArgs.url};
const axiosRequestArgs = {...localVarAxiosArgs.options, url: basePath + localVarAxiosArgs.url};
return axios.request(axiosRequestArgs);
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export interface ConfigurationParameters {
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string) | ((name?: string, scopes?: string[]) => Promise<string>);
basePath?: string;
baseOptions?: any;
formDataCtor?: new () => any;
}

export class Configuration {
Expand Down Expand Up @@ -65,14 +64,6 @@ export class Configuration {
* @memberof Configuration
*/
baseOptions?: any;
/**
* The FormData constructor that will be used to create multipart form data
* requests. You can inject this here so that execution environments that
* do not support the FormData class can still run the generated client.
*
* @type {new () => FormData}
*/
formDataCtor?: new () => any;

constructor(param: ConfigurationParameters = {}) {
this.apiKey = param.apiKey;
Expand All @@ -81,21 +72,5 @@ export class Configuration {
this.accessToken = param.accessToken;
this.basePath = param.basePath;
this.baseOptions = param.baseOptions;
this.formDataCtor = param.formDataCtor;
}

/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
* application/vnd.company+json
* @param mime - MIME (Multipurpose Internet Mail Extensions)
* @return True if the given MIME is JSON, false otherwise.
*/
public isJsonMime(mime: string): boolean {
const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
return mime !== null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json');
}
}
39 changes: 35 additions & 4 deletions packages/cactus-plugin-ledger-connector-quorum/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
# `@hyperledger/cactus-plugin-ledger-connector-quorum`

> TODO: description
## Prometheus Exporter

## Usage
This class creates a prometheus exporter, which scraps the transactions (total transaction count) for the use cases incorporating the use of Quorum connector plugin.

### Usage
The prometheus exporter object is initialized in the `PluginLedgerConnectorQuorum` class constructor itself, so instantiating the object of the `PluginLedgerConnectorQuorum` class, gives access to the exporter object.
You can also initialize the prometheus exporter object seperately and then pass it to the `IPluginLedgerConnectorQuorumOptions` interface for `PluginLedgerConnectoQuorum` constructor.

`getPrometheusExporterMetricsEndpointV1` function returns the prometheus exporter metrics, currently displaying the total transaction count, which currently increments everytime the `transact()` method of the `PluginLedgerConnectorQuorum` 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: 'quorum_ledger_connector_exporter'
metrics_path: api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-quorum/get-prometheus-exporter-metrics
scrape_interval: 5s
static_configs:
- targets: ['{host}:{port}']
```
// TODO: DEMONSTRATE API
```

Here the `host:port` is where the prometheus exporter metrics are exposed. The test cases (For example, packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/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-quorum/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_quorum_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.
21 changes: 21 additions & 0 deletions packages/cactus-plugin-ledger-connector-quorum/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion packages/cactus-plugin-ledger-connector-quorum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
"ignore": [
"src/**/generated/*"
],
"extensions": ["ts", "json"],
"extensions": [
"ts",
"json"
],
"quiet": true,
"verbose": false,
"runOnChangeOnly": true
Expand Down Expand Up @@ -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-core-method": "1.3.0",
Expand Down
Loading

0 comments on commit 2d671ec

Please sign in to comment.