Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Swoole Integration #2595

Merged
merged 14 commits into from
Mar 28, 2024
31 changes: 12 additions & 19 deletions src/Integrations/Integrations/Swoole/SwooleIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use DDTrace\HookData;
use DDTrace\Integrations\Integration;
use DDTrace\SpanData;
use DDTrace\SpanStack;
use DDTrace\Tag;
use DDTrace\Type;
use DDTrace\Util\Normalizer;
Expand All @@ -23,20 +23,20 @@ public function getName()
return self::NAME;
}

public function addTraceAnalyticsIfEnabled(SpanData $span)
/**
* {@inheritdoc}
*/
public function requiresExplicitTraceAnalyticsEnabling()
{
if (!$this->configuration->isTraceAnalyticsEnabled()) {
return;
}
$span->metrics[Tag::ANALYTICS_KEY] = $this->configuration->getTraceAnalyticsSampleRate();
return false;
}

public function instrumentRequestStart(callable $callback, SwooleIntegration $integration)
public function instrumentRequestStart(callable $callback, SwooleIntegration $integration, Server $server)
{
\DDTrace\install_hook(
$callback,
function (HookData $hook) use (&$rootSpan, $integration) {
$rootSpan = \DDTrace\start_trace_span();
function (HookData $hook) use ($integration, $server) {
$rootSpan = $hook->span(new SpanStack());
$rootSpan->name = "web.request";
$rootSpan->service = \ddtrace_config_app_name('swoole');
$rootSpan->type = Type::WEB_SERVLET;
Expand Down Expand Up @@ -97,21 +97,14 @@ function (HookData $hook) use (&$rootSpan, $integration) {
$rootSpan->resource = $request->server['request_method'] . ' ' . $normalizedPath;
$rootSpan->meta[Tag::HTTP_METHOD] = $request->server['request_method'];

$serverProtocol = $request->server['server_protocol'] ?? 'HTTP/1.1';
$scheme = strpos($serverProtocol, 'HTTPS') !== false ? 'https://' : 'http://';
$scheme = $server->ssl ? 'https://' : 'http://';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move that comparison before the install_hook call? and use ($scheme) instead? Apart from the minuscule micro-optimization, it also avoids having a circular dependency. Not sure if it makes any difference, but I think we should.

$host = $headers['host'] ?? ($request->server['remote_addr'] . ':' . $request->server['server_port']);
$path = $request->server['request_uri'] ?? $request->server['path_info'] ?? '';
$query = isset($request->server['query_string']) ? '?' . $request->server['query_string'] : '';
$url = $scheme . $host . $path . $query;
$rootSpan->meta[Tag::HTTP_URL] = Normalizer::uriNormalizeincomingPath($url);
},
function (HookData $hook) use (&$rootSpan, $integration) {
if ($hook->exception) {
$rootSpan->exception = $hook->exception;
}

\DDTrace\close_spans_until($rootSpan);
\DDTrace\close_span();
unset($rootSpan->meta['closure.declaration']);
Copy link
Collaborator

@bwoebi bwoebi Mar 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are tracing the callback, we would always have a redundant, non-really-useful tag with the location of this closure's declaration. In the case of Laravel Octane, for instance, we would always have this tag pointing to swoole-server.php, which is not really helpful.

Additionally, but to a lesser extent, this is inconsistent with other frameworks/libraries. If we are setting the location of the request callback declaration from Swoole, why aren't we setting the same for Laravel and Kernel::handle, for instance?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

}
);
}
Expand Down Expand Up @@ -139,7 +132,7 @@ function ($server, $scope, $args, $retval) use ($integration) {
list($eventName, $callback) = $args;

if ($eventName === 'request') {
$integration->instrumentRequestStart($callback, $integration);
$integration->instrumentRequestStart($callback, $integration, $server);
}
}
);
Expand Down
1 change: 1 addition & 0 deletions tests/Integrations/Swoole/CommonScenariosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected static function getEnvs()
'DD_SERVICE' => 'swoole_test_app',
'DD_TRACE_CLI_ENABLED' => 'true',
'DD_TRACE_RESOURCE_URI_QUERY_PARAM_ALLOWED' => '*',
'DD_TRACE_DEBUG' => 'true',
]);
}

Expand Down
10 changes: 0 additions & 10 deletions tests/Sapi/SwooleServer/SwooleServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,6 @@ public function start()
$this->inis['xdebug.mode'] = 'coverage';
}

/**
* If a router is provided to the built-in web server (the index file),
* the request init hook (which hooks auto_prepend_file) will not run.
* If there is no router, the script is run with php_execute_script():
* @see https://heap.space/xref/PHP-7.4/sapi/cli/php_cli_server.c?r=58b17906#2077
* This runs the auto_prepend_file as expected. However, if a router is present,
* zend_execute_scripts() will be used instead:
* @see https://heap.space/xref/PHP-7.4/sapi/cli/php_cli_server.c?r=58b17906#2202
* As a result auto_prepend_file (and the request init hook) is not executed.
*/
$cmd = sprintf(
PHP_BINARY . ' %s %s',
new IniSerializer($this->inis),
Expand Down
11 changes: 0 additions & 11 deletions tests/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,6 @@
]
}
},
"swoole4": {
"scenario-options": {
"create-lockfile": false
},
"scripts": {
"pre-autoload-dump": [
"sudo pecl uninstall swoole",
"printf 'no' | sudo pecl install -f swoole-4.8.13"
]
}
},
"swoole5": {
"scenario-options": {
"create-lockfile": false
Expand Down