2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2012-06-22 17:34:23 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2012-06-22 17:34:23 +00:00
|
|
|
_d("STATSD_HOST", null);
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class StatsDInterface extends Extension
|
|
|
|
{
|
2024-01-20 14:10:59 +00:00
|
|
|
/** @var array<string, string> */
|
2021-03-14 23:43:50 +00:00
|
|
|
public static array $stats = [];
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
private function _stats(string $type): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-10-02 09:49:32 +00:00
|
|
|
global $_shm_event_count, $cache, $database, $_shm_load_start;
|
2023-01-11 13:27:57 +00:00
|
|
|
$time = ftime() - $_shm_load_start;
|
2019-05-28 16:59:38 +00:00
|
|
|
StatsDInterface::$stats["shimmie.$type.hits"] = "1|c";
|
|
|
|
StatsDInterface::$stats["shimmie.$type.time"] = "$time|ms";
|
|
|
|
StatsDInterface::$stats["shimmie.$type.time-db"] = "{$database->dbtime}|ms";
|
|
|
|
StatsDInterface::$stats["shimmie.$type.memory"] = memory_get_peak_usage(true)."|c";
|
|
|
|
StatsDInterface::$stats["shimmie.$type.files"] = count(get_included_files())."|c";
|
|
|
|
StatsDInterface::$stats["shimmie.$type.queries"] = $database->query_count."|c";
|
|
|
|
StatsDInterface::$stats["shimmie.$type.events"] = $_shm_event_count."|c";
|
2023-02-02 16:46:25 +00:00
|
|
|
StatsDInterface::$stats["shimmie.$type.cache-hits"] = $cache->get("__etc_cache_hits", -1)."|c";
|
|
|
|
StatsDInterface::$stats["shimmie.$type.cache-misses"] = $cache->get("__etc_cache_misses", -1)."|c";
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->_stats("overall");
|
|
|
|
|
2024-04-25 13:43:01 +00:00
|
|
|
if ($event->page_starts_with("post/view")) { # 40%
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->_stats("post-view");
|
2024-04-25 13:43:01 +00:00
|
|
|
} elseif ($event->page_starts_with("post/list")) { # 30%
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->_stats("post-list");
|
2024-04-25 13:43:01 +00:00
|
|
|
} elseif ($event->page_starts_with("user")) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->_stats("user");
|
2024-04-25 13:43:01 +00:00
|
|
|
} elseif ($event->page_starts_with("upload")) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->_stats("upload");
|
2024-04-25 13:43:01 +00:00
|
|
|
} elseif ($event->page_starts_with("rss")) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->_stats("rss");
|
2024-04-25 13:43:01 +00:00
|
|
|
} elseif ($event->page_starts_with("api")) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->_stats("api");
|
|
|
|
} else {
|
|
|
|
$this->_stats("other");
|
|
|
|
}
|
|
|
|
|
2024-01-15 13:40:18 +00:00
|
|
|
// @phpstan-ignore-next-line
|
|
|
|
if (STATSD_HOST) {
|
|
|
|
$this->send(STATSD_HOST, StatsDInterface::$stats, 1.0);
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
StatsDInterface::$stats = [];
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onUserCreation(UserCreationEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
StatsDInterface::$stats["shimmie_events.user_creations"] = "1|c";
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onDataUpload(DataUploadEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
StatsDInterface::$stats["shimmie_events.uploads"] = "1|c";
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onCommentPosting(CommentPostingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
StatsDInterface::$stats["shimmie_events.comments"] = "1|c";
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onImageInfoSet(ImageInfoSetEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
StatsDInterface::$stats["shimmie_events.info-sets"] = "1|c";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_priority(): int
|
|
|
|
{
|
|
|
|
return 99;
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
/**
|
|
|
|
* @param array<string, string> $data
|
|
|
|
*/
|
|
|
|
private function send(string $host, array $data, float $sampleRate = 1): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2012-06-22 17:34:23 +00:00
|
|
|
// sampling
|
2019-05-28 16:59:38 +00:00
|
|
|
$sampledData = [];
|
2012-06-22 17:34:23 +00:00
|
|
|
|
|
|
|
if ($sampleRate < 1) {
|
|
|
|
foreach ($data as $stat => $value) {
|
|
|
|
if ((mt_rand() / mt_getrandmax()) <= $sampleRate) {
|
|
|
|
$sampledData[$stat] = "$value|@$sampleRate";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$sampledData = $data;
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if (empty($sampledData)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-06-22 17:34:23 +00:00
|
|
|
|
|
|
|
// Wrap this in a try/catch - failures in any of this should be silently ignored
|
|
|
|
try {
|
2024-01-15 13:40:18 +00:00
|
|
|
$parts = explode(":", $host);
|
2012-06-22 17:34:23 +00:00
|
|
|
$host = $parts[0];
|
2020-01-26 19:44:36 +00:00
|
|
|
$port = (int)$parts[1];
|
2012-06-22 17:34:23 +00:00
|
|
|
$fp = fsockopen("udp://$host", $port, $errno, $errstr);
|
2023-11-11 21:49:12 +00:00
|
|
|
if (!$fp) {
|
2019-05-28 16:59:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-06-22 17:34:23 +00:00
|
|
|
foreach ($sampledData as $stat => $value) {
|
|
|
|
fwrite($fp, "$stat:$value");
|
|
|
|
}
|
|
|
|
fclose($fp);
|
2023-01-11 11:15:26 +00:00
|
|
|
} catch (\Exception $e) {
|
2014-04-19 05:18:49 +00:00
|
|
|
// ignore any failures.
|
2012-06-22 17:34:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|