一个简单的库,将有关程序的统计信息写入命名管道。
statvent的Python项目详细描述
状态
Statvent提供三种功能:
- 用于在库或应用程序中记录度量的开发人员api。
- 将统计信息写入命名管道的部署程序API。
- 一个简单的web服务,它将从主机和 将结果作为json提供。
开发人员api
很简单。有三种记录数据的功能。
statvent.incr(name, value=1)
Call it with the ^{tt2}$ of a stat and it will increment it. If it doesn’t exist yet, it will initialize it to the given ^{tt3}$ (defaults to ^{tt4}$).
It’s very useful for keeping tabs on events that happen within your apps.
statvent.set(name, value)
Call it with the ^{tt2}$ of a stat and a value. The stat will be set to that value.
This function is useful for values that can fluctuate, like number of concurrent users, connections to a database, winning streaks, etc.
statvent.record(name, value, format_func=str.format)
Call it with the ^{tt2}$ of a stat and a value. By default the ^{tt2}$ must include a ^{tt10}$ format placeholder. Internally, the value is appended to a deque. When the pipe is read, the stats recorder will calculate some aggregate statistics from the contents of the deque (by default a few percentile breaks and the mean).
If your stat name scheme conflicts with the default ``str.format`` function you can provide your own function to format the stat name to include the calculated percentile labels.
stats的名称只需要是字节字符串。但是您可以格式化它们 如果你想的话,包括任何让你快乐的标点符号,等等 百分位数或其他计算的统计数据(使用statvent.record),您将需要 在格式化统计名称时要格外小心。
值可以是整数或浮点数。注意一旦你使用了一个浮点数 将保持浮动。也许没那么重要,但现在你知道了。
部署程序API
请注意,没有提到用于读取统计信息的api。那是因为 应用程序度量的创建和使用应该分离。这个 分离是通过将数据写入命名管道来实现的。
statvent.start_recorder()
Starts a thread that writes the application stats to a named pipe.
The thread blocks on the open system call won’t spend any of your application’s resources until another process opens the pipe for reading.
命名管道
默认情况下,管道位于/tmp/stats-pipe/中,并命名为 <pid>.stats,其中<pid>是正在写入数据的unix进程id 进入那个命名的管道。可以更改管道的位置 通过设置statvent.stats.config['pipe_dir']路径进行写入/读取。制造 当然你的程序有权限写在那里。
每个stat都被写入命名管道中自己的行中。的名称 stat和当前值用冒号分隔,后跟 空间。后面的值是浮点值或整数 价值。还有一个换行符。下面是一个示例:
a.b.c.d: 42 My Nice Stat: 123.45 another[one]: 0
您可以通过对 命名管道,或具有定期执行此操作并插入结果的进程 在数据库中跟踪数据。
Web服务
如果你不想用一种奇怪的纯文本格式 从命名管道中,您希望从多个进程收集统计信息 在主机上,json web服务可能会引起您的兴趣。
它将汇总所有统计数据的值,并将它们作为json提供。试着跑步 python -m statvent.web作为脚本来查看它的运行情况。