Skip to content

Commit

Permalink
feat(corda): prometheus exporter metrics integration
Browse files Browse the repository at this point in the history
        Primary Change
        ---
        1. The corda ledger connector plugin now includes the prometheus metrics exporter integration
        2. OpenAPI spec now has api endpoint for getting the prometheus metrics

        Refactorings that were also necessary to incorporate 1) and 2)
        ------
        3. GetPrometheusMetricsV1 class is created to handle the corresponding api endpoint
        4. IPluginLedgerConnectorCordaOptions interface in PluginLedgerConnectorCorda class now has a prometheusExporter object optional field
        5. The PluginLedgerConnectorCorda class has relevant functions to incorporate prometheus exporter
        6. Updated Readme.md about the prometheus exporter
        7. Updated the test case located at packages/cactus-plugin-ledger-connector-corda/src/test/typescript/integration/deploy-cordapp-jars-to-nodes.test.ts
        8. Updated the OpenAPI spec file to have run-transaction endpoint which currently returns NOT_IMPLEMENTED with 501 code.

Resolve #535

Signed-off-by: Jagpreet Singh Sasan <jagpreet.singh.sasan@accenture.com>
  • Loading branch information
jagpreetsinghsasan authored and petermetz committed Apr 28, 2021
1 parent ada532e commit 9f37755
Show file tree
Hide file tree
Showing 15 changed files with 551 additions and 2 deletions.
35 changes: 35 additions & 0 deletions packages/cactus-plugin-ledger-connector-corda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,41 @@

> TODO: description
### Usage Prometheus
The prometheus exporter object is initialized in the `PluginLedgerConnectorCorda` class constructor itself, so instantiating the object of the `PluginLedgerConnectorCorda` class, gives access to the exporter object.
You can also initialize the prometheus exporter object seperately and then pass it to the `IPluginLedgerConnectorCordaOptions` interface for `PluginLedgerConnectoCorda` constructor.

`getPrometheusExporterMetricsEndpointV1` function returns the prometheus exporter metrics, currently displaying the total transaction count, which currently increments everytime the `transact()` method of the `PluginLedgerConnectorCorda` 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: 'corda_ledger_connector_exporter'
metrics_path: api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-corda/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-corda/src/test/typescript/integration/deploy-cordapp-jars-to-nodes.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-corda/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_corda_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.

## Usage

Take a look at how the API client can be used to run transactions on a corda ledger:
Expand Down
21 changes: 21 additions & 0 deletions packages/cactus-plugin-ledger-connector-corda/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-ledger-connector-corda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"@hyperledger/cactus-core-api": "0.4.1",
"axios": "0.21.1",
"express-openapi-validator": "4.10.5",
"prom-client": "13.0.0",
"internal-ip": "6.2.0",
"joi": "14.3.1",
"node-ssh": "11.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ class ApiPluginLedgerConnectorCordaController(@Autowired(required = true) val se
}


@GetMapping(
value = ["/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-corda/get-prometheus-exporter-metrics"],
produces = ["text/plain"]
)
fun getPrometheusExporterMetricsV1(): ResponseEntity<kotlin.String> {
return ResponseEntity(service.getPrometheusExporterMetricsV1(), HttpStatus.valueOf(200))
}


