class MemcacheServerStatsSubscriber in Memcache API and Integration 8.2
Adds memcache server specific details to the stats array.
Hierarchy
- class \Drupal\memcache_admin\EventSubscriber\MemcacheServerStatsSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses MessengerTrait, StringTranslationTrait
Expanded class hierarchy of MemcacheServerStatsSubscriber
1 string reference to 'MemcacheServerStatsSubscriber'
- memcache_admin.services.yml in memcache_admin/
memcache_admin.services.yml - memcache_admin/memcache_admin.services.yml
1 service uses MemcacheServerStatsSubscriber
File
- memcache_admin/
src/ EventSubscriber/ MemcacheServerStatsSubscriber.php, line 14
Namespace
Drupal\memcache_admin\EventSubscriberView source
class MemcacheServerStatsSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
use MessengerTrait;
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[MemcacheStatsEvent::BUILD_MEMCACHE_STATS][] = [
'onPopulateStats',
100,
];
$events[MemcacheStatsEvent::REPORT_MEMCACHE_STATS][] = [
'onReportStats',
100,
];
return $events;
}
/**
* Populates the Memcache Server Stats
*
* @param \Drupal\memcache_admin\Event\MemcacheStatsEvent $event
* The event being dispatched.
*
* @throws \Exception
*/
public function onPopulateStats(MemcacheStatsEvent $event) {
$raw_stats = $event
->getRawStats();
$bin = $event
->getCacheBin();
// No cache bin data, return.
if (!isset($raw_stats[$bin])) {
return;
}
// No servers found, return.
if (!is_array($raw_stats[$bin])) {
return;
}
$servers = array_keys($raw_stats[$bin]);
$memcache_servers = [];
foreach ($servers as $server) {
// Memcache servers report libevent version, use that for detecting stats.
if (isset($raw_stats[$bin][$server]['libevent'])) {
$event
->updateFormattedStats('memcache', $bin, $server, new MemcacheStatsObject($raw_stats[$bin][$server]));
$event
->updateServers($server);
}
}
if (isset($raw_stats[$bin]['total'])) {
$event
->updateTotals([
$bin => new MemcacheStatsObject($raw_stats[$bin]['total']),
]);
}
}
/**
* Populates the reporting of a stored set of stats.
*
* @param \Drupal\memcache_admin\Event\MemcacheStatsEvent $event
*/
public function onReportStats(MemcacheStatsEvent $event) {
$stats = $event
->getFormattedStats('memcache');
$bin = $event
->getCacheBin();
// No cache bin data, return.
if (empty($stats[$bin])) {
// Failed to load statistics. Provide a useful error about where to get
// more information and help.
$this
->messenger()
->addError($this
->t('There may be a problem with your Memcache configuration. Please review @readme for more information.', [
'@readme' => 'README.txt',
]));
return;
}
// No servers found, return.
if (!is_array($stats[$bin])) {
return;
}
/**
* @var string $server
* @var MemcacheStatsObject $statistics
*/
foreach ($stats[$bin] as $server => $statistics) {
if (empty($statistics
->getUptime())) {
$this
->messenger()
->addError($this
->t('Failed to connect to server at :address.', [
':address' => $server,
]));
}
else {
$data['server_overview'][$server] = $this
->t('v@version running @uptime', [
'@version' => $statistics
->getVersion(),
'@uptime' => $statistics
->getUptime(),
]);
$data['server_time'][$server] = $statistics
->getServerTime();
$data['server_connections'][$server] = $statistics
->getConnections();
$data['cache_sets'][$server] = $statistics
->getSets();
$data['cache_gets'][$server] = $statistics
->getGets();
$data['cache_counters'][$server] = $statistics
->getCounters();
$data['cache_transfer'][$server] = $statistics
->getTransferred();
$data['cache_average'][$server] = $statistics
->getConnectionAvg();
$data['memory_available'][$server] = $statistics
->getMemory();
$data['memory_evictions'][$server] = $statistics
->getEvictions();
}
}
// Build a custom report array.
$report = [
'uptime' => [
'uptime' => [
'label' => $this
->t('Uptime'),
'servers' => $data['server_overview'],
],
'time' => [
'label' => $this
->t('Time'),
'servers' => $data['server_time'],
],
'connections' => [
'label' => $this
->t('Connections'),
'servers' => $data['server_connections'],
],
],
'stats' => [
'sets' => [
'label' => $this
->t('Sets'),
'servers' => $data["cache_sets"],
],
'gets' => [
'label' => $this
->t('Gets'),
'servers' => $data["cache_gets"],
],
'counters' => [
'label' => $this
->t('Counters'),
'servers' => $data["cache_counters"],
],
'transfer' => [
'label' => $this
->t('Transferred'),
'servers' => $data["cache_transfer"],
],
'average' => [
'label' => $this
->t('Per-connection average'),
'servers' => $data["cache_average"],
],
],
'memory' => [
'memory' => [
'label' => $this
->t('Available memory'),
'servers' => $data['memory_available'],
],
'evictions' => [
'label' => $this
->t('Evictions'),
'servers' => $data['memory_evictions'],
],
],
];
// Don't display aggregate totals if there's only one server.
if (count($stats[$bin]) > 1) {
/** @var MemcacheStatsObject $totals */
$totals = $event
->getTotals();
$report['uptime']['uptime']['total'] = $this
->t('n/a');
$report['uptime']['time']['total'] = $this
->t('n/a');
$report['uptime']['connections']['total'] = $totals[$bin]
->getConnections();
$report['stats']['sets']['total'] = $totals[$bin]
->getSets();
$report['stats']['gets']['total'] = $totals[$bin]
->getGets();
$report['stats']['counters']['total'] = $totals[$bin]
->getCounters();
$report['stats']['transfer']['total'] = $totals[$bin]
->getTransferred();
$report['stats']['average']['total'] = $totals[$bin]
->getConnectionAvg();
$report['memory']['memory']['total'] = $totals[$bin]
->getMemory();
$report['memory']['evictions']['total'] = $totals[$bin]
->getEvictions();
}
$event
->updateReport($report);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MemcacheServerStatsSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
MemcacheServerStatsSubscriber:: |
public | function | Populates the Memcache Server Stats | |
MemcacheServerStatsSubscriber:: |
public | function | Populates the reporting of a stored set of stats. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |