prometheus_client_shelf 0.4.0+3

  • Readme
  • Changelog
  • Example
  • Installing
  • 70

prometheus_client_shelf #

This package exposes Prometheus metrics for shelf using the package prometheus_client. To expose them in your server application the package comes with a shelf handler. In addition, it comes with some plug-in ready metrics for the shelf.

You can find the latest updates in the changelog.

Usage #

A simple usage example:

import 'package:prometheus_client/prometheus_client.dart';
import 'package:prometheus_client/runtime_metrics.dart' as runtime_metrics;
import 'package:prometheus_client_shelf/shelf_metrics.dart' as shelf_metrics;
import 'package:prometheus_client_shelf/shelf_handler.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_router/shelf_router.dart';

main() async {
  // Register default runtime metrics
  runtime_metrics.register();
  
  // Create a metric of type counter. 
  // Always register your metric, either at the default registry or a custom one.
  final greetingCounter =
      Counter('greetings_total', 'The total amount of greetings')..register();
  final app = Router();

  app.get('/hello', (shelf.Request request) {
    // Every time the hello is called, increase the counter by one 
    greetingCounter.inc();
    return shelf.Response.ok('hello-world');
  });
  
  // Register a handler to expose the metrics in the Prometheus text format
  app.get('/metrics', prometheusHandler());

  var handler = const shelf.Pipeline()
      // Register a middleware to track request times
      .addMiddleware(shelf_metrics.register())
      .addHandler(app.handler);
  var server = await io.serve(handler, 'localhost', 8080);

  print('Serving at http://${server.address.host}:${server.port}');
}

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

Features and bugs #

Please file feature requests and bugs at the issue tracker.

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 #

example/prometheus_client_shelf_example.dart

import 'dart:math';

import 'package:prometheus_client/prometheus_client.dart';
import 'package:prometheus_client/runtime_metrics.dart' as runtime_metrics;
import 'package:prometheus_client_shelf/shelf_metrics.dart' as shelf_metrics;
import 'package:prometheus_client_shelf/shelf_handler.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_router/shelf_router.dart';

void main() async {
  runtime_metrics.register();

  // Create a labeled gauge metric that stores the last time an endpoint was
  // accessed. Always register your metric, either at the default registry or a
  // custom one.
  final timeGauge = Gauge(
      'last_accessed_time', 'The last time the hello endpoint was accessed',
      labelNames: ['endpoint'])
    ..register();
  // Create a gauge metric without labels to store the last rolled value
  final rollGauge = Gauge('roll_value', 'The last roll value')..register();
  // Create a metric of type counter
  final greetingCounter =
      Counter('greetings_total', 'The total amount of greetings')..register();

  final app = Router();

  app.get('/hello', (shelf.Request request) {
    // Set the current time to the time metric for the label 'hello'
    timeGauge..labels(['hello']).setToCurrentTime();
    // Every time the hello is called, increase the counter by one
    greetingCounter.inc();
    return shelf.Response.ok('hello-world');
  });

  app.get('/roll', (shelf.Request request) {
    timeGauge..labels(['roll']).setToCurrentTime();
    final value = Random().nextDouble();
    // Store the rolled value without labels
    rollGauge.value = value;
    return shelf.Response.ok('rolled $value');
  });

  // Register a handler to expose the metrics in the Prometheus text format
  app.get('/metrics', prometheusHandler());

  app.all('/<ignored|.*>', (shelf.Request request) {
    return shelf.Response.notFound('Not Found');
  });

  var handler = const shelf.Pipeline()
      // Register a middleware to track request times
      .addMiddleware(shelf_metrics.register())
      .addMiddleware(shelf.logRequests())
      .addHandler(app.handler);
  var server = await io.serve(handler, 'localhost', 8080);

  print('Serving at http://${server.address.host}:${server.port}');
}

Use this package as a library

1. Depend on it

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


dependencies:
  prometheus_client_shelf: ^0.4.0+3

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_shelf/shelf_handler.dart';
import 'package:prometheus_client_shelf/shelf_metrics.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
40
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]
70
Learn more about scoring.

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

  • Dart: 2.7.0
  • pana: 0.13.4

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.5.0 <3.0.0
collection ^1.14.11 1.14.12
prometheus_client ^0.4.0+1 0.4.0+3
shelf ^0.7.5 0.7.5
Transitive dependencies
async 2.4.0
charcode 1.1.2
http_parser 3.1.3
meta 1.1.8
path 1.6.4
source_span 1.6.0
stack_trace 1.9.3
stream_channel 2.0.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
pedantic ^1.8.0
shelf_router ^0.7.1
test ^1.6.0