Skip to content

Commit

Permalink
feat(channel): add channel for sending events to newrelic
Browse files Browse the repository at this point in the history
  • Loading branch information
jylanglois-exosource committed Jan 12, 2025
1 parent f80982e commit c1ce0fb
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 0 deletions.
10 changes: 10 additions & 0 deletions config/heartbeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
],
],

'newrelic' => [
'channel' => 'newrelic',
'account' => '587431', // Fake newrelic account ID
'key' => '1f2d4c5a6b8e9f0a1b2c3d4e5f6a7b8c', // Fake newrelic API key
'eventType' => 'HeartbeatEvent', // Event name
'eventData' => [
//
]
],

'disk' => [
'channel' => 'disk',
'disk' => 'local',
Expand Down
49 changes: 49 additions & 0 deletions src/Channels/NewrelicChannel.php
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 voids
*/
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
],
]);
}
}
118 changes: 118 additions & 0 deletions tests/Unit/Channels/NewrelicChannelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?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);
}
}

0 comments on commit c1ce0fb

Please sign in to comment.