如何在 Apache 下运行的应用程序中保留动态应用程序级统计信息?

发布于 2024-08-18 21:58:24 字数 332 浏览 4 评论 0原文

我有一个在 apache 下运行的应用程序,我想保留“当前”统计信息。我想让应用程序告诉我这样的事情:

  • 每秒的请求数,按请求
  • 延迟类型细分,通过 Thrift 向各种后端服务发出请求(按服务和服务器细分)
  • 每秒处理的错误数
  • 等。

我想要无需任何外部依赖即可执行此操作。但是,我遇到了在 apache 进程之间共享统计信息的问题。显然,我不能只使用全局内存。解决此类问题的好模式是什么?

该应用程序是使用 pylons 用 python 编写的,尽管我怀疑这更多的是“跨进程通信”设计问题,而不是 python 特定的问题。

I have an application running under apache that I want to keep "in the moment" statistics on. I want to have the application tell me things like:

  • requests per second, broken down by types of request
  • latency to make requests to various backend services via thrift (broken down by service and server)
  • number of errors being served per second
  • etc.

I want to do this without any external dependencies. However, I'm running into issues sharing statistics between apache processes. Obviously, I can't just use global memory. What is a good pattern for this sort of issue?

The application is written in python using pylons, though I suspect this is more of a "communication across processes" design question than something that's python specific.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傾旎 2024-08-25 21:58:24

也许您可以将相关计数器和其他统计信息保存在所有 apache 进程都可以访问的 memcached 中?

Perhaps you could keep the relevant counters and other statistics in a memcached, that is accessed by all apache processes?

彼岸花似海 2024-08-25 21:58:24

我想在没有任何外部的情况下做到这一点
依赖关系。

如果你的 apache 以某种方式死掉了怎么办?(关注点分离?)

我个人正在使用(冗余) Nagios 用于监控硬件本身、服务和应用程序指标。这样我就可以轻松/自动地绘制“每秒请求数/在线用户数”、“每秒 CPU 负载/用户活动 X”等图表,这对很多事情都有帮助。

为 nagios 编写插件非常容易,而且还有数千个任何语言的预制脚本。

Apache监控

我通过nagios插件从apache mod_status页面提取我需要的信息来监控apache。

插件响应示例:

APACHE OK - 0.080 秒。响应时间,繁忙/空闲 18/16,打开 766/800,ReqPerSec 12.4,BytesPerReq 3074,BytesPerSec 38034

应用程序监控

我使用 mod_status 只是作为您的列表的示例我想监控。

对于我们的应用程序,我们有一个非常小的 Nagios 插件框架,因此基本上每个 nagios 检查都是一个小类,它针对缓存或数据库运行检查并将其值返回给 nagios(小而简单的命令行脚本)。

更多示例:

Memcache:
OK - consumption: 82.88% (106.1 MBytes/128.0 MBytes), connections: 2, requests/s: 10.99, hitrate: 95.2% (34601210/36346999), getrate: 50.1% (36346999/72542987)

Application feature #1 usage:
OK - last 5m: 22 last 24h: 655 ever: 26121

Application feature #2 usage:
OK - last 5m: 39 last 24h: 11011

Other applications metrics:
OK - users online: 556

我想说的是:扩展 Nagios 进行应用程序监控非常容易。
我的小框架花了我 3-4 个小时来编写,现在我添加的任何检查只需要几分钟。

Nagios 插件开发指南

I want to do this without any external
dependencies.

What if your apache dies somehow? (Separation of concerns?)

Personally I am using (redundant) Nagios to monitor the hardware itself, services, and application metrics. This way i can easily/automatically plot "requests per second/users online", "cpu load/user activy X per second" etc. graphs which help with lots of things.

Writing plugins for nagios is really easy, also there are thousands of premade scripts in any language.

Apache monitoring

I am monitoring apache by extracting the information I need from the apache mod_status page via nagios plugin.

Example plugin response:

APACHE OK - 0.080 sec. response time, Busy/Idle 18/16, open 766/800, ReqPerSec 12.4, BytesPerReq 3074, BytesPerSec 38034

Application Monitoring

I used mod_status just as an example for your list of things you'd like to monitor.

For our application we have a very small framework for Nagios plugins, so basically every nagios check is a small class which runs its check against a cache or database and returns its value to nagios (small and simple commandline-script).

more examples:

Memcache:
OK - consumption: 82.88% (106.1 MBytes/128.0 MBytes), connections: 2, requests/s: 10.99, hitrate: 95.2% (34601210/36346999), getrate: 50.1% (36346999/72542987)

Application feature #1 usage:
OK - last 5m: 22 last 24h: 655 ever: 26121

Application feature #2 usage:
OK - last 5m: 39 last 24h: 11011

Other applications metrics:
OK - users online: 556

What I want to say: Extending Nagios for application monitoring is very easy.
With my little framework which took me 3-4 hours to write, any check I am adding takes me just some minutes now.

Nagios plug-in development guidelines

过气美图社 2024-08-25 21:58:24

使用 pylons.g 对象。它是 Pylons 应用程序的 lib/app_globals.py 文件中 Globals 类的实例。它的状态更改对所有线程都是可见的,因此其中的内容需要是线程安全的。

lib/app_globals.py:

class Globals(object):
    def __init__(self):
        self.requests_served = 0

控制器/status.py:

from pylons import g

class StatusController(BaseController):
    def status(self):
        g.requests_served += 1
        return "Served %d requests." % g.requests_served

Use pylons.g object. It is an instance of Globals class in your Pylons application's lib/app_globals.py file. Its state changes will be visible to all threads, so stuff in it needs to be threadsafe.

lib/app_globals.py:

class Globals(object):
    def __init__(self):
        self.requests_served = 0

controllers/status.py:

from pylons import g

class StatusController(BaseController):
    def status(self):
        g.requests_served += 1
        return "Served %d requests." % g.requests_served
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文