Skip to content

Commit

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

	1. The consortium-manual 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. IPluginConsortiumManualOptions interface in PluginConsortiumManual class now has a prometheusExporter optional field
	5. The PluginConsortiumManual class has relevant functions and codes to incorporate prometheus exporter
	6. get-node-jws-endpoint-v1.test.ts is changed to incorporate the prometheus exporter
	7. Added Readme.md on the prometheus exporter usage

	Fixes #538

Signed-off-by: Jagpreet Singh Sasan <jagpreet.singh.sasan@accenture.com>
  • Loading branch information
jagpreetsinghsasan authored and petermetz committed Mar 30, 2021
1 parent adf0dea commit 853bdc4
Show file tree
Hide file tree
Showing 13 changed files with 498 additions and 2 deletions.
39 changes: 39 additions & 0 deletions packages/cactus-plugin-consortium-manual/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# `@hyperledger/cactus-plugin-consortium-manual`

## Prometheus Exporter
This class creates a prometheus exporter, which scrapes the total Cactus node count.

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

`getPrometheusExporterMetricsV1` function returns the prometheus exporter metrics, currently displaying the total cactus node count, which currently refreshes to match the node count in the consortium, everytime `updateMetricNodeCount` method of the `PluginConsortiumManual` 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: 'consortium_manual_exporter'
metrics_path: api/v1/plugins/@hyperledger/cactus-plugin-consortium-manual/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-consortium-manual/src/test/typescript/unit/consortium/get-node-jws-endpoint-v1.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-fabric/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_consortium_manual_total_node_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-consortium-manual/package-lock.json

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

