-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from eXolnet/feature/add-newrelic-channel
feat(channel): add channel for sending events to newrelic
- Loading branch information
Showing
3 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Exolnet\Heartbeat\Channels; | ||
|
||
use GuzzleHttp\Client as HttpClient; | ||
|
||
class NewrelicChannel | ||
{ | ||
/** | ||
* The HTTP client instance. | ||
* | ||
* @var \GuzzleHttp\Client | ||
*/ | ||
protected $http; | ||
|
||
/** | ||
* Create a new newrelic channel instance. | ||
* | ||
* @param \GuzzleHttp\Client $http | ||
* @return void | ||
*/ | ||
public function __construct(HttpClient $http) | ||
{ | ||
$this->http = $http; | ||
} | ||
|
||
/** | ||
* @param string $account | ||
* @param string $key | ||
* @param string $eventType | ||
* @param array $eventData | ||
* @return void | ||
*/ | ||
public function signal(string $account, string $key, string $eventType, array $eventData = []): void | ||
{ | ||
$url = 'https://insights-collector.newrelic.com/v1/accounts/' . $account . '/events'; | ||
|
||
$this->http->request('post', $url, [ | ||
'headers' => [ | ||
'Api-Key' => $key, | ||
'Content-Type' => 'application/json', | ||
], | ||
'json' => [ | ||
'eventType' => $eventType, | ||
...$eventData, | ||
], | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
|
||
namespace Exolnet\Heartbeat\Tests\Unit\Channels; | ||
|
||
use Exolnet\Heartbeat\Channels\NewrelicChannel; | ||
use Exolnet\Heartbeat\Tests\Unit\UnitTestCase; | ||
use GuzzleHttp\Client as HttpClient; | ||
use Mockery as m; | ||
|
||
class NewrelicChannelTest extends UnitTestCase | ||
{ | ||
/** | ||
* @var \Mockery\MockInterface|\Illuminate\Filesystem\Filesystem | ||
*/ | ||
protected $httpClient; | ||
|
||
/** | ||
* @var \Exolnet\Heartbeat\Channels\NewrelicChannel | ||
*/ | ||
protected $channel; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $url; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $key; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $eventType; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $account; | ||
|
||
/** | ||
* @return void | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->httpClient = m::mock(HttpClient::class); | ||
|
||
$this->channel = new NewrelicChannel($this->httpClient); | ||
|
||
$this->key = '1f2d4c5a6b8e9f0a1b2c3d4e5f6a7b8c'; | ||
|
||
$this->eventType = 'HeartbeatEvent'; | ||
|
||
$this->account = '587431'; | ||
|
||
$this->url = 'https://insights-collector.newrelic.com/v1/accounts/' . $this->account . '/events'; | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testInstanceOf() | ||
{ | ||
$this->assertInstanceOf(NewrelicChannel::class, $this->channel); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testSignal() | ||
{ | ||
$options = [ | ||
'headers' => [ | ||
'Api-Key' => $this->key, | ||
'Content-Type' => 'application/json', | ||
], | ||
'json' => [ | ||
'eventType' => $this->eventType | ||
] | ||
]; | ||
|
||
$this->httpClient->shouldReceive('request')->with('post', $this->url, $options); | ||
|
||
$this->channel->signal($this->account, $this->key, $this->eventType); | ||
|
||
$this->assertTrue(true); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testSignalWithAdditionalData() | ||
{ | ||
$eventData = [ | ||
'appName' => 'test', | ||
'env' => 'preprod' | ||
]; | ||
|
||
$options = [ | ||
'headers' => [ | ||
'Api-Key' => $this->key, | ||
'Content-Type' => 'application/json', | ||
], | ||
'json' => [ | ||
'eventType' => $this->eventType, | ||
...$eventData | ||
] | ||
]; | ||
|
||
$this->httpClient->shouldReceive('request')->with('post', $this->url, $options); | ||
|
||
$this->channel->signal($this->account, $this->key, $this->eventType, $eventData); | ||
|
||
$this->assertTrue(true); | ||
} | ||
} |