From d3fadc0bd0256697e4a8e9a445d48d9620339f04 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 20 Oct 2023 22:39:41 +0300 Subject: [PATCH] stop calling spans scopes --- backend.php | 30 +++++++++---------- classes/article.php | 26 ++++++++-------- classes/counters.php | 16 +++++----- classes/digest.php | 4 +-- classes/diskcache.php | 47 ++++++++++++++++------------- classes/feeds.php | 38 ++++++++++++----------- classes/pluginhost.php | 41 ++++++++++++++----------- classes/pref/feeds.php | 8 ++--- classes/rpc.php | 4 +-- classes/rssutils.php | 30 ++++++++++--------- classes/sanitizer.php | 4 +-- classes/tracer.php | 38 ++++++++++++----------- classes/urlhelper.php | 68 ++++++++++++++++++++++-------------------- 13 files changed, 187 insertions(+), 167 deletions(-) diff --git a/backend.php b/backend.php index 220d0b7b7..b4e118d62 100644 --- a/backend.php +++ b/backend.php @@ -30,10 +30,10 @@ $op = (string)clean($op); $method = (string)clean($method); - $scope = Tracer::start(__FILE__); + $span = Tracer::start(__FILE__); - register_shutdown_function(function() use ($scope) { - $scope->end(); + register_shutdown_function(function() use ($span) { + $span->end(); }); startup_gettext(); @@ -55,7 +55,7 @@ header("Content-Type: text/json"); print Errors::to_json(Errors::E_UNAUTHORIZED); - $scope->setAttribute('error', Errors::E_UNAUTHORIZED); + $span->setAttribute('error', Errors::E_UNAUTHORIZED); return; } UserHelper::load_user_plugins($_SESSION["uid"]); @@ -64,7 +64,7 @@ if (Config::is_migration_needed()) { print Errors::to_json(Errors::E_SCHEMA_MISMATCH); - $scope->setAttribute('error', Errors::E_SCHEMA_MISMATCH); + $span->setAttribute('error', Errors::E_SCHEMA_MISMATCH); return; } @@ -127,7 +127,7 @@ header("Content-Type: text/json"); print Errors::to_json(Errors::E_UNAUTHORIZED); - $scope->setAttribute('error', Errors::E_UNAUTHORIZED); + $span->setAttribute('error', Errors::E_UNAUTHORIZED); return; } @@ -140,16 +140,16 @@ } if (implements_interface($handler, 'IHandler')) { - $scope->addEvent("construct/$op"); + $span->addEvent("construct/$op"); $handler->__construct($_REQUEST); if (validate_csrf($csrf_token) || $handler->csrf_ignore($method)) { - $scope->addEvent("before/$method"); + $span->addEvent("before/$method"); $before = $handler->before($method); if ($before) { - $scope->addEvent("method/$method"); + $span->addEvent("method/$method"); if ($method && method_exists($handler, $method)) { $reflection = new ReflectionMethod($handler, $method); @@ -159,7 +159,7 @@ user_error("Refusing to invoke method $method of handler $op which has required parameters.", E_USER_WARNING); header("Content-Type: text/json"); - $scope->setAttribute('error', Errors::E_UNAUTHORIZED); + $span->setAttribute('error', Errors::E_UNAUTHORIZED); print Errors::to_json(Errors::E_UNAUTHORIZED); } } else { @@ -168,19 +168,19 @@ } else { header("Content-Type: text/json"); - $scope->setAttribute('error', Errors::E_UNKNOWN_METHOD); + $span->setAttribute('error', Errors::E_UNKNOWN_METHOD); print Errors::to_json(Errors::E_UNKNOWN_METHOD, ["info" => get_class($handler) . "->$method"]); } } - $scope->addEvent("after/$method"); + $span->addEvent("after/$method"); $handler->after(); return; } else { header("Content-Type: text/json"); print Errors::to_json(Errors::E_UNAUTHORIZED); - $scope->setAttribute('error', Errors::E_UNAUTHORIZED); + $span->setAttribute('error', Errors::E_UNAUTHORIZED); return; } } else { @@ -188,7 +188,7 @@ header("Content-Type: text/json"); print Errors::to_json(Errors::E_UNAUTHORIZED); - $scope->setAttribute('error', Errors::E_UNAUTHORIZED); + $span->setAttribute('error', Errors::E_UNAUTHORIZED); return; } } @@ -197,4 +197,4 @@ header("Content-Type: text/json"); print Errors::to_json(Errors::E_UNKNOWN_METHOD, [ "info" => (isset($handler) ? get_class($handler) : "UNKNOWN:".$op) . "->$method"]); - $scope->setAttribute('error', Errors::E_UNKNOWN_METHOD); + $span->setAttribute('error', Errors::E_UNKNOWN_METHOD); diff --git a/classes/article.php b/classes/article.php index c14804485..2aeb31b99 100755 --- a/classes/article.php +++ b/classes/article.php @@ -298,7 +298,7 @@ class Article extends Handler_Protected { * @return array{'formatted': string, 'entries': array>} */ static function _format_enclosures(int $id, bool $always_display_enclosures, string $article_content, bool $hide_images = false): array { - $scope = Tracer::start(__METHOD__); + $span = Tracer::start(__METHOD__); $enclosures = self::_get_enclosures($id); $enclosures_formatted = ""; @@ -326,7 +326,7 @@ class Article extends Handler_Protected { $enclosures_formatted, $enclosures, $id, $always_display_enclosures, $article_content, $hide_images); if (!empty($enclosures_formatted)) { - $scope->end(); + $span->end(); return [ 'formatted' => $enclosures_formatted, 'entries' => [] @@ -370,7 +370,7 @@ class Article extends Handler_Protected { } } - $scope->end(); + $span->end(); return $rv; } @@ -378,7 +378,7 @@ class Article extends Handler_Protected { * @return array */ static function _get_tags(int $id, int $owner_uid = 0, ?string $tag_cache = null): array { - $scope = Tracer::start(__METHOD__); + $span = Tracer::start(__METHOD__); $a_id = $id; @@ -427,7 +427,7 @@ class Article extends Handler_Protected { $sth->execute([$tags_str, $id, $owner_uid]); } - $scope->end(); + $span->end(); return $tags; } @@ -522,7 +522,7 @@ class Article extends Handler_Protected { * @return array> */ static function _get_labels(int $id, ?int $owner_uid = null): array { - $scope = Tracer::start(__METHOD__, []); + $span = Tracer::start(__METHOD__); $rv = array(); @@ -569,7 +569,7 @@ class Article extends Handler_Protected { else Labels::update_cache($owner_uid, $id, array("no-labels" => 1)); - $scope->end(); + $span->end(); return $rv; } @@ -581,7 +581,7 @@ class Article extends Handler_Protected { * @return array */ static function _get_image(array $enclosures, string $content, string $site_url, array $headline) { - $scope = Tracer::start(__METHOD__); + $span = Tracer::start(__METHOD__); $article_image = ""; $article_stream = ""; @@ -660,7 +660,7 @@ class Article extends Handler_Protected { if ($article_stream && $cache->exists(sha1($article_stream))) $article_stream = $cache->get_url(sha1($article_stream)); - $scope->end(); + $span->end(); return [$article_image, $article_stream, $article_kind]; } @@ -675,7 +675,7 @@ class Article extends Handler_Protected { if (count($article_ids) == 0) return []; - $scope = Tracer::start(__METHOD__); + $span = Tracer::start(__METHOD__); $entries = ORM::for_table('ttrss_entries') ->table_alias('e') @@ -696,7 +696,7 @@ class Article extends Handler_Protected { } } - $scope->end(); + $span->end(); return array_unique($rv); } @@ -709,7 +709,7 @@ class Article extends Handler_Protected { if (count($article_ids) == 0) return []; - $scope = Tracer::start(__METHOD__); + $span = Tracer::start(__METHOD__); $entries = ORM::for_table('ttrss_entries') ->table_alias('e') @@ -723,7 +723,7 @@ class Article extends Handler_Protected { array_push($rv, $entry->feed_id); } - $scope->end(); + $span->end(); return array_unique($rv); } diff --git a/classes/counters.php b/classes/counters.php index 1f5988568..948e6ee1d 100644 --- a/classes/counters.php +++ b/classes/counters.php @@ -145,7 +145,7 @@ class Counters { * @return array> */ private static function get_feeds(array $feed_ids = null): array { - $scope = Tracer::start(__METHOD__); + $span = Tracer::start(__METHOD__); $ret = []; @@ -212,7 +212,7 @@ class Counters { } - $scope->end(); + $span->end(); return $ret; } @@ -221,7 +221,7 @@ class Counters { * @return array> */ private static function get_global(): array { - $scope = Tracer::start(__METHOD__); + $span = Tracer::start(__METHOD__); $ret = [ [ @@ -239,7 +239,7 @@ class Counters { "counter" => $subcribed_feeds ]); - $scope->end(); + $span->end(); return $ret; } @@ -248,7 +248,7 @@ class Counters { * @return array> */ private static function get_virt(): array { - $scope = Tracer::start(__METHOD__); + $span = Tracer::start(__METHOD__); $ret = []; @@ -295,7 +295,7 @@ class Counters { } } - $scope->end(); + $span->end(); return $ret; } @@ -304,7 +304,7 @@ class Counters { * @return array> */ static function get_labels(array $label_ids = null): array { - $scope = Tracer::start(__METHOD__); + $span = Tracer::start(__METHOD__); $ret = []; @@ -356,7 +356,7 @@ class Counters { array_push($ret, $cv); } - $scope->end(); + $span->end(); return $ret; } } diff --git a/classes/digest.php b/classes/digest.php index 02fa76bf0..27009530f 100644 --- a/classes/digest.php +++ b/classes/digest.php @@ -2,7 +2,7 @@ class Digest { static function send_headlines_digests(): void { - $scope = Tracer::start(__METHOD__); + $span = Tracer::start(__METHOD__); $user_limit = 15; // amount of users to process (e.g. emails to send out) $limit = 1000; // maximum amount of headlines to include @@ -77,7 +77,7 @@ class Digest } } - $scope->end(); + $span->end(); Debug::log("All done."); } diff --git a/classes/diskcache.php b/classes/diskcache.php index 7bc5835a4..290fbd9c3 100644 --- a/classes/diskcache.php +++ b/classes/diskcache.php @@ -221,9 +221,11 @@ class DiskCache implements Cache_Adapter { } public function remove(string $filename): bool { - $scope = Tracer::start(__METHOD__, ['filename' => $filename]); + $span = Tracer::start(__METHOD__); + $span->setAttribute('file.name', $filename); + $rc = $this->adapter->remove($filename); - $scope->end(); + $span->end(); return $rc; } @@ -249,8 +251,8 @@ class DiskCache implements Cache_Adapter { } public function exists(string $filename): bool { - $scope = OpenTelemetry\API\Trace\Span::getCurrent(); - $scope->addEvent("DiskCache::exists: $filename"); + $span = OpenTelemetry\API\Trace\Span::getCurrent(); + $span->addEvent("DiskCache::exists: $filename"); $rc = $this->adapter->exists(basename($filename)); @@ -261,9 +263,11 @@ class DiskCache implements Cache_Adapter { * @return int|false -1 if the file doesn't exist, false if an error occurred, size in bytes otherwise */ public function get_size(string $filename) { - $scope = Tracer::start(__METHOD__, ['filename' => $filename]); + $span = Tracer::start(__METHOD__); + $span->setAttribute('file.name', $filename); + $rc = $this->adapter->get_size(basename($filename)); - $scope->end(); + $span->end(); return $rc; } @@ -274,9 +278,9 @@ class DiskCache implements Cache_Adapter { * @return int|false Bytes written or false if an error occurred. */ public function put(string $filename, $data) { - $scope = Tracer::start(__METHOD__); + $span = Tracer::start(__METHOD__); $rc = $this->adapter->put(basename($filename), $data); - $scope->end(); + $span->end(); return $rc; } @@ -322,7 +326,8 @@ class DiskCache implements Cache_Adapter { } public function send(string $filename) { - $scope = Tracer::start(__METHOD__, ['filename' => $filename]); + $span = Tracer::start(__METHOD__); + $span->setAttribute('file.name', $filename); $filename = basename($filename); @@ -330,8 +335,8 @@ class DiskCache implements Cache_Adapter { header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); echo "File not found."; - $scope->setAttribute('error', '404 not found'); - $scope->end(); + $span->setAttribute('error', '404 not found'); + $span->end(); return false; } @@ -341,8 +346,8 @@ class DiskCache implements Cache_Adapter { if (($_SERVER['HTTP_IF_MODIFIED_SINCE'] ?? '') == $gmt_modified || ($_SERVER['HTTP_IF_NONE_MATCH'] ?? '') == $file_mtime) { header('HTTP/1.1 304 Not Modified'); - $scope->setAttribute('error', '304 not modified'); - $scope->end(); + $span->setAttribute('error', '304 not modified'); + $span->end(); return false; } @@ -361,8 +366,8 @@ class DiskCache implements Cache_Adapter { print "Stored file has disallowed content type ($mimetype)"; - $scope->setAttribute('error', '400 disallowed content type'); - $scope->end(); + $span->setAttribute('error', '400 disallowed content type'); + $span->end(); return false; } @@ -384,11 +389,11 @@ class DiskCache implements Cache_Adapter { header_remove("Pragma"); - $scope->setAttribute('mimetype', $mimetype); + $span->setAttribute('mimetype', $mimetype); $rc = $this->adapter->send($filename); - $scope->end(); + $span->end(); return $rc; } @@ -419,13 +424,13 @@ class DiskCache implements Cache_Adapter { // plugins work on original source URLs used before caching // NOTE: URLs should be already absolutized because this is called after sanitize() static public function rewrite_urls(string $str): string { - $scope = OpenTelemetry\API\Trace\Span::getCurrent(); - $scope->addEvent("DiskCache::rewrite_urls"); + $span = OpenTelemetry\API\Trace\Span::getCurrent(); + $span->addEvent("DiskCache::rewrite_urls"); $res = trim($str); if (!$res) { - $scope->end(); + $span->end(); return ''; } @@ -439,7 +444,7 @@ class DiskCache implements Cache_Adapter { $need_saving = false; foreach ($entries as $entry) { - $scope->addEvent("entry: " . $entry->tagName); + $span->addEvent("entry: " . $entry->tagName); foreach (array('src', 'poster') as $attr) { if ($entry->hasAttribute($attr)) { diff --git a/classes/feeds.php b/classes/feeds.php index b99be3d23..a97ac221f 100755 --- a/classes/feeds.php +++ b/classes/feeds.php @@ -65,7 +65,8 @@ class Feeds extends Handler_Protected { $disable_cache = false; - $scope = Tracer::start(__METHOD__, [], func_get_args()); + $span = Tracer::start(__METHOD__); + $span->setAttribute('func.args', json_encode(func_get_args())); $reply = []; $rgba_cache = []; @@ -168,7 +169,7 @@ class Feeds extends Handler_Protected { $reply['search_query'] = [$search, $search_language]; $reply['vfeed_group_enabled'] = $vfeed_group_enabled; - $scope->addEvent('plugin_menu_items'); + $span->addEvent('plugin_menu_items'); $plugin_menu_items = ""; PluginHost::getInstance()->chain_hooks_callback(PluginHost::HOOK_HEADLINE_TOOLBAR_SELECT_MENU_ITEM2, @@ -202,13 +203,13 @@ class Feeds extends Handler_Protected { }, $feed, $cat_view, $qfh_ret); - $scope->addEvent('articles'); + $span->addEvent('articles'); $headlines_count = 0; if ($result instanceof PDOStatement) { while ($line = $result->fetch(PDO::FETCH_ASSOC)) { - $scope->addEvent('article: ' . $line['id']); + $span->addEvent('article: ' . $line['id']); ++$headlines_count; @@ -368,7 +369,7 @@ class Feeds extends Handler_Protected { //setting feed headline background color, needs to change text color based on dark/light $fav_color = $line['favicon_avg_color'] ?? false; - $scope->addEvent("colors"); + $span->addEvent("colors"); require_once "colors.php"; @@ -384,7 +385,7 @@ class Feeds extends Handler_Protected { $line['feed_bg_color'] = 'rgba(' . implode(",", $rgba_cache[$feed_id]) . ',0.3)'; } - $scope->addEvent("HOOK_RENDER_ARTICLE_CDM"); + $span->addEvent("HOOK_RENDER_ARTICLE_CDM"); PluginHost::getInstance()->chain_hooks_callback(PluginHost::HOOK_RENDER_ARTICLE_CDM, function ($result, $plugin) use (&$line) { @@ -463,7 +464,7 @@ class Feeds extends Handler_Protected { } } - $scope->end(); + $span->end(); return array($topmost_article_ids, $headlines_count, $feed, $disable_cache, $reply); } @@ -977,9 +978,9 @@ class Feeds extends Handler_Protected { * @throws PDOException */ static function _get_counters($feed, bool $is_cat = false, bool $unread_only = false, ?int $owner_uid = null): int { - $scope = OpenTelemetry\API\Trace\Span::getCurrent(); + $span = OpenTelemetry\API\Trace\Span::getCurrent(); - $scope->addEvent(__METHOD__ . ": $feed ($is_cat)"); + $span->addEvent(__METHOD__ . ": $feed ($is_cat)"); $n_feed = (int) $feed; $need_entries = false; @@ -1003,14 +1004,14 @@ class Feeds extends Handler_Protected { $handler = PluginHost::getInstance()->get_feed_handler($feed_id); if (implements_interface($handler, 'IVirtualFeed')) { /** @var IVirtualFeed $handler */ - //$scope->end(); + //$span->end(); return $handler->get_unread($feed_id); } else { - //$scope->end(); + //$span->end(); return 0; } } else if ($n_feed == Feeds::FEED_RECENTLY_READ) { - //$scope->end(); + //$span->end(); return 0; // tags } else if ($feed != "0" && $n_feed == 0) { @@ -1024,7 +1025,7 @@ class Feeds extends Handler_Protected { $row = $sth->fetch(); // Handle 'SUM()' returning null if there are no results - //$scope->end(); + //$span->end(); return $row["count"] ?? 0; } else if ($n_feed == Feeds::FEED_STARRED) { @@ -1058,7 +1059,7 @@ class Feeds extends Handler_Protected { $label_id = Labels::feed_to_label_id($feed); - //$scope->end(); + //$span->end(); return self::_get_label_unread($label_id, $owner_uid); } @@ -1078,7 +1079,7 @@ class Feeds extends Handler_Protected { $sth->execute([$owner_uid]); $row = $sth->fetch(); - //$scope->end(); + //$span->end(); return $row["unread"]; } else { @@ -1091,7 +1092,7 @@ class Feeds extends Handler_Protected { $sth->execute([$feed, $owner_uid]); $row = $sth->fetch(); - //$scope->end(); + //$span->end(); return $row["unread"]; } } @@ -1489,7 +1490,8 @@ class Feeds extends Handler_Protected { */ static function _get_headlines($params): array { - $scope = Tracer::start(__METHOD__, [], func_get_args()); + $span = Tracer::start(__METHOD__); + $span->setAttribute('func.args', json_encode(func_get_args())); $pdo = Db::pdo(); @@ -1983,7 +1985,7 @@ class Feeds extends Handler_Protected { $res = $pdo->query($query); } - $scope->end(); + $span->end(); return array($res, $feed_title, $feed_site_url, $last_error, $last_updated, $search_words, $first_id, $vfeed_query_part != "", $query_error_override); } diff --git a/classes/pluginhost.php b/classes/pluginhost.php index c2a949f5c..7778c008f 100755 --- a/classes/pluginhost.php +++ b/classes/pluginhost.php @@ -339,15 +339,15 @@ class PluginHost { */ function chain_hooks_callback(string $hook, Closure $callback, &...$args): void { $method = strtolower((string)$hook); - $scope = OpenTelemetry\API\Trace\Span::getCurrent(); - $scope->addEvent("chain_hooks_callback: $hook"); + $span = OpenTelemetry\API\Trace\Span::getCurrent(); + $span->addEvent("chain_hooks_callback: $hook"); foreach ($this->get_hooks((string)$hook) as $plugin) { //Debug::log("invoking: " . get_class($plugin) . "->$hook()", Debug::$LOG_VERBOSE); - //$p_scope = Tracer::start("$hook - " . get_class($plugin)); + //$p_span = Tracer::start("$hook - " . get_class($plugin)); - $scope->addEvent("$hook - " . get_class($plugin)); + $span->addEvent("$hook - " . get_class($plugin)); try { if ($callback($plugin->$method(...$args), $plugin)) @@ -358,10 +358,10 @@ class PluginHost { user_error($err, E_USER_WARNING); } - //$p_scope->end(); + //$p_span->end(); } - //$scope->end(); + //$span->end(); } /** @@ -427,6 +427,8 @@ class PluginHost { * @param PluginHost::KIND_* $kind */ function load_all(int $kind, int $owner_uid = null, bool $skip_init = false): void { + $span = Tracer::start(__METHOD__); + $span->setAttribute('func.args', json_encode(func_get_args())); $plugins = [...(glob("plugins/*") ?: []), ...(glob("plugins.local/*") ?: [])]; $plugins = array_filter($plugins, "is_dir"); @@ -435,13 +437,16 @@ class PluginHost { asort($plugins); $this->load(join(",", $plugins), (int)$kind, $owner_uid, $skip_init); + + $span->end(); } /** * @param PluginHost::KIND_* $kind */ function load(string $classlist, int $kind, int $owner_uid = null, bool $skip_init = false): void { - $scope = Tracer::start(__METHOD__); + $span = Tracer::start(__METHOD__); + $span->setAttribute('func.args', json_encode(func_get_args())); $plugins = explode(",", $classlist); @@ -451,7 +456,7 @@ class PluginHost { $class = trim($class); $class_file = strtolower(basename(clean($class))); - $scope->addEvent("$class_file: load"); + $span->addEvent("$class_file: load"); // try system plugin directory first $file = dirname(__DIR__) . "/plugins/$class_file/init.php"; @@ -478,7 +483,7 @@ class PluginHost { $_SESSION["safe_mode"] = 1; - $scope->setAttribute('error', 'plugin is blacklisted'); + $span->setAttribute('error', 'plugin is blacklisted'); continue; } @@ -490,7 +495,7 @@ class PluginHost { } catch (Error $err) { user_error($err, E_USER_WARNING); - $scope->setAttribute('error', $err); + $span->setAttribute('error', $err); continue; } @@ -501,7 +506,7 @@ class PluginHost { if ($plugin_api < self::API_VERSION) { user_error("Plugin $class is not compatible with current API version (need: " . self::API_VERSION . ", got: $plugin_api)", E_USER_WARNING); - $scope->setAttribute('error', 'plugin is not compatible with API version'); + $span->setAttribute('error', 'plugin is not compatible with API version'); continue; } @@ -510,7 +515,7 @@ class PluginHost { _bind_textdomain_codeset($class, "UTF-8"); } - $scope->addEvent("$class_file: initialize"); + $span->addEvent("$class_file: initialize"); try { switch ($kind) { @@ -541,7 +546,7 @@ class PluginHost { } $this->load_data(); - $scope->end(); + $span->end(); } function is_system(Plugin $plugin): bool { @@ -634,8 +639,8 @@ class PluginHost { } private function load_data(): void { - $scope = OpenTelemetry\API\Trace\Span::getCurrent(); - $scope->addEvent('load plugin data'); + $span = OpenTelemetry\API\Trace\Span::getCurrent(); + $span->addEvent('load plugin data'); if ($this->owner_uid && !$this->data_loaded && Config::get_schema_version() > 100) { $sth = $this->pdo->prepare("SELECT name, content FROM ttrss_plugin_storage @@ -643,7 +648,7 @@ class PluginHost { $sth->execute([$this->owner_uid]); while ($line = $sth->fetch()) { - $scope->addEvent($line["name"] . ': unserialize'); + $span->addEvent($line["name"] . ': unserialize'); $this->storage[$line["name"]] = unserialize($line["content"]); } @@ -654,8 +659,8 @@ class PluginHost { private function save_data(string $plugin): void { if ($this->owner_uid) { - $scope = OpenTelemetry\API\Trace\Span::getCurrent(); - $scope->addEvent(__METHOD__ . ": $plugin"); + $span = OpenTelemetry\API\Trace\Span::getCurrent(); + $span->addEvent(__METHOD__ . ": $plugin"); if (!$this->pdo_data) $this->pdo_data = Db::instance()->pdo_connect(); diff --git a/classes/pref/feeds.php b/classes/pref/feeds.php index a2b55b88c..fa56f6d1a 100755 --- a/classes/pref/feeds.php +++ b/classes/pref/feeds.php @@ -1080,8 +1080,8 @@ class Pref_Feeds extends Handler_Protected { * @return array */ private function feedlist_init_cat(int $cat_id): array { - $scope = OpenTelemetry\API\Trace\Span::getCurrent(); - $scope->addEvent(__METHOD__ . ": $cat_id"); + $span = OpenTelemetry\API\Trace\Span::getCurrent(); + $span->addEvent(__METHOD__ . ": $cat_id"); return [ 'id' => 'CAT:' . $cat_id, @@ -1097,8 +1097,8 @@ class Pref_Feeds extends Handler_Protected { * @return array */ private function feedlist_init_feed(int $feed_id, ?string $title = null, bool $unread = false, string $error = '', string $updated = ''): array { - $scope = OpenTelemetry\API\Trace\Span::getCurrent(); - $scope->addEvent(__METHOD__ . ": $feed_id"); + $span = OpenTelemetry\API\Trace\Span::getCurrent(); + $span->addEvent(__METHOD__ . ": $feed_id"); if (!$title) $title = Feeds::_get_title($feed_id, false); diff --git a/classes/rpc.php b/classes/rpc.php index 929c6bda0..092a0e25f 100755 --- a/classes/rpc.php +++ b/classes/rpc.php @@ -106,7 +106,7 @@ class RPC extends Handler_Protected { } function getAllCounters(): void { - $scope = Tracer::start(__METHOD__); + $span = Tracer::start(__METHOD__); @$seq = (int) $_REQUEST['seq']; @@ -134,7 +134,7 @@ class RPC extends Handler_Protected { 'seq' => $seq ]; - $scope->end(); + $span->end(); print json_encode($reply); } diff --git a/classes/rssutils.php b/classes/rssutils.php index 29368da1c..9150fd466 100755 --- a/classes/rssutils.php +++ b/classes/rssutils.php @@ -69,7 +69,7 @@ class RSSUtils { * @param array $options */ static function update_daemon_common(int $limit = 0, array $options = []): int { - $scope = Tracer::start(__METHOD__); + $span = Tracer::start(__METHOD__); if (!$limit) $limit = Config::get(Config::DAEMON_FEED_LIMIT); @@ -286,7 +286,7 @@ class RSSUtils { // Send feed digests by email if needed. Digest::send_headlines_digests(); - $scope->end(); + $span->end(); return $nf; } @@ -354,7 +354,9 @@ class RSSUtils { static function update_rss_feed(int $feed, bool $no_cache = false, bool $html_output = false) : bool { - $scope = Tracer::start(__METHOD__, [], func_get_args()); + $span = Tracer::start(__METHOD__); + $span->setAttribute('func.args', json_encode(func_get_args())); + Debug::enable_html($html_output); Debug::log("start", Debug::LOG_VERBOSE); @@ -390,19 +392,19 @@ class RSSUtils { if ($user) { if ($user->access_level == UserHelper::ACCESS_LEVEL_READONLY) { Debug::log("error: denied update for $feed: permission denied by owner access level"); - $scope->end(); + $span->end(); return false; } } else { // this would indicate database corruption of some kind Debug::log("error: owner not found for feed: $feed"); - $scope->end(); + $span->end(); return false; } } else { Debug::log("error: feeds table record not found for feed: $feed"); - $scope->end(); + $span->end(); return false; } @@ -561,7 +563,7 @@ class RSSUtils { $feed_obj->save(); } - $scope->end(); + $span->end(); return $error_message == ""; } @@ -703,7 +705,7 @@ class RSSUtils { ]); $feed_obj->save(); - $scope->end(); + $span->end(); return true; // no articles } @@ -712,7 +714,7 @@ class RSSUtils { $tstart = time(); foreach ($items as $item) { - $a_scope = Tracer::start('article'); + $a_span = Tracer::start('article'); $pdo->beginTransaction(); @@ -1305,7 +1307,7 @@ class RSSUtils { Debug::log("article processed.", Debug::LOG_VERBOSE); $pdo->commit(); - $a_scope->end(); + $a_span->end(); } Debug::log(Debug::SEPARATOR, Debug::LOG_VERBOSE); @@ -1346,12 +1348,12 @@ class RSSUtils { unset($rss); Debug::log("update failed.", Debug::LOG_VERBOSE); - $scope->end(); + $span->end(); return false; } Debug::log("update done.", Debug::LOG_VERBOSE); - $scope->end(); + $span->end(); return true; } @@ -1516,7 +1518,7 @@ class RSSUtils { * @return array> An array of filter action arrays with keys "type" and "param" */ static function get_article_filters(array $filters, string $title, string $content, string $link, string $author, array $tags, array &$matched_rules = null, array &$matched_filters = null): array { - $scope = Tracer::start(__METHOD__); + $span = Tracer::start(__METHOD__); $matches = array(); @@ -1604,7 +1606,7 @@ class RSSUtils { } } - $scope->end(); + $span->end(); return $matches; } diff --git a/classes/sanitizer.php b/classes/sanitizer.php index 68bb91b9f..a7bea9e5f 100644 --- a/classes/sanitizer.php +++ b/classes/sanitizer.php @@ -63,8 +63,8 @@ class Sanitizer { * @return false|string The HTML, or false if an error occurred. */ public static function sanitize(string $str, ?bool $force_remove_images = false, int $owner = null, string $site_url = null, array $highlight_words = null, int $article_id = null) { - $scope = OpenTelemetry\API\Trace\Span::getCurrent(); - $scope->addEvent("Sanitizer::sanitize"); + $span = OpenTelemetry\API\Trace\Span::getCurrent(); + $span->addEvent("Sanitizer::sanitize"); if (!$owner && isset($_SESSION["uid"])) $owner = $_SESSION["uid"]; diff --git a/classes/tracer.php b/classes/tracer.php index eb0ee5eaa..e36b9f947 100644 --- a/classes/tracer.php +++ b/classes/tracer.php @@ -8,6 +8,11 @@ use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator; use OpenTelemetry\API\Trace\Span; use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter; +use OpenTelemetry\SDK\Resource\ResourceInfoFactory; +use OpenTelemetry\SDK\Resource\ResourceInfo; +use OpenTelemetry\SDK\Common\Attribute\Attributes; +use OpenTelemetry\SemConv\ResourceAttributes; + class Tracer { /** @var Tracer $instance */ private static $instance; @@ -25,18 +30,27 @@ class Tracer { $exporter = new InMemoryExporter(); } - $tracerProvider = new TracerProvider(new SimpleSpanProcessor($exporter)); + $resource = ResourceInfoFactory::emptyResource()->merge( + ResourceInfo::create(Attributes::create( + [ResourceAttributes::SERVICE_NAME => Config::get(Config::OPENTELEMETRY_SERVICE)] + ), ResourceAttributes::SCHEMA_URL), + ); + + $tracerProvider = new TracerProvider(new SimpleSpanProcessor($exporter), null, $resource); + $this->tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php'); $context = TraceContextPropagator::getInstance()->extract(getallheaders()); - $span = $this->tracer->spanBuilder(Config::get(Config::OPENTELEMETRY_SERVICE)) + $span = $this->tracer->spanBuilder('root') ->setParent($context) + ->setAttribute('http.request', json_encode($_REQUEST)) ->startSpan(); - $span->activate(); + $scope = $span->activate(); - register_shutdown_function(function() use ($span, $tracerProvider) { + register_shutdown_function(function() use ($span, $tracerProvider, $scope) { $span->end(); + $scope->detach(); $tracerProvider->shutdown(); }); @@ -44,19 +58,11 @@ class Tracer { /** * @param string $name - * @param array|array> $tags - * @param array $args * @return OpenTelemetry\API\Trace\SpanInterface */ - private function _start(string $name, array $tags = [], array $args = []) { + private function _start(string $name) { $span = $this->tracer->spanBuilder($name)->startSpan(); - foreach ($tags as $k => $v) { - $span->setAttribute($k, $v); - } - - $span->setAttribute("func.args", json_encode($args)); - $span->activate(); return $span; @@ -64,12 +70,10 @@ class Tracer { /** * @param string $name - * @param array|array> $tags - * @param array $args * @return OpenTelemetry\API\Trace\SpanInterface */ - public static function start(string $name, array $tags = [], array $args = []) { - return self::get_instance()->_start($name, $tags, $args); + public static function start(string $name) { + return self::get_instance()->_start($name); } public static function get_instance() : Tracer { diff --git a/classes/urlhelper.php b/classes/urlhelper.php index ac585dd54..dbbde55e6 100644 --- a/classes/urlhelper.php +++ b/classes/urlhelper.php @@ -185,12 +185,13 @@ class UrlHelper { * @return false|string */ static function resolve_redirects(string $url, int $timeout, int $nest = 0) { - $scope = Tracer::start(__METHOD__, ['url' => $url]); + $span = Tracer::start(__METHOD__); + $span->setAttribute('func.args', json_encode(func_get_args())); // too many redirects if ($nest > 10) { - $scope->setAttribute('error', 'too many redirects'); - $scope->end(); + $span->setAttribute('error', 'too many redirects'); + $span->end(); return false; } @@ -226,12 +227,12 @@ class UrlHelper { } } - $scope->end(); + $span->end(); return $url; } - $scope->setAttribute('error', 'request failed'); - $scope->end(); + $span->setAttribute('error', 'request failed'); + $span->end(); // request failed? return false; } @@ -279,7 +280,8 @@ class UrlHelper { } $url = $options["url"]; - $scope = Tracer::start(__METHOD__, ['url' => $url]); + $span = Tracer::start(__METHOD__); + $span->setAttribute('func.args', json_encode(func_get_args())); $type = isset($options["type"]) ? $options["type"] : false; $login = isset($options["login"]) ? $options["login"] : false; @@ -303,8 +305,8 @@ class UrlHelper { if (!$url) { self::$fetch_last_error = "Requested URL failed extended validation."; - $scope->setAttribute('error', self::$fetch_last_error); - $scope->end(); + $span->setAttribute('error', self::$fetch_last_error); + $span->end(); return false; } @@ -314,8 +316,8 @@ class UrlHelper { if (!$ip_addr || strpos($ip_addr, "127.") === 0) { self::$fetch_last_error = "URL hostname failed to resolve or resolved to a loopback address ($ip_addr)"; - $scope->setAttribute('error', self::$fetch_last_error); - $scope->end(); + $span->setAttribute('error', self::$fetch_last_error); + $span->end(); return false; } @@ -327,8 +329,8 @@ class UrlHelper { if (!$ch) { self::$fetch_last_error = "curl_init() failed"; - $scope->setAttribute('error', self::$fetch_last_error); - $scope->end(); + $span->setAttribute('error', self::$fetch_last_error); + $span->end(); return false; } @@ -432,8 +434,8 @@ class UrlHelper { if (!self::validate(self::$fetch_effective_url, true)) { self::$fetch_last_error = "URL received after redirection failed extended validation."; - $scope->setAttribute('error', self::$fetch_last_error); - $scope->end(); + $span->setAttribute('error', self::$fetch_last_error); + $span->end(); return false; } @@ -442,8 +444,8 @@ class UrlHelper { if (!self::$fetch_effective_ip_addr || strpos(self::$fetch_effective_ip_addr, "127.") === 0) { self::$fetch_last_error = "URL hostname received after redirection failed to resolve or resolved to a loopback address (".self::$fetch_effective_ip_addr.")"; - $scope->setAttribute('error', self::$fetch_last_error); - $scope->end(); + $span->setAttribute('error', self::$fetch_last_error); + $span->end(); return false; } @@ -460,8 +462,8 @@ class UrlHelper { self::$fetch_last_error_content = $contents; curl_close($ch); - $scope->setAttribute('error', self::$fetch_last_error); - $scope->end(); + $span->setAttribute('error', self::$fetch_last_error); + $span->end(); return false; } @@ -473,8 +475,8 @@ class UrlHelper { } curl_close($ch); - $scope->setAttribute('error', self::$fetch_last_error); - $scope->end(); + $span->setAttribute('error', self::$fetch_last_error); + $span->end(); return false; } @@ -488,7 +490,7 @@ class UrlHelper { if ($tmp) $contents = $tmp; } - $scope->end(); + $span->end(); return $contents; } else { @@ -543,8 +545,8 @@ class UrlHelper { if (!self::validate(self::$fetch_effective_url, true)) { self::$fetch_last_error = "URL received after redirection failed extended validation."; - $scope->setAttribute('error', self::$fetch_last_error); - $scope->end(); + $span->setAttribute('error', self::$fetch_last_error); + $span->end(); return false; } @@ -553,8 +555,8 @@ class UrlHelper { if (!self::$fetch_effective_ip_addr || strpos(self::$fetch_effective_ip_addr, "127.") === 0) { self::$fetch_last_error = "URL hostname received after redirection failed to resolve or resolved to a loopback address (".self::$fetch_effective_ip_addr.")"; - $scope->setAttribute('error', self::$fetch_last_error); - $scope->end(); + $span->setAttribute('error', self::$fetch_last_error); + $span->end(); return false; } @@ -563,8 +565,8 @@ class UrlHelper { if ($data === false) { self::$fetch_last_error = "'file_get_contents' failed."; - $scope->setAttribute('error', self::$fetch_last_error); - $scope->end(); + $span->setAttribute('error', self::$fetch_last_error); + $span->end(); return false; } @@ -600,8 +602,8 @@ class UrlHelper { self::$fetch_last_error_content = $data; - $scope->setAttribute('error', self::$fetch_last_error); - $scope->end(); + $span->setAttribute('error', self::$fetch_last_error); + $span->end(); return false; } @@ -614,13 +616,13 @@ class UrlHelper { if ($tmp) $data = $tmp; } - $scope->end(); + $span->end(); return $data; } else { self::$fetch_last_error = 'Successful response, but no content was received.'; - $scope->setAttribute('error', self::$fetch_last_error); - $scope->end(); + $span->setAttribute('error', self::$fetch_last_error); + $span->end(); return false; } }