1 change: 1 addition & 0 deletions packages/cactus-plugin-consortium-manual/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"axios": "0.21.1",
"body-parser": "1.19.0",
"express": "4.17.1",
"prom-client": "13.0.0",
"jose": "1.27.2",
"json-stable-stringify": "1.0.1",
"typescript-optional": "2.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
"format": "The general format which is a JSON object, not a string."
}
}
},
"PrometheusExporterMetricsResponse": {
"type": "string",
"nullable": false
}
}
},
Expand Down Expand Up @@ -111,6 +115,31 @@
}
}
}
},
"/api/v1/plugins/@hyperledger/cactus-plugin-consortium-manual/get-prometheus-exporter-metrics": {
"get": {
"x-hyperledger-cactus": {
"http": {
"verbLowerCase": "get",
"path": "/api/v1/plugins/@hyperledger/cactus-plugin-consortium-manual/get-prometheus-exporter-metrics"
}
},
"operationId": "getPrometheusExporterMetricsV1",
"summary": "Get the Prometheus Metrics",
"parameters": [],
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/PrometheusExporterMetricsResponse"
}
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ import {
} from "@hyperledger/cactus-core";

import OAS from "../../json/openapi.json";
import { PluginConsortiumManual } from "../plugin-consortium-manual";

export interface IGetNodeJwsEndpointOptions {
plugin: PluginConsortiumManual;
keyPairPem: string;
consortiumRepo: ConsortiumRepository;
logLevel?: LogLevelDesc;
}

export class GetNodeJwsEndpoint implements IWebServiceEndpoint {
private readonly log: Logger;
private readonly plugin: PluginConsortiumManual;

constructor(public readonly options: IGetNodeJwsEndpointOptions) {
const fnTag = "GetNodeJwsEndpoint#constructor()";
Expand All @@ -43,6 +46,12 @@ export class GetNodeJwsEndpoint implements IWebServiceEndpoint {
throw new Error(`${fnTag} options.keyPairPem falsy.`);
}
Checks.truthy(options.consortiumRepo, `${fnTag} options.consortiumRepo`);
Checks.truthy(options.plugin, `${fnTag} options.plugin`);
Checks.truthy(
options.plugin instanceof PluginConsortiumManual,
`${fnTag} options.plugin instanceof PluginConsortiumManual`,
);
this.plugin = options.plugin;

const level = options.logLevel || "INFO";
const label = "get-node-jws-endpoint-v1";
Expand Down Expand Up @@ -95,6 +104,11 @@ export class GetNodeJwsEndpoint implements IWebServiceEndpoint {
const fnTag = "GetNodeJwsEndpoint#createJws()";
const { keyPairPem, consortiumRepo: repo } = this.options;
try {
// TODO: move this logic here entirely to the plugin itself. We already
// have an issue open for it on GH most likely, someone may already be
// working on this very thing actually so please do double check prior
// to diving in and working on it to avoid redundant effort.
this.plugin.updateMetricNodeCount();
const keyPair = JWK.asKey(keyPairPem);
const payloadObject = { consortiumDatabase: repo.consortiumDatabase };
const payloadJson = jsonStableStringify(payloadObject);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Express, Request, Response } from "express";

import {
Logger,
LoggerProvider,
LogLevelDesc,
Checks,
} from "@hyperledger/cactus-common";

import {
IWebServiceEndpoint,
IExpressRequestHandler,
} from "@hyperledger/cactus-core-api";

import OAS from "../../json/openapi.json";

import { registerWebServiceEndpoint } from "@hyperledger/cactus-core";

import { PluginConsortiumManual } from "../plugin-consortium-manual";

export interface IGetPrometheusExporterMetricsEndpointV1Options {
logLevel?: LogLevelDesc;
plugin: PluginConsortiumManual;
}

export class GetPrometheusExporterMetricsEndpointV1
implements IWebServiceEndpoint {
private readonly log: Logger;

constructor(
public readonly opts: IGetPrometheusExporterMetricsEndpointV1Options,
) {
const fnTag = "GetPrometheusExporterMetricsEndpointV1#constructor()";

Checks.truthy(opts, `${fnTag} options`);
Checks.truthy(opts.plugin, `${fnTag} options.plugin`);

this.log = LoggerProvider.getOrCreate({
label: "get-prometheus-exporter-metrics-v1",
level: opts.logLevel || "INFO",
});
}

public getExpressRequestHandler(): IExpressRequestHandler {
return this.handleRequest.bind(this);
}

public getPath(): string {
return OAS.paths[
"/api/v1/plugins/@hyperledger/cactus-plugin-consortium-manual/get-prometheus-exporter-metrics"
].get["x-hyperledger-cactus"].http.path;
}

public getVerbLowerCase(): string {
return OAS.paths[
"/api/v1/plugins/@hyperledger/cactus-plugin-consortium-manual/get-prometheus-exporter-metrics"
].get["x-hyperledger-cactus"].http.verbLowerCase;
}

public registerExpress(app: Express): IWebServiceEndpoint {
registerWebServiceEndpoint(app, this);
return this;
}

async handleRequest(req: Request, res: Response): Promise<void> {
const fnTag = "GetPrometheusExporterMetrics#handleRequest()";
const verbUpper = this.getVerbLowerCase().toUpperCase();
this.log.debug(`${verbUpper} ${this.getPath()}`);

try {
const resBody = await this.opts.plugin.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 });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,42 @@ export const DefaultApiAxiosParamCreator = function (configuration?: Configurati



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,
};
},
/**
*
* @summary Get the Prometheus Metrics
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
getPrometheusExporterMetricsV1: async (options: any = {}): Promise<RequestArgs> => {
const localVarPath = `/api/v1/plugins/@hyperledger/cactus-plugin-consortium-manual/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]);
Expand Down Expand Up @@ -203,6 +239,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<string>> {
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);
};
},
}
};

Expand Down Expand Up @@ -230,6 +279,15 @@ export const DefaultApiFactory = function (configuration?: Configuration, basePa
getNodeJws(options?: any): AxiosPromise<GetNodeJwsResponse> {
return DefaultApiFp(configuration).getNodeJws(options).then((request) => request(axios, basePath));
},
/**
*
* @summary Get the Prometheus Metrics
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
getPrometheusExporterMetricsV1(options?: any): AxiosPromise<string> {
return DefaultApiFp(configuration).getPrometheusExporterMetricsV1(options).then((request) => request(axios, basePath));
},
};
};

Expand Down Expand Up @@ -261,6 +319,17 @@ export class DefaultApi extends BaseAPI {
public getNodeJws(options?: any) {
return DefaultApiFp(this.configuration).getNodeJws(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));
}
}


Loading

0 comments on commit 853bdc4

Please sign in to comment.