Skip to content

Commit

Permalink
Merge pull request #246 from spiasecki/feature-api-host-in-config
Browse files Browse the repository at this point in the history
Feature api host in config
  • Loading branch information
jderusse committed Jan 24, 2020
2 parents 1a208ee + c36e103 commit 8fcc93f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## UNRELEASED

### Added

- Added `api_host` configuration property used by `NotifyDeploymentCommand`

## v2.1.3

### Added
Expand Down
6 changes: 3 additions & 3 deletions Command/NotifyDeploymentCommand.php
Expand Up @@ -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->getApiKey(), $this->createPayload($appName, $input), $this->newrelic->getApiHost());

switch ($response['status']) {
case 200:
Expand All @@ -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_key, string $payload, ?string $api_host = null): array
{
$headers = [
\sprintf('x-api-key: %s', $api_key),
Expand All @@ -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 ?? 'api.newrelic.com'), false, \stream_context_create($context));
\error_reporting($level);
if (false === $content) {
$error = \error_get_last();
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -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(null)->end()
->scalarNode('license_key')->defaultValue(null)->end()
->scalarNode('application_name')->defaultValue(null)->end()
->arrayNode('deployment_names')
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/EkinoNewRelicExtension.php
Expand Up @@ -75,6 +75,7 @@ public function load(array $configs, ContainerBuilder $container): void
'$licenseKey' => $config['license_key'],
'$xmit' => $config['xmit'],
'$deploymentNames' => $config['deployment_names'],
'$apiHost' => $config['api_host'],
]
);

Expand Down
9 changes: 8 additions & 1 deletion NewRelic/Config.php
Expand Up @@ -20,17 +20,19 @@ class Config
{
private $name;
private $apiKey;
private $apiHost = null;
private $licenseKey;
private $xmit;
private $customEvents;
private $customMetrics;
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 $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;
$this->apiHost = $apiHost;
$this->licenseKey = (!empty($licenseKey) ? $licenseKey : \ini_get('newrelic.license')) ?: '';
$this->xmit = $xmit;
$this->deploymentNames = $deploymentNames;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion Tests/NewRelic/ConfigTest.php
Expand Up @@ -20,10 +20,11 @@ class ConfigTest extends TestCase
{
public function testGeneric()
{
$newRelic = new Config('Ekino', 'XXX');
$newRelic = new Config('Ekino', 'XXX', null, false, [], 'api.host');

$this->assertSame('Ekino', $newRelic->getName());
$this->assertSame('XXX', $newRelic->getApiKey());
$this->assertSame('api.host', $newRelic->getApiHost());

$this->assertEmpty($newRelic->getCustomEvents());
$this->assertEmpty($newRelic->getCustomMetrics());
Expand Down Expand Up @@ -75,5 +76,7 @@ public function testDefaults()

$this->assertNotNull($newRelic->getLicenseKey());
$this->assertSame(\ini_get('newrelic.license') ?: '', $newRelic->getLicenseKey());

$this->assertNull($newRelic->getApiHost());
}
}

0 comments on commit 8fcc93f

Please sign in to comment.