Skip to content

Commit

Permalink
Add mysqli_real_query to MysqliIntegration
Browse files Browse the repository at this point in the history
  • Loading branch information
junjihashimoto committed Nov 19, 2024
1 parent 93849dd commit 3af6692
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/DDTrace/Integrations/Mysqli/MysqliIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,24 @@ function (SpanData $span, $args) use ($integration) {
}
});

\DDTrace\install_hook('mysqli_real_query', function (HookData $hook) use ($integration) {
list($mysqli, $query) = $hook->args;

$span = $hook->span();
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;
$integration->setDefaultAttributes($span, 'mysqli_real_query', $query);
$integration->addTraceAnalyticsIfEnabled($span);
$integration->setConnectionInfo($span, $mysqli);

DatabaseIntegrationHelper::injectDatabaseIntegrationData($hook, 'mysql', 1);
}, function (HookData $hook) use ($integration) {
list($mysqli, $query) = $hook->args;
$span = $hook->span();
$integration->setConnectionInfo($span, $mysqli);

MysqliCommon::storeQuery($mysqli, $query);
});

\DDTrace\install_hook('mysqli_prepare', function (HookData $hook) use ($integration) {
list(, $query) = $hook->args;

Expand Down Expand Up @@ -174,6 +192,24 @@ function (SpanData $span, $args) use ($integration) {
}
});

\DDTrace\install_hook('mysqli::real_query', function (HookData $hook) use ($integration) {
list($query) = $hook->args;

$span = $hook->span();
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;
$integration->setDefaultAttributes($span, 'mysqli.real_query', $query);
$integration->addTraceAnalyticsIfEnabled($span);
$integration->setConnectionInfo($span, $this);

DatabaseIntegrationHelper::injectDatabaseIntegrationData($hook, 'mysql');
}, function (HookData $hook) use ($integration) {
list($query) = $hook->args;
$span = $hook->span();
$integration->setConnectionInfo($span, $this);

MysqliCommon::storeQuery($this, $query);
});

\DDTrace\install_hook('mysqli::prepare', function (HookData $hook) use ($integration) {
list($query) = $hook->args;

Expand Down
38 changes: 38 additions & 0 deletions tests/Integrations/Mysqli/MysqliTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ public function testProceduralQuery()
]);
}

public function testProceduralRealQuery()
{
$traces = $this->isolateTracer(function () {
$mysqli = \mysqli_connect(self::$host, self::$user, self::$password, self::$database);
\mysqli_real_query($mysqli, 'SELECT * from tests');
$mysqli->close();
});

$this->assertFlameGraph($traces, [
SpanAssertion::exists('mysqli_connect', 'mysqli_connect'),
SpanAssertion::build('mysqli_real_query', 'mysqli', 'sql', 'SELECT * from tests')
->withExactTags(self::baseTags(true, false))
->withExactMetrics([
'_dd.agent_psr' => 1.0,
'_sampling_priority_v1' => 1.0,
]),
]);
}

public function testProceduralQueryPeerServiceEnabled()
{
$this->putEnvAndReloadConfig(['DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED=true']);
Expand Down Expand Up @@ -253,6 +272,25 @@ public function testConstructorQuery()
]);
}

public function testConstructorRealQuery()
{
$traces = $this->isolateTracer(function () {
$mysqli = new \mysqli(self::$host, self::$user, self::$password, self::$database);
$mysqli->real_query('SELECT * from tests');
$mysqli->close();
});

$this->assertFlameGraph($traces, [
SpanAssertion::exists('mysqli.__construct', 'mysqli.__construct'),
SpanAssertion::build('mysqli.real_query', 'mysqli', 'sql', 'SELECT * from tests')
->withExactTags(self::baseTags())
->withExactMetrics([
'_dd.agent_psr' => 1.0,
'_sampling_priority_v1' => 1.0,
]),
]);
}

public function testConstructorQueryPeerServiceEnabled()
{
$this->putEnvAndReloadConfig(['DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED=true']);
Expand Down

0 comments on commit 3af6692

Please sign in to comment.