Skip to content

Commit

Permalink
Correctly convert floats to ints (#19) (#20)
Browse files Browse the repository at this point in the history
* Correctly convert floats to ints (#19)

* Rejig variable names
  • Loading branch information
ingalless authored Dec 18, 2023
1 parent 81cd306 commit 46c6a9c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" printerClass="NunoMaduro\Collision\Adapters\Phpunit\Printer">
<phpunit bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" convertDeprecationsToExceptions="true" processIsolation="false" stopOnFailure="false" printerClass="NunoMaduro\Collision\Adapters\Phpunit\Printer">
<testsuites>
<testsuite name="Laravel Application Insights Test Suite">
<directory>tests</directory>
Expand All @@ -10,4 +10,4 @@
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
</phpunit>
4 changes: 2 additions & 2 deletions src/ApplicationInsights.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
5 changes: 3 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand Down
45 changes: 41 additions & 4 deletions tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,7 +28,6 @@ public function test_that_it_loads_correctly()
$this->assertTrue($insights->isEnabled());

$this->assertInstanceOf(\Mondago\ApplicationInsights\ApplicationInsights::class, $insights);

}

/**
Expand All @@ -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);

}

/**
Expand All @@ -58,7 +61,41 @@ public function test_that_it_tries_to_send_data()
Insights::shouldThrowExceptions(true);

Insights::trackRequest(new Request(), new Response());

}

}

/**
* 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()
]
]);
}
}

0 comments on commit 46c6a9c

Please sign in to comment.