Skip to content

Commit

Permalink
Include libraries and git_sha in metadata from ocramius/package-versions
Browse files Browse the repository at this point in the history
  • Loading branch information
asgrim committed Aug 21, 2019
1 parent 48ca1e1 commit 831eb87
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 65 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"php": ">=7.1.0,<7.5.0",
"ext-json": "*",
"ext-sockets": "*",
"ocramius/package-versions": "^1.4",
"psr/log": "^1.1",
"ralouphie/getallheaders": "2.0.5",
"ramsey/uuid": "^3.7"
Expand Down
102 changes: 51 additions & 51 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 25 additions & 10 deletions src/Events/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
namespace Scoutapm\Events;

use DateTimeImmutable;
use PackageVersions\Versions;
use Scoutapm\Connector\Command;
use Scoutapm\Helper\Timer;
use const PHP_VERSION;
use function array_keys;
use function array_map;
use function explode;
use function gethostname;

/**
Expand All @@ -28,7 +32,7 @@ public function __construct(DateTimeImmutable $now)
}

/**
* @return array<string, (string|array<int, string>|null)>
* @return array<string, (string|array<int, array<int, string>>|null)>
*/
private function data() : array
{
Expand All @@ -44,24 +48,35 @@ private function data() : array
'database_engine' => '',
'database_adapter' => '',
'application_name' => '',
'libraries' => [],
'libraries' => $this->getLibraries(),
'paas' => '',
'application_root' => '',
'scm_subdirectory' => '',
'git_sha' => '',
'git_sha' => $this->rootPackageGitSha(),
];
}

private function rootPackageGitSha() : string
{
return explode('@', Versions::getVersion(Versions::ROOT_PACKAGE_NAME))[1];
}

/**
* @return array<int, array<int, string>>
* Return an array of arrays: [["package name", "package version"], ....]
*
* @TODO: Return an array of arrays: [["package name", "package version"], ....]
* @return array<int, array<int, string>>
*/
// private function getLibraries() : array
// {
// $composer = require __DIR__ . "/vendor/autoload.php";
// return [];
// }
private function getLibraries() : array
{
return array_map(
/** @return string[][]|array<int, string> */
static function (string $package, string $version) : array {
return [$package, $version];
},
array_keys(Versions::VERSIONS),
Versions::VERSIONS
);
}

/**
* Turn this object into a list of commands to send to the CoreAgent
Expand Down
16 changes: 12 additions & 4 deletions tests/Unit/Events/MetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
use DateTimeImmutable;
use DateTimeZone;
use Exception;
use PackageVersions\Versions;
use PHPUnit\Framework\TestCase;
use Scoutapm\Events\Metadata;
use Scoutapm\Helper\Timer;
use const PHP_VERSION;
use function array_keys;
use function array_map;
use function explode;
use function gethostname;
use function json_decode;
use function json_encode;
Expand All @@ -25,8 +29,6 @@ public function testMetadataSerializesToJson() : void

$serialized = json_encode(new Metadata($time));

self::assertNotEmpty($serialized);

self::assertEquals(
[
'ApplicationEvent' => [
Expand All @@ -43,11 +45,17 @@ public function testMetadataSerializesToJson() : void
'database_engine' => '',
'database_adapter' => '',
'application_name' => '',
'libraries' => [],
'libraries' => array_map(
static function ($package, $version) {
return [$package, $version];
},
array_keys(Versions::VERSIONS),
Versions::VERSIONS
),
'paas' => '',
'application_root' => '',
'scm_subdirectory' => '',
'git_sha' => '',
'git_sha' => explode('@', Versions::getVersion(Versions::ROOT_PACKAGE_NAME))[1],
],
'event_type' => 'scout.metadata',
'source' => 'php',
Expand Down

0 comments on commit 831eb87

Please sign in to comment.