Git Product home page Git Product logo

guzzle-middleware-responsetimer's Introduction

Response timer for Guzzle

build_status_img sensiolabs_insight_img code_quality_img latest_stable_version_img versioneye_dependencies_img license_img twitter_img

A simple timer and logger that records response times for requests made by a Guzzle client to a PSR-3 log. Created initially to help Gousto get an idea of how microservices were interacting and performing.

It has some advantages over some other timers that already existed, in that it natively supports asynchronous calls, and uses the Request object itself as the key, therefore allowing multiple calls to the same URI to be recorded separately.

Installation

Installation is recommended via composer:

composer require shrikeh/guzzle-middleware-response-timer

Requirements and versioning

If installed by composer, all requirements should be taken care of. Semantic versioning is in use and strongly adhered to in tags 1.0 and beyond; branches before that are a little bit more free as I was shopping with ideas and the interface.

Tags <2.0 are 5.6 compatible; versions 2.0 and beyond are PHP 7.1+ only.

Basic usage

The following is a simple example using the quickStart() method, which accepts a Psr\Log\LoggerInterface logger (in this case, a simple file stream implemented by Monolog):

<?php

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Promise;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Shrikeh\GuzzleMiddleware\TimerLogger\Middleware;

require_once __DIR__.'/../vendor/autoload.php';

$logFile = __DIR__.'/logs/example.log';
$logFile = new SplFileObject($logFile, 'w+');

// create a log channel
$log = new Logger('guzzle');
$log->pushHandler(new StreamHandler(
    $logFile->getRealPath(),
    Logger::DEBUG
));

// hand it to the middleware (this will create a default working example)
$middleware = Middleware::quickStart($log);

// now create a Guzzle middleware stack
$stack = HandlerStack::create();

// and register the middleware on the stack
$stack->push($middleware());

$config = [
    'timeout'   => 2,
    'handler' => $stack,
];

// then hand the stack to the client
$client = new Client($config);

$promises = [
    'facebook'  => $client->getAsync('https://www.facebook.com'),
    'wikipedia' => $client->getAsync('https://en.wikipedia.org/wiki/Main_Page'),
    'google'    => $client->getAsync('https://www.google.co.uk'),
];

$results = Promise\settle($promises)->wait();

print $logFile->fread($logFile->getSize());

Exception handling

By default, the Middleware::quickStart() method boots the start and stop handlers with a TriggerErrorHandler that simply swallows any exception thrown and generates an E_USER_NOTICE error instead. This is to ensure that any problems with logging do not cause any application-level problems: there isn't a default scenario in which a problem logging response times should break your application. Nor, as the exception is most likely to do with the underlying Logger, is there logging of the exception thrown.

If you wish to throw exceptions and handle them differently, load your handlers with an implementation of the ExceptionHandlerInterface.

Service Provider

The package comes with a ServiceProvider for Pimple, my preferred choice of standalone PSR-11 compliant container. Pimple itself is not listed as a dependency in require (although it is in require-dev for testing), so you will need to add it to your project directly if you wish to use the service provider:

composer require --prefer-dist pimple/pimple

As the service provider relies on a PSR-3 Logger, building a Container requires passing this in:

<?php

require_once __DIR__.'/../vendor/autoload.php';

use Monolog\Handler\StreamHandler;
use Psr\Log\LogLevel;
use Shrikeh\GuzzleMiddleware\TimerLogger\ServiceProvider\TimerLogger;

$logsPath = __DIR__.'/logs';
if (!is_dir($logsPath)) {
    mkdir($logsPath);
}

$logFile = new SplFileObject($logsPath.'/example.log', 'w+');

// create a log channel
$logger = new \Monolog\Logger('guzzle');
$logger->pushHandler(new StreamHandler(
    $logFile->getRealPath(),
    LogLevel::DEBUG
));

$pimple = new Pimple\Container();


// Create the middleware directly from an active instance of a LoggerInterface
$pimple->register(TimerLogger::fromLogger($logger));

$callable = function() use ($logFile) {
    $logger = new \Monolog\Logger('guzzle');
    $logger->pushHandler(new StreamHandler(
        $logFile->getRealPath(),
        LogLevel::DEBUG
    ));

    return $logger;
};

// Alternatively pass a simple callable to the static constructor
$pimple->register(TimerLogger::fromCallable($callable));

$someKeyForALogger = 'some_key_for_a_logger';

$pimple[$someKeyForALogger] = $callable;

$container = new Pimple\Psr11\Container($pimple);

// Or pass it a PSR-11 container and the key that will unwrap the PSR-3 LoggerInterface
$pimple->register(TimerLogger::fromContainer($container, $someKeyForALogger));

// The middleware is good to go.
echo get_class($pimple[TimerLogger::MIDDLEWARE]);

guzzle-middleware-responsetimer's People

Contributors

scrutinizer-auto-fixer avatar shrikeh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.