-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fabric): add prometheus exporter
Primary Change -------------- 1. The fabric 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. The generate-sdk command now has DateTime mapping to Date (to read Date as Date object instead of string) 4. GetPrometheusMetricsV1 class is created to handle the corresponding api endpoint 5. IPluginLedgerConnectorFabricOptions interface in PluginLedgerConnectorFabric class now has a prometheusExporter optional field 6. The PluginLedgerConnectorFabric class has relevant functions and codes to incorporate prometheus exporter 7. run-transaction-endpoint-v1.test is changed to incorporate the prometheus exporter for both fabric 1.4.x and 2.x versions 8. Added Readme.md on the prometheus exporter usage Fixes #531 Signed-off-by: Jagpreet Singh Sasan <jagpreet.singh.sasan@accenture.com>
- Loading branch information
1 parent
075ad90
commit b892655
Showing
15 changed files
with
393 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,41 @@ | ||
# `@hyperledger/cactus-plugin-ledger-connector-fabric` | ||
|
||
> 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 Fabric connector plugin. | ||
|
||
|
||
### Usage | ||
The prometheus exporter object is initialized in the `PluginLedgerConnectorFabric` class constructor itself, so instantiating the object of the `PluginLedgerConnectorFabric` class, gives access to the exporter object. | ||
You can also initialize the prometheus exporter object seperately and then pass it to the `IPluginLedgerConnectorFabricOptions` interface for `PluginLedgerConnectoFabric` constructor. | ||
|
||
`getPrometheusExporterMetricsEndpointV1` function returns the prometheus exporter metrics, currently displaying the total transaction count, which currently increments everytime the `transact()` method of the `PluginLedgerConnectorFabric` 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: 'fabric_ledger_connector_exporter' | ||
metrics_path: api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-fabric/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-fabric/src/test/typescript/integration/fabric-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-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_fabric_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. |
51 changes: 46 additions & 5 deletions
51
packages/cactus-plugin-ledger-connector-fabric/package-lock.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ | |
|
||
git_push.sh | ||
.npmignore | ||
.gitignore |
1 change: 0 additions & 1 deletion
1
...or-fabric/src/main/typescript/generated/openapi/typescript-axios/.openapi-generator/FILES
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
.gitignore | ||
api.ts | ||
base.ts | ||
configuration.ts | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...typescript/get-prometheus-exporter-metrics/get-prometheus-exporter-metrics-endpoint-v1.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { PluginLedgerConnectorFabric } from "../plugin-ledger-connector-fabric"; | ||
|
||
export interface IGetPrometheusExporterMetricsEndpointV1Options { | ||
logLevel?: LogLevelDesc; | ||
connector: PluginLedgerConnectorFabric; | ||
} | ||
|
||
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.connector, `${fnTag} options.connector`); | ||
|
||
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-ledger-connector-fabric/get-prometheus-exporter-metrics" | ||
].get["x-hyperledger-cactus"].http.path; | ||
} | ||
|
||
public getVerbLowerCase(): string { | ||
return OAS.paths[ | ||
"/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-fabric/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.connector.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 }); | ||
} | ||
} | ||
} |
Oops, something went wrong.