|
1 | | -# prometheus_client |
2 | | -A Dart prometheus client library |
| 1 | +prometheus_client |
| 2 | +=== |
| 3 | + |
| 4 | +This is a simple Dart implementation of the [Prometheus][prometheus] client library, [similar to to libraries for other languages][writing_clientlibs]. |
| 5 | +It supports the default metric types like gauges, counters, or histograms. |
| 6 | +Metrics can be exported using the [text format][text_format]. |
| 7 | +To expose them in your server application the package comes with a [shelf][shelf] handler. |
| 8 | +In addition, it comes with some plug-in ready metrics for the Dart runtime and shelf. |
| 9 | + |
| 10 | +You can find the latest updates in the [changelog][changelog]. |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +A simple usage example: |
| 15 | + |
| 16 | +```dart |
| 17 | +import 'package:prometheus_client/prometheus_client.dart'; |
| 18 | +import 'package:prometheus_client/runtime_metrics.dart' as runtime_metrics; |
| 19 | +import 'package:prometheus_client/shelf_handler.dart'; |
| 20 | +import 'package:shelf/shelf.dart' as shelf; |
| 21 | +import 'package:shelf/shelf_io.dart' as io; |
| 22 | +import 'package:shelf_router/shelf_router.dart'; |
| 23 | +
|
| 24 | +main() async { |
| 25 | + // Register default runtime metrics |
| 26 | + runtime_metrics.register(); |
| 27 | + |
| 28 | + // Create a metric of type counter. |
| 29 | + // Always register your metric, either at the default registry or a custom one. |
| 30 | + final greetingCounter = |
| 31 | + Counter('greetings_total', 'The total amount of greetings')..register(); |
| 32 | + final app = Router(); |
| 33 | +
|
| 34 | + app.get('/hello', (shelf.Request request) { |
| 35 | + // Every time the hello is called, increase the counter by one |
| 36 | + greetingCounter.inc(); |
| 37 | + return shelf.Response.ok('hello-world'); |
| 38 | + }); |
| 39 | + |
| 40 | + // Register a handler to expose the metrics in the Prometheus text format |
| 41 | + app.get('/metrics', prometheusHandler()); |
| 42 | +
|
| 43 | + var handler = const shelf.Pipeline() |
| 44 | + .addHandler(app.handler); |
| 45 | + var server = await io.serve(handler, 'localhost', 8080); |
| 46 | +
|
| 47 | + print('Serving at http://${server.address.host}:${server.port}'); |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +Start the example application and access the exposed metrics at `http://localhost:8080/metrics`. |
| 52 | +For a full usage example, take a look at [`example/prometheus_client.example.dart`][example]. |
| 53 | + |
| 54 | +## Planned features |
| 55 | + |
| 56 | +To achieve the requirements from the Prometheus [Writing Client Libraries][writing_clientlibs] documentation, some features still have to be implemented: |
| 57 | + |
| 58 | +* Support `Summary` metric type. |
| 59 | +* Support timestamp in samples and text format. |
| 60 | +* Split out shelf support into own package to avoid dependencies on shelf. |
| 61 | + |
| 62 | + |
| 63 | +## Features and bugs |
| 64 | + |
| 65 | +Please file feature requests and bugs at the [issue tracker][tracker]. |
| 66 | + |
| 67 | +[tracker]: https://github.com/Fox32/prometheus_client/issues |
| 68 | +[writing_clientlibs]: https://prometheus.io/docs/instrumenting/writing_clientlibs/ |
| 69 | +[prometheus]: https://prometheus.io/ |
| 70 | +[text_format]: https://prometheus.io/docs/instrumenting/exposition_formats/#text-based-format |
| 71 | +[shelf]: https://pub.dev/packages/shelf |
| 72 | +[example]: ./example/prometheus_client_example.dart |
| 73 | +[changelog]: ./CHANGELOG.md |
0 commit comments