@PostMapping(
value = ["/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-corda/invoke-contract"],
produces = ["application/json"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ interface ApiPluginLedgerConnectorCordaService {

fun diagnoseNodeV1(diagnoseNodeV1Request: DiagnoseNodeV1Request?): DiagnoseNodeV1Response

fun getPrometheusExporterMetricsV1(): kotlin.String

fun invokeContractV1(invokeContractV1Request: InvokeContractV1Request?): InvokeContractV1Response

fun listFlowsV1(listFlowsV1Request: ListFlowsV1Request?): ListFlowsV1Response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,10 @@
"$ref": "#/components/schemas/NodeDiagnosticInfo"
}
}
},
"PrometheusExporterMetricsResponse": {
"type": "string",
"nullable": false
}
}
},
Expand Down Expand Up @@ -904,6 +908,31 @@
}
}
}
},
"/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-corda/get-prometheus-exporter-metrics": {
"get": {
"x-hyperledger-cactus": {
"http": {
"verbLowerCase": "get",
"path": "/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-corda/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 @@ -765,6 +765,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<RequestArgs> => {
const localVarPath = `/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-corda/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,
};
},
/**
*
* @summary Invokes a contract on a Corda ledger (e.g. a flow)
Expand Down Expand Up @@ -922,6 +958,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);
};
},
/**
*
* @summary Invokes a contract on a Corda ledger (e.g. a flow)
Expand Down Expand Up @@ -990,6 +1039,15 @@ export const DefaultApiFactory = function (configuration?: Configuration, basePa
diagnoseNodeV1(diagnoseNodeV1Request?: DiagnoseNodeV1Request, options?: any): AxiosPromise<DiagnoseNodeV1Response> {
return DefaultApiFp(configuration).diagnoseNodeV1(diagnoseNodeV1Request, 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));
},
/**
*
* @summary Invokes a contract on a Corda ledger (e.g. a flow)
Expand Down Expand Up @@ -1051,6 +1109,17 @@ export class DefaultApi extends BaseAPI {
return DefaultApiFp(this.configuration).diagnoseNodeV1(diagnoseNodeV1Request, 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));
}

/**
*
* @summary Invokes a contract on a Corda ledger (e.g. a flow)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,23 @@ import {

import { DeployContractJarsEndpoint } from "./web-services/deploy-contract-jars-endpoint";

import {
IGetPrometheusExporterMetricsEndpointV1Options,
GetPrometheusExporterMetricsEndpointV1,
} from "./web-services/get-prometheus-exporter-metrics-endpoint-v1";

import { PrometheusExporter } from "./prometheus-exporter/prometheus-exporter";
import {
IInvokeContractEndpointV1Options,
InvokeContractEndpointV1,
} from "./web-services/invoke-contract-endpoint-v1";

export interface IPluginLedgerConnectorCordaOptions
extends ICactusPluginOptions {
logLevel?: LogLevelDesc;
sshConfigAdminShell: SshConfig;
corDappsDir: string;
prometheusExporter?: PrometheusExporter;
cordaStartCmd?: string;
cordaStopCmd?: string;
}
Expand All @@ -38,6 +50,7 @@ export class PluginLedgerConnectorCorda

private readonly instanceId: string;
private readonly log: Logger;
public prometheusExporter: PrometheusExporter;

private endpoints: IWebServiceEndpoint[] | undefined;

Expand All @@ -58,6 +71,23 @@ export class PluginLedgerConnectorCorda
const label = "plugin-ledger-connector-corda";
this.log = LoggerProvider.getOrCreate({ level, label });
this.instanceId = this.options.instanceId;
this.prometheusExporter =
options.prometheusExporter ||
new PrometheusExporter({ pollingIntervalInMin: 1 });
Checks.truthy(
this.prometheusExporter,
`${fnTag} options.prometheusExporter`,
);
}

public getPrometheusExporter(): PrometheusExporter {
return this.prometheusExporter;
}

public async getPrometheusExporterMetrics(): Promise<string> {
const res: string = await this.prometheusExporter.getPrometheusMetrics();
this.log.debug(`getPrometheusExporterMetrics() response: %o`, res);
return res;
}

public async getConsensusAlgorithmFamily(): Promise<
Expand All @@ -83,7 +113,8 @@ export class PluginLedgerConnectorCorda
}

public async transact(): Promise<any> {
throw new Error("Method not implemented.");
this.prometheusExporter.addCurrentTransaction();
return null as any;
}

async registerWebServices(app: Express): Promise<IWebServiceEndpoint[]> {
Expand Down Expand Up @@ -111,6 +142,24 @@ export class PluginLedgerConnectorCorda

endpoints.push(endpoint);
}

{
const opts: IInvokeContractEndpointV1Options = {
connector: this,
logLevel: this.options.logLevel,
};
const endpoint = new InvokeContractEndpointV1(opts);
endpoints.push(endpoint);
}

{
const opts: IGetPrometheusExporterMetricsEndpointV1Options = {
connector: this,
logLevel: this.options.logLevel,
};
const endpoint = new GetPrometheusExporterMetricsEndpointV1(opts);
endpoints.push(endpoint);
}
this.log.info(`Instantiated endpoints of ${pkgName}`);
return endpoints;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Transactions } from "./response.type";

import { totalTxCount, K_CACTUS_CORDA_TOTAL_TX_COUNT } from "./metrics";

export async function collectMetrics(transactions: Transactions) {
totalTxCount.labels(K_CACTUS_CORDA_TOTAL_TX_COUNT).set(transactions.counter);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Gauge } from "prom-client";

export const K_CACTUS_CORDA_TOTAL_TX_COUNT = "cactus_corda_total_tx_count";

export const totalTxCount = new Gauge({
name: K_CACTUS_CORDA_TOTAL_TX_COUNT,
help: "Total transactions executed",
labelNames: ["type"],
});
Loading

0 comments on commit 9f37755

Please sign in to comment.