Skip to content

Commit

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

	1. The api-server 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. IApiServerConstructorOptions interface in ApiServer class now has a prometheusExporter optional field
	5. The ApiServer class has relevant functions and codes to incorporate prometheus exporter
	6. runtime-plugin-imports.test.ts is changed to incorporate the prometheus exporter
	7. Added Readme.md on the prometheus exporter usage
	8. Fixed Readme.md under keychain-memory and consortium-manual plugins

Resolve hyperledger-cacti#539

Signed-off-by: Jagpreet Singh Sasan <jagpreet.singh.sasan@accenture.com>
  • Loading branch information
jagpreetsinghsasan committed Mar 30, 2021
1 parent 853bdc4 commit d329213
Show file tree
Hide file tree
Showing 13 changed files with 659 additions and 4 deletions.
45 changes: 45 additions & 0 deletions packages/cactus-cmd-api-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
- [Deployment Scenarios](#deployment-scenarios)
- [Production Deployment Example](#production-deployment-example)
- [Low Resource Deployment Example](#low-resource-deployment-example)
- [Prometheus Exporter](#prometheus-exporter)
- [Usage Prometheus](#usage-prometheus)
- [Prometheus Integration](#prometheus-integration)
- [Helper Code](#helper-code)
- [FAQ](#faq)
- [What is the difference between a Cactus Node and a Cactus API Server?](#what-is-the-difference-between-a-cactus-node-and-a-cactus-api-server)
- [Is the API server horizontally scalable?](#is-the-api-server-horizontally-scalable)
Expand Down Expand Up @@ -196,6 +200,47 @@ of the machine they are hosted on:

![deployment-low-resource-example.png](https://github.com/hyperledger/cactus/raw/4a337be719a9d2e2ccb877edccd7849f4be477ec/whitepaper/deployment-low-resource-example.png)

## 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 `ApiServer` class constructor itself, so instantiating the object of the `ApiServer` class, gives access to the exporter object.
You can also initialize the prometheus exporter object seperately and then pass it to the `IApiServerConstructorOptions` interface for `ApiServer` constructor.

`getPrometheusExporterMetricsV1` function returns the prometheus exporter metrics, currently displaying the total plugins imported, which currently refreshes to match the plugin count, everytime `setTotalPluginImports` method 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/api-server/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/api-server/get-prometheus-exporter-metrics/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_api_server_total_plugin_imports** 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.





## FAQ

Expand Down
Loading

0 comments on commit d329213

Please sign in to comment.