diff --git a/phpunit.xml b/phpunit.xml index e966b8c..8e332ca 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,5 +1,5 @@ - + tests @@ -10,4 +10,4 @@ src/ - \ No newline at end of file + diff --git a/src/ApplicationInsights.php b/src/ApplicationInsights.php index e63ab03..f06d38f 100644 --- a/src/ApplicationInsights.php +++ b/src/ApplicationInsights.php @@ -142,8 +142,8 @@ public function trackRequest(Request $request, Response $response, bool $sendAsy $this->insights->trackRequest( 'app ' . $request->getMethod() . ' ' . $request->getUri(), $request->fullUrl(), - $this->getRequestStartTime(), - $this->getRequestDurationTime(), + intval($this->getRequestStartTime()), + intval($this->getRequestDurationTime()), $response->getStatusCode(), $response->isSuccessful(), $this->getRequestProperties(), diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index d5405f3..f5858ea 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -5,6 +5,7 @@ use ApplicationInsights\Channel\Contracts\Cloud; use ApplicationInsights\Telemetry_Client; use ApplicationInsights\Telemetry_Context; +use Illuminate\Database\Events\QueryExecuted; use Illuminate\Support\Facades\DB; use Illuminate\Support\ServiceProvider as LaravelServiceProvider; @@ -22,8 +23,8 @@ public function boot(): void ]); if (config(static::DISPLAY_NAME . '.is_enabled')) { - DB::listen(function ($query) { - $this->app[static::DISPLAY_NAME]->trackDependency($query->connection->getConfig('host') ?? 'db', $query->time, 'SQL', [ + DB::listen(function (QueryExecuted $query) { + $this->app[static::DISPLAY_NAME]->trackDependency($query->connection->getConfig('host') ?? 'db', intval($query->time), 'SQL', [ 'sql' => $query->sql, 'bindings' => $query->bindings, 'connection' => $query->connectionName, diff --git a/tests/ServiceProviderTest.php b/tests/ServiceProviderTest.php index e526757..dbec70f 100644 --- a/tests/ServiceProviderTest.php +++ b/tests/ServiceProviderTest.php @@ -3,9 +3,14 @@ namespace Mondago\ApplicationInsights\Tests; use GuzzleHttp\Exception\ClientException; +use Illuminate\Database\Connection; +use Illuminate\Database\Events\QueryExecuted; use Illuminate\Http\Request; use Illuminate\Http\Response; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Event; use Insights; +use Mondago\ApplicationInsights\ApplicationInsights; use Mondago\ApplicationInsights\ServiceProvider; class ServiceProviderTest extends TestCase @@ -23,7 +28,6 @@ public function test_that_it_loads_correctly() $this->assertTrue($insights->isEnabled()); $this->assertInstanceOf(\Mondago\ApplicationInsights\ApplicationInsights::class, $insights); - } /** @@ -39,7 +43,6 @@ public function test_that_it_is_disabled_if_set_in_configuration() $this->assertFalse($insights->isEnabled()); $this->assertInstanceOf(\Mondago\ApplicationInsights\ApplicationInsights::class, $insights); - } /** @@ -58,7 +61,41 @@ public function test_that_it_tries_to_send_data() Insights::shouldThrowExceptions(true); Insights::trackRequest(new Request(), new Response()); - } -} \ No newline at end of file + + /** + * Check it listens to DB correctly + * + * @return void + */ + public function test_that_it_listens_to_db_queries() + { + $this->app['config']->set(ServiceProvider::DISPLAY_NAME . '.instrumentation_key', 'notarealinstrumentationkey'); + + $insights = $this->spy(ApplicationInsights::class); + + $connection = $this->getConnection(); + + $queryTimeAsFloat = 3.34; + $expectedQueryTimeAsInt = 3; + + Event::dispatch(new QueryExecuted( + sql: "select * from users where name = ?", + bindings: "mondy", + time: $queryTimeAsFloat, + connection: $connection + )); + + $insights->shouldHaveReceived('trackDependency', [ + $connection->getConfig('host'), + $expectedQueryTimeAsInt, + 'SQL', + [ + 'sql' => 'select * from users where name = ?', + 'bindings' => 'mondy', + 'connection' => $connection->getName() + ] + ]); + } +}