adonis5-prometheus
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -0,3 +1,16 @@ | ||
/// <reference types="@adonisjs/application/build/adonis-typings" /> | ||
/// <reference types="@adonisjs/http-server/build/adonis-typings" /> | ||
declare module '@ioc:Adonis/Prometheus' { | ||
export * from 'prom-client'; | ||
} | ||
declare module '@ioc:Adonis/Prometheus/Middlewares/CollectPerformanceMetrics' { | ||
import { ApplicationContract } from '@ioc:Adonis/Core/Application'; | ||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'; | ||
export interface MiddlewareContract { | ||
new (application: ApplicationContract): { | ||
handle(ctx: HttpContextContract, next: () => Promise<void>): any; | ||
}; | ||
} | ||
const CollectPerformanceMetricsMiddleware: MiddlewareContract; | ||
export default CollectPerformanceMetricsMiddleware; | ||
} |
/// <reference types="@adonisjs/application/build/adonis-typings" /> | ||
import { ApplicationContract } from '@ioc:Adonis/Core/Application'; | ||
import { ApplicationContract, IocContract } from '@ioc:Adonis/Core/Application'; | ||
export default class PrometheusProvider { | ||
protected app: ApplicationContract; | ||
static needsApplication: boolean; | ||
protected container: IocContract; | ||
constructor(app: ApplicationContract); | ||
register(): void; | ||
private exposeMetrics; | ||
boot(): Promise<void>; | ||
ready(): Promise<void>; | ||
shutdown(): Promise<void>; | ||
} |
@@ -26,2 +26,3 @@ "use strict"; | ||
this.app = app; | ||
this.container = app.container; | ||
} | ||
@@ -39,2 +40,13 @@ register() { | ||
this.app.container.singleton('Adonis/Prometheus', () => prometheus); | ||
this.app.container.singleton('Adonis/Prometheus/Middlewares/CollectPerformanceMetrics', () => { | ||
const { CollectPerformanceMetrics } = require('../src/CollectPerformanceMetrics'); | ||
const { Metrics } = require('../src/Metrics'); | ||
const config = this.container.use('Adonis/Core/Config').get('prometheus'); | ||
const metrics = new Metrics(config); | ||
const enableUptimeMetric = config.uptimeMetric.enabled; | ||
if (enableUptimeMetric) { | ||
metrics.uptimeMetric.inc(1); | ||
} | ||
return new CollectPerformanceMetrics(metrics, config); | ||
}); | ||
} | ||
@@ -49,13 +61,4 @@ exposeMetrics(urlPath = '/metrics') { | ||
} | ||
async boot() { | ||
// All bindings are ready, feel free to use them | ||
} | ||
async ready() { | ||
// App is ready | ||
} | ||
async shutdown() { | ||
// Cleanup, since app is going down | ||
} | ||
} | ||
exports.default = PrometheusProvider; | ||
PrometheusProvider.needsApplication = true; |
@@ -1,16 +0,8 @@ | ||
import Prometheus from '@ioc:Adonis/Prometheus'; | ||
declare const _default: { | ||
/** | ||
* Total time each HTTP request takes. | ||
*/ | ||
httpMetric: Prometheus.Histogram<string>; | ||
/** | ||
* Uptime performance of the application. | ||
*/ | ||
uptimeMetric: Prometheus.Gauge<string>; | ||
/** | ||
* No. of request handled. | ||
*/ | ||
throughputMetric: Prometheus.Counter<string>; | ||
}; | ||
export default _default; | ||
import * as prometheus from 'prom-client'; | ||
export declare class Metrics { | ||
protected config: any; | ||
httpMetric: prometheus.Histogram<string>; | ||
uptimeMetric: prometheus.Gauge<string>; | ||
throughputMetric: prometheus.Counter<string>; | ||
constructor(config: any); | ||
} |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Prometheus_1 = __importDefault(require("@ioc:Adonis/Prometheus")); | ||
const Config_1 = __importDefault(require("@ioc:Adonis/Core/Config")); | ||
exports.default = { | ||
/** | ||
* Total time each HTTP request takes. | ||
*/ | ||
httpMetric: new Prometheus_1.default.Histogram(Config_1.default.get('prometheus.httpMetric')), | ||
/** | ||
* Uptime performance of the application. | ||
*/ | ||
uptimeMetric: new Prometheus_1.default.Gauge(Config_1.default.get('prometheus.uptimeMetric')), | ||
/** | ||
* No. of request handled. | ||
*/ | ||
throughputMetric: new Prometheus_1.default.Counter(Config_1.default.get('prometheus.throughputMetric')), | ||
}; | ||
exports.Metrics = void 0; | ||
const prometheus = __importStar(require("prom-client")); | ||
class Metrics { | ||
constructor(config) { | ||
this.config = config; | ||
/** | ||
* Total time each HTTP request takes. | ||
*/ | ||
this.httpMetric = new prometheus.Histogram(config.httpMetric); | ||
/** | ||
* Uptime performance of the application. | ||
*/ | ||
this.uptimeMetric = new prometheus.Gauge(config.uptimeMetric); | ||
/** | ||
* No. of request handled. | ||
*/ | ||
this.throughputMetric = new prometheus.Counter(config.throughputMetric); | ||
} | ||
} | ||
exports.Metrics = Metrics; |
{ | ||
"name": "adonis5-prometheus", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Prometheus wrapper for Adonis 5", | ||
@@ -109,3 +109,3 @@ "main": "build/providers/PrometheusProvider.js", | ||
}, | ||
"readme": "<div align=\"center\">\n <img src=\"https://i.imgur.com/QZf8jrj.png\" width=\"300px\" /> \n <br/>\n <h3>Adonis5-Prometheus</h3>\n <p>Simple Prometheus Wrapper for Adonis</p>\n</div>\n\n## Installation\n```\nnpm i adonis5-prometheus\nnode ace configure adonis5-prometheus\n```\n\n## Usage\n\nA configuration file has been added in `config/prometheus.ts`.\n\nBy default the system metrics are collected ( `systemMetrics.enabled: true` ), so now you can call the endpoint {{host}}/metrics to get the measured metrics.\n\n[npm-image]: https://img.shields.io/npm/v/adonis5-prometheus.svg?style=for-the-badge&logo=npm\n[npm-url]: https://npmjs.org/package/adonis5-prometheus \"npm\"\n\n[license-image]: https://img.shields.io/npm/l/adonis5-prometheus?color=blueviolet&style=for-the-badge\n[license-url]: LICENSE.md \"license\"\n\n[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript\n[typescript-url]: \"typescript\"\n" | ||
"readme": "<div align=\"center\">\r\n <img src=\"https://i.imgur.com/QZf8jrj.png\" width=\"300px\" /> \r\n <br/>\r\n <h3>Adonis5-Prometheus</h3>\r\n <p>Simple Prometheus Wrapper for Adonis</p>\r\n <img src=\"https://img.shields.io/npm/v/adonis5-prometheus.svg?style=for-the-badge&logo=npm\" />\r\n <img src=\"https://img.shields.io/npm/l/adonis5-prometheus?color=blueviolet&style=for-the-badge\" />\r\n <img src=\"https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript\" />\r\n</div>\r\n\r\n## Installation\r\n```\r\nnpm i adonis5-prometheus\r\nnode ace configure adonis5-prometheus\r\n```\r\n\r\n## Usage\r\n\r\nA configuration file has been added in `config/prometheus.ts`.\r\n\r\nBy default the system metrics are collected ( `systemMetrics.enabled: true` ), so now you can call the endpoint `{{host}}/metrics` to get the measured metrics.\r\n\r\n## Built-in Metrics\r\nThere currently exists built-ins metrics such as:\r\n- HTTP Metric: Total time each HTTP request takes.\r\n- Uptime Metric: Uptime performance of the application.\r\n- Throughput metric: No. of request handled.\r\n\r\nTo enable them, simply register the `CollectPerformanceMetrics` as the first item in the start/kernel.ts:\r\n\r\n```typescript\r\nServer.middleware.register([\r\n // Make it first in the list for reliable metrics.\r\n () => import('@ioc:Adonis/Prometheus/Middlewares/CollectPerformanceMetrics'),\r\n () => import('@ioc:Adonis/Core/BodyParser'),\r\n ...\r\n])\r\n```\r\n\r\n## Custom Metrics\r\n```typescript\r\n// Register your custom metrics in the separate file you want.\r\nexport const OrderMetric = new Prometheus.Counter({\r\n name: 'sent_orders',\r\n help: 'Total Orders Sent',\r\n})\r\n\r\n// OrderController.ts\r\nimport { OrderMetric } from 'App/Metrics'\r\n\r\nexport default class OrderController {\r\n public async store({ request }: HttpContextContract) {\r\n const order = await request.validate({ schema: OrderSchema })\r\n\r\n // ...\r\n OrderMetric.inc()\r\n // ...\r\n }\r\n}\r\n```\r\nWhen hitting `{{host}}/metrics` you will now get the following:\r\n```\r\n# HELP send_orders Total Orders Sent\r\n# TYPE send_orders counter\r\nsent_orders 2\r\n```\r\n\r\n## Documentation\r\nThis library is a wrapper for prom-client. The prom-client object can be imported with `import Prometheus from '@ioc:Adonis/Prometheus`. Check out the [documentation](https://github.com/siimon/prom-client) for more information.\r\n\r\n## Acknowledgments\r\n- [tnkemdilim/adonis-prometheus](https://github.com/tnkemdilim/adonis-prometheus) - At first, I just adapted his library to support Adonis5.\r\n" | ||
} |
@@ -6,2 +6,5 @@ <div align="center"> | ||
<p>Simple Prometheus Wrapper for Adonis</p> | ||
<img src="https://img.shields.io/npm/v/adonis5-prometheus.svg?style=for-the-badge&logo=npm" /> | ||
<img src="https://img.shields.io/npm/l/adonis5-prometheus?color=blueviolet&style=for-the-badge" /> | ||
<img src="https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript" /> | ||
</div> | ||
@@ -19,11 +22,53 @@ | ||
By default the system metrics are collected ( `systemMetrics.enabled: true` ), so now you can call the endpoint {{host}}/metrics to get the measured metrics. | ||
By default the system metrics are collected ( `systemMetrics.enabled: true` ), so now you can call the endpoint `{{host}}/metrics` to get the measured metrics. | ||
[npm-image]: https://img.shields.io/npm/v/adonis5-prometheus.svg?style=for-the-badge&logo=npm | ||
[npm-url]: https://npmjs.org/package/adonis5-prometheus "npm" | ||
## Built-in Metrics | ||
There currently exists built-ins metrics such as: | ||
- HTTP Metric: Total time each HTTP request takes. | ||
- Uptime Metric: Uptime performance of the application. | ||
- Throughput metric: No. of request handled. | ||
[license-image]: https://img.shields.io/npm/l/adonis5-prometheus?color=blueviolet&style=for-the-badge | ||
[license-url]: LICENSE.md "license" | ||
To enable them, simply register the `CollectPerformanceMetrics` as the first item in the start/kernel.ts: | ||
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript | ||
[typescript-url]: "typescript" | ||
```typescript | ||
Server.middleware.register([ | ||
// Make it first in the list for reliable metrics. | ||
() => import('@ioc:Adonis/Prometheus/Middlewares/CollectPerformanceMetrics'), | ||
() => import('@ioc:Adonis/Core/BodyParser'), | ||
... | ||
]) | ||
``` | ||
## Custom Metrics | ||
```typescript | ||
// Register your custom metrics in the separate file you want. | ||
export const OrderMetric = new Prometheus.Counter({ | ||
name: 'sent_orders', | ||
help: 'Total Orders Sent', | ||
}) | ||
// OrderController.ts | ||
import { OrderMetric } from 'App/Metrics' | ||
export default class OrderController { | ||
public async store({ request }: HttpContextContract) { | ||
const order = await request.validate({ schema: OrderSchema }) | ||
// ... | ||
OrderMetric.inc() | ||
// ... | ||
} | ||
} | ||
``` | ||
When hitting `{{host}}/metrics` you will now get the following: | ||
``` | ||
# HELP send_orders Total Orders Sent | ||
# TYPE send_orders counter | ||
sent_orders 2 | ||
``` | ||
## Documentation | ||
This library is a wrapper for prom-client. The prom-client object can be imported with `import Prometheus from '@ioc:Adonis/Prometheus`. Check out the [documentation](https://github.com/siimon/prom-client) for more information. | ||
## Acknowledgments | ||
- [tnkemdilim/adonis-prometheus](https://github.com/tnkemdilim/adonis-prometheus) - At first, I just adapted his library to support Adonis5. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19389
16
202
73