Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Controller to show Phystrix metrics using symfony2 phystrix-bundle config
odesk_phystrix:
default:
# Whether fallback logic of the phystrix command is enabled
fallback: false
circuitBreaker:
# How many failed request it might be before we open the circuit (disallow consecutive requests)
errorThresholdPercentage: 5
# If true, the circuit breaker will always be open regardless the metrics
forceOpen: false
# If true, the circuit breaker will always be closed, allowing all requests, regardless the metrics
forceClosed: false
# How many requests we need minimally before we can start making decisions about service stability
requestVolumeThreshold: 1
# For how long to wait before attempting to access a failing service
sleepWindowInMilliseconds: 5000
metrics:
# This is for caching metrics so they are not recalculated more often than needed
healthSnapshotIntervalInMilliseconds: 1000
# The period of time within which we the stats are collected
rollingStatisticalWindowInMilliseconds: 50000
# The more buckets the more precise and actual the stats and slower the calculation.
rollingStatisticalWindowBuckets: 10
# Request cache, if enabled and a command has getCacheKey implemented caches results within current http request
requestCache: false
# Request log collects all commands executed within current http request
requestLog: true
// When working with Phystrix-bundle you must supply configuration in config.yml.
// Phystrix-Dashboard offers helper classes to serve phystrix metrics but you need to pass a config array with
// the config.
// Next code shows how you can create a symfony2 controller that passes the phystrix config.yml configuration and
// avoids duplications.
<?php
namespace MyBundle\Controller;
use Odesk\PhystrixDashboard\MetricsEventStream\ApcMetricsPoller;
use Odesk\PhystrixDashboard\MetricsEventStream\MetricsServer;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Zend\Config\Config;
class PhystrixController extends Controller
{
/**
* Streaming response to return Phystrix statistics.
*
* @Route("/phystrix_status", name="phystrix_status")
*/
public function statusAction()
{
// Made this end point accesible from local host in production environment
if ('prod' === $this->get('kernel')->getEnvironment()) {
if (isset($_SERVER['HTTP_CLIENT_IP'])
|| isset($_SERVER['HTTP_X_FORWARDED_FOR'])
|| !(in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1')) || php_sapi_name() === 'cli-server')
) {
header('HTTP/1.0 403 Forbidden');
exit('You are not allowed to access this file.');
}
}
$response = new StreamedResponse();
$response->setCallback(function () {
// Get phystrix data configuration
$configData = $this->getParameter('phystrix.configuration.data');
$config = new Config($configData);
$metricsPoller = new ApcMetricsPoller($config);
$metricsServer = new MetricsServer($metricsPoller);
$metricsServer->run();
});
$response->send();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment