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

Include libraries and git_sha in metadata from ocramius/package-versions #39

Merged
merged 1 commit into from
Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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|^3.0",
"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