prometheus_client 0.4.0+4

  • Readme
  • Changelog
  • Example
  • Installing
  • 76

prometheus_client #

This is a simple Dart implementation of the Prometheus client library, similar to to libraries for other languages. It supports the default metric types like gauges, counters, summaries, or histograms. Metrics can be exported using the text format. To expose them in your server application the package comes with the package prometheus_client_shelf to integrate metrics with a shelf handler. In addition, it comes with some plug-in ready metrics for the Dart runtime.

You can find the latest updates in the changelog.

Usage #

A simple usage example:

import 'dart:io';

import 'package:prometheus_client/format.dart' as format;
import 'package:prometheus_client/prometheus_client.dart';
import 'package:prometheus_client/runtime_metrics.dart' as runtime_metrics;

main() async {
  // Register runtime metrics with the default metrics registry
  runtime_metrics.register();

  // Create a Histogram metrics without labels. Always register your metric,
  // either at the default registry or a custom one.
  final durationHistogram = Histogram(
    'http_request_duration_seconds',
    'The duration of http requests in seconds.',
  )..register();

  // Create a metric of type counter, with a label for the requested path:
  final metricRequestsCounter = Counter(
      'metric_requests_total', 'The total amount of requests of the metrics.',
      labelNames: ['path'])
    ..register();

  // Create a http server
  final server = await HttpServer.bind(
    InternetAddress.loopbackIPv4,
    8080,
  );
  print('Listening on localhost:${server.port}');

  await for (HttpRequest request in server) {
    // Measure the request duration
    await durationHistogram.observeDuration(Future(() async {
      // Count calls to the metric endpoint by path.
      metricRequestsCounter.labels([request.uri.path]).inc();

      // Output metrics in the text representation
      format.write004(request.response,
          CollectorRegistry.defaultRegistry.collectMetricFamilySamples());
      await request.response.close();
    }));
  }
}

Start the example application and access the exposed metrics at http://localhost:8080/. For a full usage example, take a look at example/prometheus_client_example.dart.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

0.4.0+4 #

0.4.0+3 #

  • Remove author from pubspec.

0.4.0+2 #

  • Fix some analyzer issues, no functional changes.

0.4.0+1 #

  • Align version constraint to prometheus_client_shelf.

0.4.0 #

0.3.0+1 #

  • Increase version constraint range on package collection to ^1.14.11 to be compatible with flutter.

0.3.0 #

  • Implement Summary metric type.

0.2.0 #

  • Support timestamp for samples.

0.1.0 #

  • Initial version.
  • Implements Counter, Gauge and Histogram.
  • Includes a shelf handler to export metrics and a shelf middleware to measure performance.

example/prometheus_client_example.dart

import 'dart:io';

import 'package:prometheus_client/format.dart' as format;
import 'package:prometheus_client/prometheus_client.dart';
import 'package:prometheus_client/runtime_metrics.dart' as runtime_metrics;

main() async {
  // Register runtime metrics with the default metrics registry
  runtime_metrics.register();

  // Create a Histogram metrics without labels. Always register your metric,
  // either at the default registry or a custom one.
  final durationHistogram = Histogram(
    'http_request_duration_seconds',
    'The duration of http requests in seconds.',
  )..register();

  // Create a metric of type counter, with a label for the requested path:
  final metricRequestsCounter = Counter(
      'metric_requests_total', 'The total amount of requests of the metrics.',
      labelNames: ['path'])
    ..register();

  // Create a http server
  final server = await HttpServer.bind(
    InternetAddress.loopbackIPv4,
    8080,
  );
  print('Listening on localhost:${server.port}');

  await for (HttpRequest request in server) {
    // Measure the request duration
    await durationHistogram.observeDuration(Future(() async {
      // Count calls to the metric endpoint by path.
      metricRequestsCounter.labels([request.uri.path]).inc();

      // Output metrics in the text representation
      format.write004(request.response,
          CollectorRegistry.defaultRegistry.collectMetricFamilySamples());
      await request.response.close();
    }));
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  prometheus_client: ^0.4.0+4

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:prometheus_client/prometheus_client.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
52
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
76
Learn more about scoring.

We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.5.0 <3.0.0
collection ^1.14.11 1.14.13 1.15.0-nnbd
Dev dependencies
pedantic ^1.8.0
test ^1.6.0