Skip to content

Commit

Permalink
feat(besu): add prometheus exporter
Browse files Browse the repository at this point in the history
	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. IPluginLedgerConnectorFabricOptions interface in PluginLedgerConnectorFabric class now has a prometheusExporter optional field
	5. The PluginLedgerConnectorFabric 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#533

Signed-off-by: Jagpreet Singh Sasan <jagpreet.singh.sasan@accenture.com>
  • Loading branch information
jagpreetsinghsasan committed Feb 23, 2021
1 parent 48bb129 commit 77d3809
Show file tree
Hide file tree
Showing 14 changed files with 475 additions and 84 deletions.
39 changes: 35 additions & 4 deletions packages/cactus-plugin-ledger-connector-besu/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
# `@hyperledger/cactus-plugin-ledger-connector`

> 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 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}']
```
// 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-besu/src/test/typescript/integration/besu-v1-4-x/run-transaction-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-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.
21 changes: 21 additions & 0 deletions packages/cactus-plugin-ledger-connector-besu/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-besu/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-eea": "0.10.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,35 @@
"nullable": false
}
}
},
"PrometheusExporterOptions": {
"type": "object",
"required": [
"metricsPollingIntervalInMin"
],
"properties": {
"metricsPollingIntervalInMin": {
"description": "The polling interval for the prometheus exporter (in minutes)",
"type": "number",
"nullable": false
}
}
},
"PrometheusExporterMetricsRequest": {
"type": "object",
"required": [
"promExporter"
],
"properties": {
"promExporter":{
"description": "The prometheus exporter class object",
"$ref": "#/components/schemas/PrometheusExporterOptions"
}
}
},
"PrometheusExporterMetricsResponse": {
"type": "string",
"nullable": false
}
}
},
Expand Down Expand Up @@ -672,6 +701,40 @@
}
}
}
},
"/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": [],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/PrometheusExporterMetricsRequest"
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/PrometheusExporterMetricsResponse"
}
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.0.0
5.0.0-beta2
Loading

0 comments on commit 77d3809

Please sign in to comment.