stevencorona/opcache-json 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

stevencorona/opcache-json

Composer 安装命令:

composer require stevencorona/opcache-json

包简介

Library that grabs some critical stats from Zend Opcache and outputs to JSON for monitoring and alerting. Can also output to statsd sink.

README 文档

README

PHP 5.5 is awesome. It's fast, more modern, and has a bunch of great features. One of those new, awesome features is that it gets bundled with Zend Opcache by default— no more APC! Hooray!

But this win brings some new burden. Zend Opcache has a TON of configuration settings and monitoring them is not that easy. The default output is a little bit clunky and wasn't really designed with modern devops in mind.

Designed for Modern Devops

This library was designed to solve that problem. It cleans up Zend Opcache's internal stats and makes it easily grabbable as JSON, which your app can choose to expose to an internal HTTP endpoint. Opcache-json also exposes this data to statsd, which you can use to view historical cache data and watch trends.

Getting Started

The best way to install it is to use Composer and add the following to your project's composer.json file:

{
    "require": {
        "stevencorona/opcache-json": "*"
    }
}

Basic Usage

// By default Statsd output is disabled
// $opcache = new Opcache\Status;

// Or pass in Statsd config with an array
// $opcache = new Opcache\Status(["host" => "localhost", "port" => "8125"]);

// Or configure the Statsd connection with a block
$opcache = new Opcache\Status(function() {

  $c   = new \Domnikl\Statsd\Connection\UdpSocket("127.0.0.1", "8125");
  return new \Domnikl\Statsd\Client($c, "opcache");

});

echo $opcache->status(true);

Statsd Output

Statsd output comes stock. Just configure your connection by passing a block or an array to the Opcache\Status constructor. The following keys are sent as gauge values to Statsd:

opcache.used_memory:5593056|g
opcache.free_memory:61515808|g
opcache.wasted_memory:0|g
opcache.current_wasted_percentage:0|g
opcache.num_cached_scripts:11|g
opcache.num_cached_keys:13|g
opcache.max_cached_keys:3907|g
opcache.hits:33|g
opcache.start_time:1396011952|g
opcache.last_restart_time:0|g
opcache.oom_restarts:0|g
opcache.hash_restarts:0|g
opcache.manual_restarts:0|g
opcache.misses:11|g
opcache.blacklist_misses:0|g
opcache.blacklist_miss_ratio:0|g
opcache.opcache_hit_rate:75|g

Hint: If you have a multi-server or multi-worker environment (hint: most of us do), you probably want to add your server hostname and PHP PID to the key namespace.

Extra stats

If you want to send extra stats about state of Opcache, meaning one of the following metrics:

opcache.opcache_enabled|1|g
opcache.cache_full|0|g
opcache.restart_pending|0|g
opcache.restart_in_progress|0|g

You can do it by calling Opcache\Status->send_extra_stats() method before calling status():

$opcache->send_extra_stats(array (
  'opcache_enabled'     => true,
  'cache_full'          => true,
));

How to run this in production

The obvious, but less than elegant, way might be embed this library into your application and create an endpoint. Workable, but not so clean.

Here's a better way to do it, if you're using PHP-FPM. First, you need to install the cgi-fcgi utility (it's in the ubuntu package apt-get install libfcgi0ldbl).

Next, clone this repository to somewhere on your server. For this demo, we'll pretend that it's in /tmp/opcache-json. I'll also assume that your PHP-FPM pool is running on 127.0.0.1:9000. Now, here's the trick:

SCRIPT_NAME=/example.php \
SCRIPT_FILENAME=/tmp/opcache-json/example.php \
REQUEST_METHOD=GET \
cgi-fcgi -bind -connect 127.0.0.1:9000

That will return the JSON status of your running PHP-FPM pool back to you. You can put it into a cron or something and automatically monitor it without having to pollute your running application with this code.

JSON Output

The JSON output is easy to ready and can be exposed as an internal HTTP endpoint, which can be consumed by human eyes or a monitoring systems. It's pretty straightforward— the output looks like this

JSON Output

Running the tests

You must enable opcache.enable_cli to run the tests. PHP must be built with Zend Opcache.

$ composer install
$ ./vendor/bin/phpunit -d opcache.enable_cli=1 tests/Opcache.php

The MIT License (MIT)

Copyright (c) 2014 Steve Corona Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

stevencorona/opcache-json 适用场景与选型建议

stevencorona/opcache-json 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 310.69k 次下载、GitHub Stars 达 117, 最近一次更新时间为 2014 年 03 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 stevencorona/opcache-json 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 stevencorona/opcache-json 我们能提供哪些服务?
定制开发 / 二次开发

基于 stevencorona/opcache-json 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 310.69k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 117
  • 点击次数: 37
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 117
  • Watchers: 5
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-03-28