All Projects → fetzi → server-timing

fetzi / server-timing

Licence: MIT License
Collect backend metrics and provide them as Server-Timing header in your responses

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to server-timing

AkamaiOPEN-edgegrid-php-client
PHP client library for Akamai {OPEN} EdgeGrid Authentication scheme (based on Guzzle)
Stars: ✭ 38 (+11.76%)
Mutual labels:  middleware
horse-logger
Middleware for access logging in HORSE
Stars: ✭ 25 (-26.47%)
Mutual labels:  middleware
jwt-auth
JSON Web Token Authentication for Laravel and Lumen
Stars: ✭ 46 (+35.29%)
Mutual labels:  middleware
NiFi4Trading
NiFi Bundle for FIX Protocol
Stars: ✭ 14 (-58.82%)
Mutual labels:  middleware
DotNETCarRental
Daily car rental simulation with ASP.NET.
Stars: ✭ 13 (-61.76%)
Mutual labels:  middleware
whoops-middleware
PSR-15 compatible middleware for Whoops, the pretty error handler
Stars: ✭ 24 (-29.41%)
Mutual labels:  middleware
throttle
Throttling Middleware for Martini
Stars: ✭ 63 (+85.29%)
Mutual labels:  middleware
rbac
RBAC - Simple, concurrent Role Based Access Control(GO)
Stars: ✭ 67 (+97.06%)
Mutual labels:  middleware
spiderable-middleware
🤖 Prerendering for JavaScript powered websites. Great solution for PWAs (Progressive Web Apps), SPAs (Single Page Applications), and other websites based on top of front-end JavaScript frameworks
Stars: ✭ 29 (-14.71%)
Mutual labels:  middleware
express-firebase-middleware
🔥 Express middleware for your Firebase applications
Stars: ✭ 53 (+55.88%)
Mutual labels:  middleware
connect-browser-sync
Connect middleware for BrowserSync
Stars: ✭ 16 (-52.94%)
Mutual labels:  middleware
xenon
A middleware abstraction library that provides a simple programming interface to various compute and storage resources.
Stars: ✭ 28 (-17.65%)
Mutual labels:  middleware
express-ping
Let all your express applications expose a common API to inform about their internal status and health.
Stars: ✭ 50 (+47.06%)
Mutual labels:  middleware
oryx
.NET Cross platform and highly composable middleware for building web request handlers in F#
Stars: ✭ 178 (+423.53%)
Mutual labels:  middleware
interface-doc
The technical specification of the data interface that describes how to integrate the fiskaltrust Middleware into POS systems.
Stars: ✭ 17 (-50%)
Mutual labels:  middleware
redux-global-loader
A Redux middleware for global loader
Stars: ✭ 13 (-61.76%)
Mutual labels:  middleware
AspNetCore.Weixin
An ASP.NET Core middleware for Wechat/Weixin message handling and apis. (微信公众平台/接口调用服务)
Stars: ✭ 24 (-29.41%)
Mutual labels:  middleware
rmw ecal
ROS2 middleware based on eCAL
Stars: ✭ 30 (-11.76%)
Mutual labels:  middleware
mooseware
💀 Skeleton for writing a middleware handler
Stars: ✭ 47 (+38.24%)
Mutual labels:  middleware
express-xml-bodyparser
Simple XML body parser connect/express middleware
Stars: ✭ 64 (+88.24%)
Mutual labels:  middleware

Server-Timing middleware package

Packagist Github Actions StyleCI Maintainability

server-timing is a PHP middleware package that adds the Server-Timing header to your responses. This information can be viewed in your web browser, for example in Google Chrome.

More information on the ServerTiming response header can be found on MDN docs.

The package automatically adds two default metrics but allows you to add custom metrics as well.

Installation

composer require fetzi/server-timing

Setup

The package automatically measures two Timing values in the middleware implementation:

  1. Bootstrap: The time taken in the application bootstrapping phase
  2. Request: The total time your request handler(s) needed to process your request

To make sure all timings are as exact as possible it is important to add the middleware at the outermost (last) position of your request handler stack.

PSR-15 Middleware

To enable the middleware you simply need to add an instance of Fetzi\ServerTiming\ServerTimingMiddleware to your middleware stack.

In a Slim application you can do this by simply adding

$app->add(new ServerTimingMiddleware($container->get(ServerTimings::class)));

to your middleware stack code.

Laravel Middleware

To enable the middleware in your Laravel application you need to use the Fetzi\ServerTiming\Laravel\ServerTimingMiddleware class.

If you want to enable the middleware in general simply add the class definition to the $middleware array in the Kernel class. If you want to enable the middleware only if a condition is met, you need to override the handle method of the Kernel as demonstrated here.

Usage

To be able to add your custom server timings you need to make sure that you register the ServerTimings class as a singleton in your dependency injection container. This allows you to inject the ServerTimings class into any other class and create and measure a custom server timing.

function fetchUsers()
{
    $fetchUsers = $this->serverTimings->create('fetchUsers');
    $fetchUsers->start();
    $users = $this->db->getUsers();
    $fetchUsers->stop();

    return $users;
}

This code will generate a new server timing named fetchUsers and by calling the start and the stop method the the execution time is measured.

Creating a ServerTiming instance

The ServerTimings class provides a create method to create a ServerTiming instance that is automatically registered and will be sent back by the middleware.

// create a ServerTiming with a name
$serverTiming = $serverTimings->create('foo');

// create a ServerTiming with a name and a description
$serverTiming = $serverTimings->create('foo', 'bar');

Measuring a ServerTiming

The ServerTiming instance provides two methods for starting and stopping the measurement.

// normal usage
$serverTiming->start();
// ...
$serverTiming->stop();

// set a manual start value (a microtime value as float)
$serverTiming->start(1000000.00);
// ...
$serverTiming->stop();

The middleware will make sure to collect all measured ServerTiming instances and append their values in the Server-Timing response header.

License

The MIT License (MIT). Please see the License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].