diff --git a/config/heartbeat.php b/config/heartbeat.php index 2e2ebd7..3e2b574 100644 --- a/config/heartbeat.php +++ b/config/heartbeat.php @@ -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', diff --git a/src/Channels/NewrelicChannel.php b/src/Channels/NewrelicChannel.php new file mode 100644 index 0000000..6f7caad --- /dev/null +++ b/src/Channels/NewrelicChannel.php @@ -0,0 +1,49 @@ +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 + ], + ]); + } +} diff --git a/tests/Unit/Channels/NewrelicChannelTest.php b/tests/Unit/Channels/NewrelicChannelTest.php new file mode 100644 index 0000000..f693923 --- /dev/null +++ b/tests/Unit/Channels/NewrelicChannelTest.php @@ -0,0 +1,118 @@ +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); + } +}