Monitoring API calls in Symfony2 Application

Janusz Slota - Tue 08 October 2013 - symfony2, api, monitoring

These days, we use APIs more and more, not only for integrations but also for persistence. Therefore rather than monitoring database calls in Doctrine, we need to start monitor the number of API calls to external REST services - as it may happen that various parts of the system are calling various API endpoints, sometimes even few times on the single page load. In fact, you probably don't know how many API calls your application is making at the moment, unless you monitor this activity.

In order to make it seamless I've created ApiLoggerBundle. It adds API tab into Symfony profiler and works in similar way to Doctrine's.

Profiler API Link

Internally it extends from Buzz client and wraps the API to measure time and get various parameters. Currently it works only with Buzz, however if you need support for other http clients, please feel free to submit PR on Github.

Profiler API Results

Installation

Easy, just follow instruction in README file. First add it to your composer.json

{
    "require-dev": {
        "nixilla/api-logger-bundle": "~0.2.0"
    }
}

This bundle is meant to be used in dev mode only, as exposing information in prod can cause security issues. Therefore make sure you add it to require-dev section. Now just update your vendors:

./bin/composer.phar update --dev

And add the bundle to your AppKernel:

<?php

// app/AppKernel.php

class AppKernel extends Kernel
{
    // other code here

    if (in_array($this->getEnvironment(), array('dev', 'test'))) {
        // your other dev bundles here
        $bundles[] = new Nixilla\Api\LoggerBundle\NixillaApiLoggerBundle();
    }
}

Sensio/BuzzBundle

If you're using @buzz service in your application, then you can override default definition and take advantage of this bundle. All you need to do is add these few lines into your config_dev.yml

# app/config/config_dev.yml

imports:
    - { resource: config.yml }

# make sure you add this after imports, otherwise your setting may be overridden

parameters:
    buzz.client.class: Nixilla\Api\LoggerBundle\Proxy\Buzz\Client\Curl

services:
    buzz.client:
        class: %buzz.client.class%
        calls:
            - [ 'setTimeout', [ %buzz.client.timeout% ] ]
            - [ 'setLogger', [ @nixilla.api.logger ] ]

From now, every time you call any method on @buzz, it'll be logged and visible in the Symfony profiler.

HWIOAuthBundle

Primarily this bundle was created to help me debug HWIOAuthBundle API calls. HWIOAuthBundle is using Buzz too, but not with Sensio/BuzzBundle. It means that configuration needs to be slightly different.

# app/config/config_dev.yml

imports:
    - { resource: config.yml }

# make sure you add this after imports, otherwise your setting may be overridden

parameters:
    buzz.client.class: Nixilla\Api\LoggerBundle\Proxy\Buzz\Client\Curl

services:
    hwi_oauth.http_client:
        class: %buzz.client.class%
        calls:
            - [ 'setLogger', [ @nixilla.api.logger ] ]