From 31dbf2a6bd4d78b226d2af8a7776c43b66ebf413 Mon Sep 17 00:00:00 2001 From: Szymon Piasecki Date: Thu, 16 Jan 2020 07:04:06 +0100 Subject: [PATCH 1/5] Add api_host configuration property --- CHANGELOG.md | 4 ++++ Command/NotifyDeploymentCommand.php | 6 +++--- DependencyInjection/Configuration.php | 3 ++- DependencyInjection/EkinoNewRelicExtension.php | 1 + NewRelic/Config.php | 9 ++++++++- README.md | 1 + Tests/NewRelic/ConfigTest.php | 3 ++- 7 files changed, 21 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index daf4cba..641d32a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## UNRELEASED +### Added + +- Added `api_host` configuration property used by `NotifyDeploymentCommand` + ## v2.1.3 ### Added diff --git a/Command/NotifyDeploymentCommand.php b/Command/NotifyDeploymentCommand.php index 66dfd71..6c29c80 100644 --- a/Command/NotifyDeploymentCommand.php +++ b/Command/NotifyDeploymentCommand.php @@ -74,7 +74,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $exitCode = 0; foreach ($appNames as $appName) { - $response = $this->performRequest($this->newrelic->getApiKey(), $this->createPayload($appName, $input)); + $response = $this->performRequest($this->newrelic->getApiHost(), $this->newrelic->getApiKey(), $this->createPayload($appName, $input)); switch ($response['status']) { case 200: @@ -99,7 +99,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int return $exitCode; } - public function performRequest(string $api_key, string $payload): array + public function performRequest(string $api_host, string $api_key, string $payload): array { $headers = [ \sprintf('x-api-key: %s', $api_key), @@ -116,7 +116,7 @@ public function performRequest(string $api_key, string $payload): array ]; $level = \error_reporting(0); - $content = \file_get_contents('https://api.newrelic.com/deployments.xml', false, \stream_context_create($context)); + $content = \file_get_contents(\sprintf('https://%s/deployments.xml', $api_host), false, \stream_context_create($context)); \error_reporting($level); if (false === $content) { $error = \error_get_last(); diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index c248353..1d71aa3 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -36,7 +36,8 @@ public function getConfigTreeBuilder(): TreeBuilder ->booleanNode('enabled')->defaultTrue()->end() ->scalarNode('interactor')->end() ->booleanNode('twig')->defaultValue(\class_exists(Environment::class))->end() - ->scalarNode('api_key')->defaultValue(null)->end() + ->scalarNode('api_key')->defaultValue(null)->treatNullLike('api.newrelic.com')->end() + ->scalarNode('api_host')->defaultValue('api.newrelic.com')->end() ->scalarNode('license_key')->defaultValue(null)->end() ->scalarNode('application_name')->defaultValue(null)->end() ->arrayNode('deployment_names') diff --git a/DependencyInjection/EkinoNewRelicExtension.php b/DependencyInjection/EkinoNewRelicExtension.php index 5f174d1..6deda85 100644 --- a/DependencyInjection/EkinoNewRelicExtension.php +++ b/DependencyInjection/EkinoNewRelicExtension.php @@ -71,6 +71,7 @@ public function load(array $configs, ContainerBuilder $container): void ->setArguments( [ '$name' => $config['application_name'], + '$apiHost' => $config['api_host'], '$apiKey' => $config['api_key'], '$licenseKey' => $config['license_key'], '$xmit' => $config['xmit'], diff --git a/NewRelic/Config.php b/NewRelic/Config.php index e557c60..48bd900 100644 --- a/NewRelic/Config.php +++ b/NewRelic/Config.php @@ -20,6 +20,7 @@ class Config { private $name; private $apiKey; + private $apiHost; private $licenseKey; private $xmit; private $customEvents; @@ -27,10 +28,11 @@ class Config private $customParameters; private $deploymentNames; - public function __construct(?string $name, string $apiKey = null, string $licenseKey = null, bool $xmit = false, array $deploymentNames = []) + public function __construct(?string $name, string $apiHost, string $apiKey = null, string $licenseKey = null, bool $xmit = false, array $deploymentNames = []) { $this->name = (!empty($name) ? $name : \ini_get('newrelic.appname')) ?: ''; $this->apiKey = $apiKey; + $this->apiHost = $apiHost; $this->licenseKey = (!empty($licenseKey) ? $licenseKey : \ini_get('newrelic.license')) ?: ''; $this->xmit = $xmit; $this->deploymentNames = $deploymentNames; @@ -105,6 +107,11 @@ public function getApiKey(): ?string return $this->apiKey; } + public function getApiHost(): string + { + return $this->apiHost; + } + public function getLicenseKey(): ?string { return $this->licenseKey; diff --git a/README.md b/README.md index 2f492b6..23b7a7a 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ ekino_new_relic: # as php ini-value deployment_names: ~ # default value is 'application_name', supports string array or semi-colon separated string api_key: # New Relic API + api_host: ~ # New Relic API Host (default value is api.newrelic.com, for EU should be set to api.eu.newrelic.com ) license_key: # New Relic license key (optional, default value is read from php.ini) xmit: false # if you want to record the metric data up to the point newrelic_set_appname is called, set this to true (default: false) logging: false # If true, logs all New Relic interactions to the Symfony log (default: false) diff --git a/Tests/NewRelic/ConfigTest.php b/Tests/NewRelic/ConfigTest.php index 36116b7..0cbb628 100644 --- a/Tests/NewRelic/ConfigTest.php +++ b/Tests/NewRelic/ConfigTest.php @@ -20,10 +20,11 @@ class ConfigTest extends TestCase { public function testGeneric() { - $newRelic = new Config('Ekino', 'XXX'); + $newRelic = new Config('Ekino', 'api.host', 'XXX'); $this->assertSame('Ekino', $newRelic->getName()); $this->assertSame('XXX', $newRelic->getApiKey()); + $this->assertSame('api.host', $newRelic->getApiHost()); $this->assertEmpty($newRelic->getCustomEvents()); $this->assertEmpty($newRelic->getCustomMetrics()); From 5d34ac5eed675533f70da0a9c5f2c888d58774d9 Mon Sep 17 00:00:00 2001 From: Szymon Piasecki Date: Thu, 16 Jan 2020 07:04:06 +0100 Subject: [PATCH 2/5] Add api_host configuration property --- CHANGELOG.md | 4 ++++ Command/NotifyDeploymentCommand.php | 6 +++--- DependencyInjection/Configuration.php | 1 + DependencyInjection/EkinoNewRelicExtension.php | 1 + NewRelic/Config.php | 9 ++++++++- README.md | 1 + Tests/NewRelic/ConfigTest.php | 3 ++- 7 files changed, 20 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index daf4cba..641d32a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## UNRELEASED +### Added + +- Added `api_host` configuration property used by `NotifyDeploymentCommand` + ## v2.1.3 ### Added diff --git a/Command/NotifyDeploymentCommand.php b/Command/NotifyDeploymentCommand.php index 66dfd71..6c29c80 100644 --- a/Command/NotifyDeploymentCommand.php +++ b/Command/NotifyDeploymentCommand.php @@ -74,7 +74,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $exitCode = 0; foreach ($appNames as $appName) { - $response = $this->performRequest($this->newrelic->getApiKey(), $this->createPayload($appName, $input)); + $response = $this->performRequest($this->newrelic->getApiHost(), $this->newrelic->getApiKey(), $this->createPayload($appName, $input)); switch ($response['status']) { case 200: @@ -99,7 +99,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int return $exitCode; } - public function performRequest(string $api_key, string $payload): array + public function performRequest(string $api_host, string $api_key, string $payload): array { $headers = [ \sprintf('x-api-key: %s', $api_key), @@ -116,7 +116,7 @@ public function performRequest(string $api_key, string $payload): array ]; $level = \error_reporting(0); - $content = \file_get_contents('https://api.newrelic.com/deployments.xml', false, \stream_context_create($context)); + $content = \file_get_contents(\sprintf('https://%s/deployments.xml', $api_host), false, \stream_context_create($context)); \error_reporting($level); if (false === $content) { $error = \error_get_last(); diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index c248353..4c5b25c 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -37,6 +37,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->scalarNode('interactor')->end() ->booleanNode('twig')->defaultValue(\class_exists(Environment::class))->end() ->scalarNode('api_key')->defaultValue(null)->end() + ->scalarNode('api_host')->defaultValue('api.newrelic.com')->end() ->scalarNode('license_key')->defaultValue(null)->end() ->scalarNode('application_name')->defaultValue(null)->end() ->arrayNode('deployment_names') diff --git a/DependencyInjection/EkinoNewRelicExtension.php b/DependencyInjection/EkinoNewRelicExtension.php index 5f174d1..6deda85 100644 --- a/DependencyInjection/EkinoNewRelicExtension.php +++ b/DependencyInjection/EkinoNewRelicExtension.php @@ -71,6 +71,7 @@ public function load(array $configs, ContainerBuilder $container): void ->setArguments( [ '$name' => $config['application_name'], + '$apiHost' => $config['api_host'], '$apiKey' => $config['api_key'], '$licenseKey' => $config['license_key'], '$xmit' => $config['xmit'], diff --git a/NewRelic/Config.php b/NewRelic/Config.php index e557c60..48bd900 100644 --- a/NewRelic/Config.php +++ b/NewRelic/Config.php @@ -20,6 +20,7 @@ class Config { private $name; private $apiKey; + private $apiHost; private $licenseKey; private $xmit; private $customEvents; @@ -27,10 +28,11 @@ class Config private $customParameters; private $deploymentNames; - public function __construct(?string $name, string $apiKey = null, string $licenseKey = null, bool $xmit = false, array $deploymentNames = []) + public function __construct(?string $name, string $apiHost, string $apiKey = null, string $licenseKey = null, bool $xmit = false, array $deploymentNames = []) { $this->name = (!empty($name) ? $name : \ini_get('newrelic.appname')) ?: ''; $this->apiKey = $apiKey; + $this->apiHost = $apiHost; $this->licenseKey = (!empty($licenseKey) ? $licenseKey : \ini_get('newrelic.license')) ?: ''; $this->xmit = $xmit; $this->deploymentNames = $deploymentNames; @@ -105,6 +107,11 @@ public function getApiKey(): ?string return $this->apiKey; } + public function getApiHost(): string + { + return $this->apiHost; + } + public function getLicenseKey(): ?string { return $this->licenseKey; diff --git a/README.md b/README.md index 2f492b6..23b7a7a 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ ekino_new_relic: # as php ini-value deployment_names: ~ # default value is 'application_name', supports string array or semi-colon separated string api_key: # New Relic API + api_host: ~ # New Relic API Host (default value is api.newrelic.com, for EU should be set to api.eu.newrelic.com ) license_key: # New Relic license key (optional, default value is read from php.ini) xmit: false # if you want to record the metric data up to the point newrelic_set_appname is called, set this to true (default: false) logging: false # If true, logs all New Relic interactions to the Symfony log (default: false) diff --git a/Tests/NewRelic/ConfigTest.php b/Tests/NewRelic/ConfigTest.php index 36116b7..0cbb628 100644 --- a/Tests/NewRelic/ConfigTest.php +++ b/Tests/NewRelic/ConfigTest.php @@ -20,10 +20,11 @@ class ConfigTest extends TestCase { public function testGeneric() { - $newRelic = new Config('Ekino', 'XXX'); + $newRelic = new Config('Ekino', 'api.host', 'XXX'); $this->assertSame('Ekino', $newRelic->getName()); $this->assertSame('XXX', $newRelic->getApiKey()); + $this->assertSame('api.host', $newRelic->getApiHost()); $this->assertEmpty($newRelic->getCustomEvents()); $this->assertEmpty($newRelic->getCustomMetrics()); From b208af6bb3b7477898b26ae0ec8965c49c460989 Mon Sep 17 00:00:00 2001 From: Szymon Piasecki Date: Tue, 21 Jan 2020 11:45:22 +0100 Subject: [PATCH 3/5] Reverted BC breaks in Config --- Command/NotifyDeploymentCommand.php | 6 +++--- DependencyInjection/Configuration.php | 2 +- NewRelic/Config.php | 6 +++--- Tests/NewRelic/ConfigTest.php | 4 +++- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Command/NotifyDeploymentCommand.php b/Command/NotifyDeploymentCommand.php index 6c29c80..42cc141 100644 --- a/Command/NotifyDeploymentCommand.php +++ b/Command/NotifyDeploymentCommand.php @@ -74,7 +74,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $exitCode = 0; foreach ($appNames as $appName) { - $response = $this->performRequest($this->newrelic->getApiHost(), $this->newrelic->getApiKey(), $this->createPayload($appName, $input)); + $response = $this->performRequest($this->newrelic->getApiKey(), $this->createPayload($appName, $input), $this->newrelic->getApiHost()); switch ($response['status']) { case 200: @@ -99,7 +99,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int return $exitCode; } - public function performRequest(string $api_host, string $api_key, string $payload): array + public function performRequest(string $api_key, string $payload, string $api_host = null): array { $headers = [ \sprintf('x-api-key: %s', $api_key), @@ -116,7 +116,7 @@ public function performRequest(string $api_host, string $api_key, string $payloa ]; $level = \error_reporting(0); - $content = \file_get_contents(\sprintf('https://%s/deployments.xml', $api_host), false, \stream_context_create($context)); + $content = \file_get_contents(\sprintf('https://%s/deployments.xml', $api_host ?? 'api.newrelic.com'), false, \stream_context_create($context)); \error_reporting($level); if (false === $content) { $error = \error_get_last(); diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 4c5b25c..9033d38 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -37,7 +37,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->scalarNode('interactor')->end() ->booleanNode('twig')->defaultValue(\class_exists(Environment::class))->end() ->scalarNode('api_key')->defaultValue(null)->end() - ->scalarNode('api_host')->defaultValue('api.newrelic.com')->end() + ->scalarNode('api_host')->defaultValue(null)->end() ->scalarNode('license_key')->defaultValue(null)->end() ->scalarNode('application_name')->defaultValue(null)->end() ->arrayNode('deployment_names') diff --git a/NewRelic/Config.php b/NewRelic/Config.php index 48bd900..07c56dc 100644 --- a/NewRelic/Config.php +++ b/NewRelic/Config.php @@ -20,7 +20,7 @@ class Config { private $name; private $apiKey; - private $apiHost; + private $apiHost = null; private $licenseKey; private $xmit; private $customEvents; @@ -28,7 +28,7 @@ class Config private $customParameters; private $deploymentNames; - public function __construct(?string $name, string $apiHost, string $apiKey = null, string $licenseKey = null, bool $xmit = false, array $deploymentNames = []) + public function __construct(?string $name, string $apiKey = null, string $licenseKey = null, bool $xmit = false, array $deploymentNames = [], ?string $apiHost = null) { $this->name = (!empty($name) ? $name : \ini_get('newrelic.appname')) ?: ''; $this->apiKey = $apiKey; @@ -107,7 +107,7 @@ public function getApiKey(): ?string return $this->apiKey; } - public function getApiHost(): string + public function getApiHost(): ?string { return $this->apiHost; } diff --git a/Tests/NewRelic/ConfigTest.php b/Tests/NewRelic/ConfigTest.php index 0cbb628..ac05759 100644 --- a/Tests/NewRelic/ConfigTest.php +++ b/Tests/NewRelic/ConfigTest.php @@ -20,7 +20,7 @@ class ConfigTest extends TestCase { public function testGeneric() { - $newRelic = new Config('Ekino', 'api.host', 'XXX'); + $newRelic = new Config('Ekino', 'XXX', null, false, [], 'api.host'); $this->assertSame('Ekino', $newRelic->getName()); $this->assertSame('XXX', $newRelic->getApiKey()); @@ -76,5 +76,7 @@ public function testDefaults() $this->assertNotNull($newRelic->getLicenseKey()); $this->assertSame(\ini_get('newrelic.license') ?: '', $newRelic->getLicenseKey()); + + $this->assertNull($newRelic->getApiHost()); } } From 4af2a1d0720ba3a65679e6297a17b0c4dff05d6b Mon Sep 17 00:00:00 2001 From: Szymon Piasecki Date: Tue, 21 Jan 2020 11:47:18 +0100 Subject: [PATCH 4/5] Added nullable type in performRequest --- Command/NotifyDeploymentCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Command/NotifyDeploymentCommand.php b/Command/NotifyDeploymentCommand.php index 42cc141..837ebcb 100644 --- a/Command/NotifyDeploymentCommand.php +++ b/Command/NotifyDeploymentCommand.php @@ -99,7 +99,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int return $exitCode; } - public function performRequest(string $api_key, string $payload, string $api_host = null): array + public function performRequest(string $api_key, string $payload, ?string $api_host = null): array { $headers = [ \sprintf('x-api-key: %s', $api_key), From c36e1034da489ed12776f87f72db0163583b4ea2 Mon Sep 17 00:00:00 2001 From: Szymon Piasecki Date: Tue, 21 Jan 2020 12:04:39 +0100 Subject: [PATCH 5/5] Extension Config setup --- DependencyInjection/EkinoNewRelicExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DependencyInjection/EkinoNewRelicExtension.php b/DependencyInjection/EkinoNewRelicExtension.php index 6deda85..11cf8f4 100644 --- a/DependencyInjection/EkinoNewRelicExtension.php +++ b/DependencyInjection/EkinoNewRelicExtension.php @@ -71,11 +71,11 @@ public function load(array $configs, ContainerBuilder $container): void ->setArguments( [ '$name' => $config['application_name'], - '$apiHost' => $config['api_host'], '$apiKey' => $config['api_key'], '$licenseKey' => $config['license_key'], '$xmit' => $config['xmit'], '$deploymentNames' => $config['deployment_names'], + '$apiHost' => $config['api_host'], ] );