diff options
author | Andrew Dolgov <[email protected]> | 2023-10-20 17:12:29 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2023-10-20 21:13:39 +0300 |
commit | cdd7ad020e165fe680703b6d3319b908b682fb7a (patch) | |
tree | b51eb09b7b4587e8fbc5624ac8d88d28cfcd0b04 /classes | |
parent | 45a9ff0c88cbd33892ff16ab837e9059937d656e (diff) |
jaeger-client -> opentelemetry
Diffstat (limited to 'classes')
-rwxr-xr-x | classes/article.php | 14 | ||||
-rw-r--r-- | classes/config.php | 8 | ||||
-rw-r--r-- | classes/counters.php | 8 | ||||
-rw-r--r-- | classes/digest.php | 2 | ||||
-rw-r--r-- | classes/diskcache.php | 38 | ||||
-rwxr-xr-x | classes/feeds.php | 38 | ||||
-rwxr-xr-x | classes/pluginhost.php | 43 | ||||
-rwxr-xr-x | classes/pref/feeds.php | 8 | ||||
-rwxr-xr-x | classes/rpc.php | 2 | ||||
-rwxr-xr-x | classes/rssutils.php | 20 | ||||
-rw-r--r-- | classes/sanitizer.php | 5 | ||||
-rw-r--r-- | classes/tracer.php | 79 | ||||
-rw-r--r-- | classes/urlhelper.php | 62 |
13 files changed, 164 insertions, 163 deletions
diff --git a/classes/article.php b/classes/article.php index 63469ccd2..c14804485 100755 --- a/classes/article.php +++ b/classes/article.php @@ -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->close(); + $scope->end(); return [ 'formatted' => $enclosures_formatted, 'entries' => [] @@ -370,7 +370,7 @@ class Article extends Handler_Protected { } } - $scope->close(); + $scope->end(); return $rv; } @@ -427,7 +427,7 @@ class Article extends Handler_Protected { $sth->execute([$tags_str, $id, $owner_uid]); } - $scope->close(); + $scope->end(); return $tags; } @@ -569,7 +569,7 @@ class Article extends Handler_Protected { else Labels::update_cache($owner_uid, $id, array("no-labels" => 1)); - $scope->close(); + $scope->end(); return $rv; } @@ -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->close(); + $scope->end(); return [$article_image, $article_stream, $article_kind]; } @@ -696,7 +696,7 @@ class Article extends Handler_Protected { } } - $scope->close(); + $scope->end(); return array_unique($rv); } @@ -723,7 +723,7 @@ class Article extends Handler_Protected { array_push($rv, $entry->feed_id); } - $scope->close(); + $scope->end(); return array_unique($rv); } diff --git a/classes/config.php b/classes/config.php index a8fc97124..302a5763e 100644 --- a/classes/config.php +++ b/classes/config.php @@ -193,10 +193,10 @@ class Config { const HTTP_429_THROTTLE_INTERVAL = "HTTP_429_THROTTLE_INTERVAL"; /** host running Jaeger collector to receive traces (disabled if empty) */ - const JAEGER_REPORTING_HOST = "JAEGER_REPORTING_HOST"; + const OPENTELEMETRY_HOST = "OPENTELEMETRY_HOST"; /** Jaeger service name */ - const JAEGER_SERVICE_NAME = "JAEGER_SERVICE_NAME"; + const OPENTELEMETRY_SERVICE = "OPENTELEMETRY_SERVICE"; /** default values for all global configuration options */ private const _DEFAULTS = [ @@ -255,8 +255,8 @@ class Config { Config::HTTP_USER_AGENT => [ 'Tiny Tiny RSS/%s (https://tt-rss.org/)', Config::T_STRING ], Config::HTTP_429_THROTTLE_INTERVAL => [ 3600, Config::T_INT ], - Config::JAEGER_REPORTING_HOST => [ "", Config::T_STRING ], - Config::JAEGER_SERVICE_NAME => [ "tt-rss", Config::T_STRING ], + Config::OPENTELEMETRY_HOST => [ "", Config::T_STRING ], + Config::OPENTELEMETRY_SERVICE => [ "tt-rss", Config::T_STRING ], ]; /** @var Config|null */ diff --git a/classes/counters.php b/classes/counters.php index fcf28f938..1f5988568 100644 --- a/classes/counters.php +++ b/classes/counters.php @@ -212,7 +212,7 @@ class Counters { } - $scope->close(); + $scope->end(); return $ret; } @@ -239,7 +239,7 @@ class Counters { "counter" => $subcribed_feeds ]); - $scope->close(); + $scope->end(); return $ret; } @@ -295,7 +295,7 @@ class Counters { } } - $scope->close(); + $scope->end(); return $ret; } @@ -356,7 +356,7 @@ class Counters { array_push($ret, $cv); } - $scope->close(); + $scope->end(); return $ret; } } diff --git a/classes/digest.php b/classes/digest.php index d77a83b8c..02fa76bf0 100644 --- a/classes/digest.php +++ b/classes/digest.php @@ -77,7 +77,7 @@ class Digest } } - $scope->close(); + $scope->end(); Debug::log("All done."); } diff --git a/classes/diskcache.php b/classes/diskcache.php index b5c10e89d..7bc5835a4 100644 --- a/classes/diskcache.php +++ b/classes/diskcache.php @@ -223,7 +223,7 @@ class DiskCache implements Cache_Adapter { public function remove(string $filename): bool { $scope = Tracer::start(__METHOD__, ['filename' => $filename]); $rc = $this->adapter->remove($filename); - $scope->close(); + $scope->end(); return $rc; } @@ -249,9 +249,10 @@ class DiskCache implements Cache_Adapter { } public function exists(string $filename): bool { - $scope = Tracer::start(__METHOD__, ['filename' => $filename]); + $scope = OpenTelemetry\API\Trace\Span::getCurrent(); + $scope->addEvent("DiskCache::exists: $filename"); + $rc = $this->adapter->exists(basename($filename)); - $scope->close(); return $rc; } @@ -262,7 +263,7 @@ class DiskCache implements Cache_Adapter { public function get_size(string $filename) { $scope = Tracer::start(__METHOD__, ['filename' => $filename]); $rc = $this->adapter->get_size(basename($filename)); - $scope->close(); + $scope->end(); return $rc; } @@ -275,7 +276,7 @@ class DiskCache implements Cache_Adapter { public function put(string $filename, $data) { $scope = Tracer::start(__METHOD__); $rc = $this->adapter->put(basename($filename), $data); - $scope->close(); + $scope->end(); return $rc; } @@ -329,8 +330,8 @@ class DiskCache implements Cache_Adapter { header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); echo "File not found."; - $scope->getSpan()->setTag('error', '404 not found'); - $scope->close(); + $scope->setAttribute('error', '404 not found'); + $scope->end(); return false; } @@ -340,8 +341,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->getSpan()->setTag('error', '304 not modified'); - $scope->close(); + $scope->setAttribute('error', '304 not modified'); + $scope->end(); return false; } @@ -360,8 +361,8 @@ class DiskCache implements Cache_Adapter { print "Stored file has disallowed content type ($mimetype)"; - $scope->getSpan()->setTag('error', '400 disallowed content type'); - $scope->close(); + $scope->setAttribute('error', '400 disallowed content type'); + $scope->end(); return false; } @@ -383,11 +384,11 @@ class DiskCache implements Cache_Adapter { header_remove("Pragma"); - $scope->getSpan()->setTag('mimetype', $mimetype); + $scope->setAttribute('mimetype', $mimetype); $rc = $this->adapter->send($filename); - $scope->close(); + $scope->end(); return $rc; } @@ -418,12 +419,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 = Tracer::start(__METHOD__); + $scope = OpenTelemetry\API\Trace\Span::getCurrent(); + $scope->addEvent("DiskCache::rewrite_urls"); $res = trim($str); if (!$res) { - $scope->close(); + $scope->end(); return ''; } @@ -437,7 +439,7 @@ class DiskCache implements Cache_Adapter { $need_saving = false; foreach ($entries as $entry) { - $e_scope = Tracer::start('entry', ['tagName' => $entry->tagName]); + $scope->addEvent("entry: " . $entry->tagName); foreach (array('src', 'poster') as $attr) { if ($entry->hasAttribute($attr)) { @@ -470,8 +472,6 @@ class DiskCache implements Cache_Adapter { $entry->setAttribute("srcset", RSSUtils::encode_srcset($matches)); } - - $e_scope->close(); } if ($need_saving) { @@ -482,8 +482,6 @@ class DiskCache implements Cache_Adapter { } } - $scope->close(); - return $res; } } diff --git a/classes/feeds.php b/classes/feeds.php index 3c55fcc25..b99be3d23 100755 --- a/classes/feeds.php +++ b/classes/feeds.php @@ -168,7 +168,7 @@ class Feeds extends Handler_Protected { $reply['search_query'] = [$search, $search_language]; $reply['vfeed_group_enabled'] = $vfeed_group_enabled; - $p_scope = Tracer::start('plugin_menu_items'); + $scope->addEvent('plugin_menu_items'); $plugin_menu_items = ""; PluginHost::getInstance()->chain_hooks_callback(PluginHost::HOOK_HEADLINE_TOOLBAR_SELECT_MENU_ITEM2, @@ -202,15 +202,13 @@ class Feeds extends Handler_Protected { }, $feed, $cat_view, $qfh_ret); - $p_scope->close(); - - $a_scope = Tracer::start('articles'); + $scope->addEvent('articles'); $headlines_count = 0; if ($result instanceof PDOStatement) { while ($line = $result->fetch(PDO::FETCH_ASSOC)) { - $aa_scope = Tracer::start('article', ['id' => $line['id']]); + $scope->addEvent('article: ' . $line['id']); ++$headlines_count; @@ -370,7 +368,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; - $c_scope = Tracer::start('colors'); + $scope->addEvent("colors"); require_once "colors.php"; @@ -386,7 +384,7 @@ class Feeds extends Handler_Protected { $line['feed_bg_color'] = 'rgba(' . implode(",", $rgba_cache[$feed_id]) . ',0.3)'; } - $c_scope->close(); + $scope->addEvent("HOOK_RENDER_ARTICLE_CDM"); PluginHost::getInstance()->chain_hooks_callback(PluginHost::HOOK_RENDER_ARTICLE_CDM, function ($result, $plugin) use (&$line) { @@ -403,13 +401,9 @@ class Feeds extends Handler_Protected { unset($line[$k]); array_push($reply['content'], $line); - - $aa_scope->close(); } } - $a_scope->close(); - if (!$headlines_count) { if ($result instanceof PDOStatement) { @@ -469,7 +463,7 @@ class Feeds extends Handler_Protected { } } - $scope->close(); + $scope->end(); return array($topmost_article_ids, $headlines_count, $feed, $disable_cache, $reply); } @@ -983,7 +977,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 = Tracer::start(__METHOD__, [], func_get_args()); + $scope = OpenTelemetry\API\Trace\Span::getCurrent(); + + $scope->addEvent(__METHOD__ . ": $feed ($is_cat)"); $n_feed = (int) $feed; $need_entries = false; @@ -1007,14 +1003,14 @@ class Feeds extends Handler_Protected { $handler = PluginHost::getInstance()->get_feed_handler($feed_id); if (implements_interface($handler, 'IVirtualFeed')) { /** @var IVirtualFeed $handler */ - $scope->close(); + //$scope->end(); return $handler->get_unread($feed_id); } else { - $scope->close(); + //$scope->end(); return 0; } } else if ($n_feed == Feeds::FEED_RECENTLY_READ) { - $scope->close(); + //$scope->end(); return 0; // tags } else if ($feed != "0" && $n_feed == 0) { @@ -1028,7 +1024,7 @@ class Feeds extends Handler_Protected { $row = $sth->fetch(); // Handle 'SUM()' returning null if there are no results - $scope->close(); + //$scope->end(); return $row["count"] ?? 0; } else if ($n_feed == Feeds::FEED_STARRED) { @@ -1062,7 +1058,7 @@ class Feeds extends Handler_Protected { $label_id = Labels::feed_to_label_id($feed); - $scope->close(); + //$scope->end(); return self::_get_label_unread($label_id, $owner_uid); } @@ -1082,7 +1078,7 @@ class Feeds extends Handler_Protected { $sth->execute([$owner_uid]); $row = $sth->fetch(); - $scope->close(); + //$scope->end(); return $row["unread"]; } else { @@ -1095,7 +1091,7 @@ class Feeds extends Handler_Protected { $sth->execute([$feed, $owner_uid]); $row = $sth->fetch(); - $scope->close(); + //$scope->end(); return $row["unread"]; } } @@ -1987,7 +1983,7 @@ class Feeds extends Handler_Protected { $res = $pdo->query($query); } - $scope->close(); + $scope->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 85b5f1df4..c2a949f5c 100755 --- a/classes/pluginhost.php +++ b/classes/pluginhost.php @@ -339,12 +339,15 @@ class PluginHost { */ function chain_hooks_callback(string $hook, Closure $callback, &...$args): void { $method = strtolower((string)$hook); - $scope = Tracer::start(__METHOD__, ['hook' => $hook]); + $scope = OpenTelemetry\API\Trace\Span::getCurrent(); + $scope->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_scope = Tracer::start("$hook - " . get_class($plugin)); + + $scope->addEvent("$hook - " . get_class($plugin)); try { if ($callback($plugin->$method(...$args), $plugin)) @@ -355,10 +358,10 @@ class PluginHost { user_error($err, E_USER_WARNING); } - $p_scope->close(); + //$p_scope->end(); } - $scope->close(); + //$scope->end(); } /** @@ -448,7 +451,7 @@ class PluginHost { $class = trim($class); $class_file = strtolower(basename(clean($class))); - $p_scope = Tracer::start("loading $class_file"); + $scope->addEvent("$class_file: load"); // try system plugin directory first $file = dirname(__DIR__) . "/plugins/$class_file/init.php"; @@ -457,7 +460,6 @@ class PluginHost { $file = dirname(__DIR__) . "/plugins.local/$class_file/init.php"; if (!file_exists($file)) { - $p_scope->close(); continue; } } @@ -476,8 +478,7 @@ class PluginHost { $_SESSION["safe_mode"] = 1; - $p_scope->getSpan()->setTag('error', 'plugin is blacklisted'); - $p_scope->close(); + $scope->setAttribute('error', 'plugin is blacklisted'); continue; } @@ -489,8 +490,7 @@ class PluginHost { } catch (Error $err) { user_error($err, E_USER_WARNING); - $p_scope->getSpan()->setTag('error', $err); - $p_scope->close(); + $scope->setAttribute('error', $err); continue; } @@ -501,8 +501,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); - $p_scope->getSpan()->setTag('error', 'plugin is not compatible with API version'); - $p_scope->close(); + $scope->setAttribute('error', 'plugin is not compatible with API version'); continue; } @@ -511,7 +510,7 @@ class PluginHost { _bind_textdomain_codeset($class, "UTF-8"); } - $i_scope = Tracer::start('init and register plugin'); + $scope->addEvent("$class_file: initialize"); try { switch ($kind) { @@ -537,17 +536,12 @@ class PluginHost { } catch (Error $err) { user_error($err, E_USER_WARNING); } - - $i_scope->close(); - } } - $p_scope->close(); } $this->load_data(); - - $scope->close(); + $scope->end(); } function is_system(Plugin $plugin): bool { @@ -640,7 +634,8 @@ class PluginHost { } private function load_data(): void { - $scope = Tracer::start(__METHOD__); + $scope = OpenTelemetry\API\Trace\Span::getCurrent(); + $scope->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 @@ -648,18 +643,19 @@ class PluginHost { $sth->execute([$this->owner_uid]); while ($line = $sth->fetch()) { + $scope->addEvent($line["name"] . ': unserialize'); + $this->storage[$line["name"]] = unserialize($line["content"]); } $this->data_loaded = true; } - - $scope->close(); } private function save_data(string $plugin): void { if ($this->owner_uid) { - $scope = Tracer::start(__METHOD__); + $scope = OpenTelemetry\API\Trace\Span::getCurrent(); + $scope->addEvent(__METHOD__ . ": $plugin"); if (!$this->pdo_data) $this->pdo_data = Db::instance()->pdo_connect(); @@ -687,7 +683,6 @@ class PluginHost { } $this->pdo_data->commit(); - $scope->close(); } } diff --git a/classes/pref/feeds.php b/classes/pref/feeds.php index b22b40ba5..a2b55b88c 100755 --- a/classes/pref/feeds.php +++ b/classes/pref/feeds.php @@ -1080,6 +1080,9 @@ class Pref_Feeds extends Handler_Protected { * @return array<string, mixed> */ private function feedlist_init_cat(int $cat_id): array { + $scope = OpenTelemetry\API\Trace\Span::getCurrent(); + $scope->addEvent(__METHOD__ . ": $cat_id"); + return [ 'id' => 'CAT:' . $cat_id, 'items' => array(), @@ -1094,7 +1097,8 @@ class Pref_Feeds extends Handler_Protected { * @return array<string, mixed> */ private function feedlist_init_feed(int $feed_id, ?string $title = null, bool $unread = false, string $error = '', string $updated = ''): array { - $scope = Tracer::start(__METHOD__, []); + $scope = OpenTelemetry\API\Trace\Span::getCurrent(); + $scope->addEvent(__METHOD__ . ": $feed_id"); if (!$title) $title = Feeds::_get_title($feed_id, false); @@ -1102,8 +1106,6 @@ class Pref_Feeds extends Handler_Protected { if ($unread === false) $unread = Feeds::_get_counters($feed_id, false, true); - $scope->close(); - return [ 'id' => 'FEED:' . $feed_id, 'name' => $title, diff --git a/classes/rpc.php b/classes/rpc.php index afd3c0c79..929c6bda0 100755 --- a/classes/rpc.php +++ b/classes/rpc.php @@ -134,7 +134,7 @@ class RPC extends Handler_Protected { 'seq' => $seq ]; - $scope->close(); + $scope->end(); print json_encode($reply); } diff --git a/classes/rssutils.php b/classes/rssutils.php index 30db08963..29368da1c 100755 --- a/classes/rssutils.php +++ b/classes/rssutils.php @@ -286,7 +286,7 @@ class RSSUtils { // Send feed digests by email if needed. Digest::send_headlines_digests(); - $scope->close(); + $scope->end(); return $nf; } @@ -390,19 +390,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->close(); + $scope->end(); return false; } } else { // this would indicate database corruption of some kind Debug::log("error: owner not found for feed: $feed"); - $scope->close(); + $scope->end(); return false; } } else { Debug::log("error: feeds table record not found for feed: $feed"); - $scope->close(); + $scope->end(); return false; } @@ -561,7 +561,7 @@ class RSSUtils { $feed_obj->save(); } - $scope->close(); + $scope->end(); return $error_message == ""; } @@ -703,7 +703,7 @@ class RSSUtils { ]); $feed_obj->save(); - $scope->close(); + $scope->end(); return true; // no articles } @@ -1305,7 +1305,7 @@ class RSSUtils { Debug::log("article processed.", Debug::LOG_VERBOSE); $pdo->commit(); - $a_scope->close(); + $a_scope->end(); } Debug::log(Debug::SEPARATOR, Debug::LOG_VERBOSE); @@ -1346,12 +1346,12 @@ class RSSUtils { unset($rss); Debug::log("update failed.", Debug::LOG_VERBOSE); - $scope->close(); + $scope->end(); return false; } Debug::log("update done.", Debug::LOG_VERBOSE); - $scope->close(); + $scope->end(); return true; } @@ -1604,7 +1604,7 @@ class RSSUtils { } } - $scope->close(); + $scope->end(); return $matches; } diff --git a/classes/sanitizer.php b/classes/sanitizer.php index 28874d16f..68bb91b9f 100644 --- a/classes/sanitizer.php +++ b/classes/sanitizer.php @@ -63,7 +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 = Tracer::start(__METHOD__); + $scope = OpenTelemetry\API\Trace\Span::getCurrent(); + $scope->addEvent("Sanitizer::sanitize"); if (!$owner && isset($_SESSION["uid"])) $owner = $_SESSION["uid"]; @@ -224,8 +225,6 @@ class Sanitizer { $res = $doc->saveHTML(); - $scope->close(); - /* strip everything outside of <body>...</body> */ $res_frag = array(); diff --git a/classes/tracer.php b/classes/tracer.php index 5a23dfeba..3a734641e 100644 --- a/classes/tracer.php +++ b/classes/tracer.php @@ -1,61 +1,72 @@ <?php -use OpenTracing\GlobalTracer; -use OpenTracing\Scope; + +use OpenTelemetry\Contrib\Otlp\OtlpHttpTransportFactory; +use OpenTelemetry\Contrib\Otlp\SpanExporter; +use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor; +use OpenTelemetry\SDK\Trace\TracerProvider; +use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator; +use OpenTelemetry\API\Trace\Span; +use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter; class Tracer { /** @var Tracer $instance */ private static $instance; + private static $tracer; public function __construct() { - $jaeger_host = Config::get(Config::JAEGER_REPORTING_HOST); - - if ($jaeger_host) { - $config = new \Jaeger\Config( - [ - 'sampler' => [ - 'type' => \Jaeger\SAMPLER_TYPE_CONST, - 'param' => true, - ], - 'logging' => true, - "local_agent" => [ - "reporting_host" => $jaeger_host, - "reporting_port" => 6832 - ], - 'dispatch_mode' => \Jaeger\Config::JAEGER_OVER_BINARY_UDP, - ], - Config::get(Config::JAEGER_SERVICE_NAME) - ); - - $config->initializeTracer(); - - register_shutdown_function(function() { - $tracer = GlobalTracer::get(); - $tracer->flush(); - }); + $opentelemetry_host = Config::get(Config::OPENTELEMETRY_HOST); + + if ($opentelemetry_host) { + $transport = (new OtlpHttpTransportFactory())->create("http://$opentelemetry_host/v1/traces", 'application/x-protobuf'); + $exporter = new SpanExporter($transport); + } else { + $exporter = new InMemoryExporter(); } + + $tracerProvider = new TracerProvider(new SimpleSpanProcessor($exporter)); + $this->tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php'); + + $context = TraceContextPropagator::getInstance()->extract(getallheaders()); + $span = $this->tracer->spanBuilder(Config::get(Config::OPENTELEMETRY_SERVICE)) + ->setParent($context) + ->startSpan(); + + $span->activate(); + + register_shutdown_function(function() use ($span, $tracerProvider) { + $span->end(); + + $tracerProvider->shutdown(); + }); } /** * @param string $name * @param array<string>|array<string, array<string, mixed>> $tags * @param array<string> $args - * @return Scope + * @return Span */ - private function _start(string $name, array $tags = [], array $args = []): Scope { - $tracer = GlobalTracer::get(); + private function _start(string $name, array $tags = [], array $args = []) { + $span = $this->tracer->spanBuilder($name)->startSpan(); + + foreach ($tags as $k => $v) { + $span->setAttribute($k, $v); + } + + $span->setAttribute("func.args", json_encode($args)); - $tags['args'] = json_encode($args); + $span->activate(); - return $tracer->startActiveSpan($name, ['tags' => $tags]); + return $span; } /** * @param string $name * @param array<string>|array<string, array<string, mixed>> $tags * @param array<string> $args - * @return Scope + * @return Span */ - public static function start(string $name, array $tags = [], array $args = []) : Scope { + public static function start(string $name, array $tags = [], array $args = []) { return self::get_instance()->_start($name, $tags, $args); } diff --git a/classes/urlhelper.php b/classes/urlhelper.php index 6300e7346..ac585dd54 100644 --- a/classes/urlhelper.php +++ b/classes/urlhelper.php @@ -189,8 +189,8 @@ class UrlHelper { // too many redirects if ($nest > 10) { - $scope->getSpan()->setTag('error', 'too many redirects'); - $scope->close(); + $scope->setAttribute('error', 'too many redirects'); + $scope->end(); return false; } @@ -226,12 +226,12 @@ class UrlHelper { } } - $scope->close(); + $scope->end(); return $url; } - $scope->getSpan()->setTag('error', 'request failed'); - $scope->close(); + $scope->setAttribute('error', 'request failed'); + $scope->end(); // request failed? return false; } @@ -303,8 +303,8 @@ class UrlHelper { if (!$url) { self::$fetch_last_error = "Requested URL failed extended validation."; - $scope->getSpan()->setTag('error', self::$fetch_last_error); - $scope->close(); + $scope->setAttribute('error', self::$fetch_last_error); + $scope->end(); return false; } @@ -314,8 +314,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->getSpan()->setTag('error', self::$fetch_last_error); - $scope->close(); + $scope->setAttribute('error', self::$fetch_last_error); + $scope->end(); return false; } @@ -327,8 +327,8 @@ class UrlHelper { if (!$ch) { self::$fetch_last_error = "curl_init() failed"; - $scope->getSpan()->setTag('error', self::$fetch_last_error); - $scope->close(); + $scope->setAttribute('error', self::$fetch_last_error); + $scope->end(); return false; } @@ -432,8 +432,8 @@ class UrlHelper { if (!self::validate(self::$fetch_effective_url, true)) { self::$fetch_last_error = "URL received after redirection failed extended validation."; - $scope->getSpan()->setTag('error', self::$fetch_last_error); - $scope->close(); + $scope->setAttribute('error', self::$fetch_last_error); + $scope->end(); return false; } @@ -442,8 +442,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->getSpan()->setTag('error', self::$fetch_last_error); - $scope->close(); + $scope->setAttribute('error', self::$fetch_last_error); + $scope->end(); return false; } @@ -460,8 +460,8 @@ class UrlHelper { self::$fetch_last_error_content = $contents; curl_close($ch); - $scope->getSpan()->setTag('error', self::$fetch_last_error); - $scope->close(); + $scope->setAttribute('error', self::$fetch_last_error); + $scope->end(); return false; } @@ -473,8 +473,8 @@ class UrlHelper { } curl_close($ch); - $scope->getSpan()->setTag('error', self::$fetch_last_error); - $scope->close(); + $scope->setAttribute('error', self::$fetch_last_error); + $scope->end(); return false; } @@ -488,7 +488,7 @@ class UrlHelper { if ($tmp) $contents = $tmp; } - $scope->close(); + $scope->end(); return $contents; } else { @@ -543,8 +543,8 @@ class UrlHelper { if (!self::validate(self::$fetch_effective_url, true)) { self::$fetch_last_error = "URL received after redirection failed extended validation."; - $scope->getSpan()->setTag('error', self::$fetch_last_error); - $scope->close(); + $scope->setAttribute('error', self::$fetch_last_error); + $scope->end(); return false; } @@ -553,8 +553,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->getSpan()->setTag('error', self::$fetch_last_error); - $scope->close(); + $scope->setAttribute('error', self::$fetch_last_error); + $scope->end(); return false; } @@ -563,8 +563,8 @@ class UrlHelper { if ($data === false) { self::$fetch_last_error = "'file_get_contents' failed."; - $scope->getSpan()->setTag('error', self::$fetch_last_error); - $scope->close(); + $scope->setAttribute('error', self::$fetch_last_error); + $scope->end(); return false; } @@ -600,8 +600,8 @@ class UrlHelper { self::$fetch_last_error_content = $data; - $scope->getSpan()->setTag('error', self::$fetch_last_error); - $scope->close(); + $scope->setAttribute('error', self::$fetch_last_error); + $scope->end(); return false; } @@ -614,13 +614,13 @@ class UrlHelper { if ($tmp) $data = $tmp; } - $scope->close(); + $scope->end(); return $data; } else { self::$fetch_last_error = 'Successful response, but no content was received.'; - $scope->getSpan()->setTag('error', self::$fetch_last_error); - $scope->close(); + $scope->setAttribute('error', self::$fetch_last_error); + $scope->end(); return false; } } |