• Stars
    star
    155
  • Rank 240,864 (Top 5 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A prometheus exporter for Laravel

laravel-prometheus-exporter

A prometheus exporter for Laravel.

Author Build Status StyleCI Software License Packagist Version Total Downloads

This package is a wrapper bridging jimdo/prometheus_client_php into Laravel.

Installation

composer require superbalist/laravel-prometheus-exporter

Register the service provider in app.php

'providers' => [
    // ...
    Superbalist\LaravelPrometheusExporter\PrometheusServiceProvider::class,
]

Register the facade in app.php

'aliases' => [
    // ...
    'Prometheus' => Superbalist\LaravelPrometheusExporter\PrometheusFacade::class,
]

Configuration

The package has a default configuration which uses the following environment variables.

PROMETHEUS_NAMESPACE=app

PROMETHEUS_METRICS_ROUTE_ENABLED=true
PROMETHEUS_METRICS_ROUTE_PATH=metrics
PROMETHEUS_METRICS_ROUTE_MIDDLEWARE=null

PROMETHEUS_STORAGE_ADAPTER=memory

REDIS_HOST=localhost
REDIS_PORT=6379
PROMETHEUS_REDIS_PREFIX=PROMETHEUS_

To customize the configuration file, publish the package configuration using Artisan.

php artisan vendor:publish --provider="Superbalist\LaravelPrometheusExporter\PrometheusServiceProvider"

You can then edit the generated config at app/config/prometheus.php.

Storage Adapters

The storage adapter is used to persist metrics across requests. The memory adapter is enabled by default, meaning data will only be persisted across the current request. We recommend using the redis or apc adapter in production environments.

The PROMETHEUS_STORAGE_ADAPTER env var is used to specify the storage adapter.

If redis is used, the REDIS_HOST and REDIS_PORT vars also need to be configured.

Exporting Metrics

The package adds a /metrics end-point, enabled by default, which exposes all metrics gathered by collectors.

This can be turned on/off using the PROMETHEUS_METRICS_ROUTE_ENABLED var, and can also be changed using the PROMETHEUS_METRICS_ROUTE_PATH var.

If you would like to protect this end-point, you can write any custom middleware and enable it using PROMETHEUS_METRICS_ROUTE_MIDDLEWARE.

Collectors

A collector is a class, implementing the CollectorInterface, which is responsible for collecting data for one or many metrics.

Please see the ExampleCollector included in this repository.

You can auto-load your collectors by adding them to the collectors array in the prometheus.php config.

Usage

// retrieve the exporter
$exporter = app(\Superbalist\LaravelPrometheusExporter::class);
// or
$exporter = app('prometheus');
// or
$exporter = Prometheus::getFacadeRoot();

// register a new collector
$collector = new \My\New\Collector();
$exporter->registerCollector($collector);

// retrieve all collectors
var_dump($exporter->getCollectors());

// retrieve a collector by name
$collector = $exporter->getCollector('user');

// export all metrics
// this is called automatically when the /metrics end-point is hit
var_dump($exporter->export());

// the following methods can be used to create and interact with counters, gauges and histograms directly
// these methods will typically be called by collectors, but can be used to register any custom metrics directly,
// without the need of a collector

// create a counter
$counter = $exporter->registerCounter('search_requests_total', 'The total number of search requests.');
$counter->inc(); // increment by 1
$counter->incBy(2);

// create a counter (with labels)
$counter = $exporter->registerCounter('search_requests_total', 'The total number of search requests.', ['request_type']);
$counter->inc(['GET']); // increment by 1
$counter->incBy(2, ['GET']);

// retrieve a counter
$counter = $exporter->getCounter('search_requests_total');

// create a gauge
$gauge = $exporter->registerGauge('users_online_total', 'The total number of users online.');
$gauge->inc(); // increment by 1
$gauge->incBy(2);
$gauge->dec(); // decrement by 1
$gauge->decBy(2);
$gauge->set(36);

// create a gauge (with labels)
$gauge = $exporter->registerGauge('users_online_total', 'The total number of users online.', ['group']);
$gauge->inc(['staff']); // increment by 1
$gauge->incBy(2, ['staff']);
$gauge->dec(['staff']); // decrement by 1
$gauge->decBy(2, ['staff']);
$gauge->set(36, ['staff']);

// retrieve a gauge
$counter = $exporter->getGauge('users_online_total');

// create a histogram
$histogram = $exporter->registerHistogram(
    'response_time_seconds',
    'The response time of a request.',
    [],
    [0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0]
);
// the buckets must be in asc order
// if buckets aren't specified, the default 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0 buckets will be used
$histogram->observe(5.0);

// create a histogram (with labels)
$histogram = $exporter->registerHistogram(
    'response_time_seconds',
    'The response time of a request.',
    ['request_type'],
    [0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0]
);
// the buckets must be in asc order
// if buckets aren't specified, the default 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0 buckets will be used
$histogram->observe(5.0, ['GET']);

// retrieve a histogram
$counter = $exporter->getHistogram('response_time_seconds');

More Repositories

1

laravel-google-cloud-storage

A Google Cloud Storage filesystem for Laravel
PHP
508
star
2

flysystem-google-cloud-storage

Flysystem Adapter for Google Cloud Storage
PHP
266
star
3

laravel-pubsub

A Pub-Sub abstraction for Laravel
PHP
68
star
4

php-pubsub

A PHP abstraction for the pub-sub pattern
PHP
45
star
5

php-pubsub-kafka

A Kafka adapter for the php-pubsub package
PHP
30
star
6

php-pubsub-redis

A Redis adapter for the php-pubsub package
PHP
24
star
7

monolog-google-cloud-json-formatter

A Monolog extension for formatting log entries for Google Cloud Logging
PHP
15
star
8

php-pubsub-google-cloud

A Google Cloud adapter for the php-pubsub package
PHP
12
star
9

php-money

A money and currency library for handling arbitrary-precision arithmetic
PHP
11
star
10

laravel-zendesk

A Laravel Zendesk library for integrating with the Zendesk API
PHP
9
star
11

php-event-pubsub

An event protocol and implementation over pub/sub
PHP
8
star
12

python-pubsub

Python
6
star
13

laravel-event-pubsub

An event protocol and implementation over pub/sub for Laravel
PHP
6
star
14

laravel4-storage

A filesystem abstraction library for Laravel 4
PHP
4
star
15

laravel-ajax-redirector

A Laravel library for handling AJAX redirects
PHP
3
star
16

js-pubsub

A JS abstraction for the pub-sub pattern
JavaScript
3
star
17

js-pubsub-google-cloud

A Google Cloud adapter for the js-pubsub package
JavaScript
2
star
18

laravel-lusitanian-oauth-session-store

A Laravel session storage interface for the lusitanian/oauth library
PHP
2
star
19

js-pubsub-redis

A Redis adapter for the js-pubsub package
JavaScript
2
star
20

js-event-pubsub

An event protocol and implementation over pub/sub
JavaScript
2
star
21

js-pubsub-http

An HTTP adapter for the js-pubsub package
JavaScript
2
star
22

laravel-appboy

A Laravel library for sending push notifications via the Appboy API
PHP
1
star
23

simple-sms-panacea-mobile

An adapter for the simple-sms Laravel library for sending SMSs via the Panacea Mobile API
PHP
1
star
24

laravel4-event-pubsub

An event protocol and implementation over pub/sub for Laravel 4
PHP
1
star
25

panaceaapi

The Panacea Mobile PHP class for easy deployment and integration
PHP
1
star
26

php-panaceamobile

An API client for sending SMSs via the Panacea Mobile API
PHP
1
star
27

spreeza-dust-recursive-loader

Webpack loader for processing dust templates recursively.
JavaScript
1
star
28

laravel4-psr6-cache-bridge

A PSR6 cache implementation for Laravel 4
PHP
1
star
29

js-pubsub-manager

A manager & factory for the js-pubsub package
JavaScript
1
star
30

php-pubsub-http

An HTTP adapter for the php-pubsub package
PHP
1
star
31

lint-review-images

Images built for use with lint-review
Dockerfile
1
star
32

laravel4-pubsub

A Pub-Sub abstraction for Laravel 4
PHP
1
star
33

php-appboy

A PHP client for sending push notifications via the Appboy API
PHP
1
star
34

js-pubsub-rest-proxy

An HTTP server which acts as a gateway for publishing messages via a js-pubsub adapter
JavaScript
1
star