-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fully functional New Relic sink (#13)
- Loading branch information
Showing
7 changed files
with
193 additions
and
29 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './recorder'; | ||
export * from './metrics'; | ||
export * from './sink'; | ||
export * from './sink'; | ||
export * from './sinks/newrelic'; |
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,61 @@ | ||
import { Sink } from '../sink'; | ||
import { Metrics, MetricType } from '../metrics'; | ||
|
||
enum NRMetricType { | ||
Count = 'count', | ||
Distribution = 'distribution', | ||
Gauge = 'gauge', | ||
Summary = 'summary', | ||
UniqueCount = 'uniqueCount', | ||
} | ||
|
||
class NRMetric { | ||
'interval.ms': number; | ||
constructor( | ||
protected readonly name: string, | ||
protected readonly type: NRMetricType, | ||
protected readonly value: any = 1, | ||
protected attributes = {}, | ||
protected timestamp = Date.now(), | ||
) { | ||
this['interval.ms'] = 1; | ||
} | ||
} | ||
|
||
export class NewRelicSink implements Sink { | ||
constructor( | ||
protected readonly licenseKey: string, | ||
protected readonly apiEndpoint = 'https://metric-api.eu.newrelic.com/metric/v1' | ||
) {} | ||
|
||
async send(metrics: Metrics): Promise<void> { | ||
const nrMetrics = []; | ||
for (const metric of metrics) { | ||
switch (metric.type) { | ||
case MetricType.Counter: | ||
nrMetrics.push(new NRMetric(metric.name, NRMetricType.Count, metric.value, metric.metadata)); | ||
break; | ||
case MetricType.Gauge: | ||
nrMetrics.push(new NRMetric(metric.name, NRMetricType.Gauge, metric.value, metric.metadata)); | ||
break; | ||
default: | ||
console.log(`NewRelicSink does not have support for '${metric.type}' metrics yet`); | ||
} | ||
} | ||
|
||
// Send the metric to the New Relic Metrics API | ||
const response = await fetch(this.apiEndpoint, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-License-Key': this.licenseKey | ||
}, | ||
body: JSON.stringify([{ metrics: nrMetrics }]) | ||
}); | ||
|
||
// Check the response status and throw an error if it's not successful | ||
if (!response.ok) { | ||
throw new Error(`Failed to send metrics to New Relic Metrics API: ${response.status} ${response.statusText}`); | ||
} | ||
} | ||
} |
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,45 @@ | ||
import fetchMock, { enableFetchMocks } from 'jest-fetch-mock'; | ||
import { NewRelicSink } from '../../src/sinks/newrelic'; | ||
import { Metric, MetricType } from '../../src/metrics'; | ||
|
||
describe('Recorder', function() { | ||
it('Sends metrics to New Relic - Response OK', async function() { | ||
enableFetchMocks(); | ||
jest.useFakeTimers(); | ||
jest.setSystemTime(new Date('2023-01-01')); // 1672531200000 | ||
|
||
fetchMock.mockResponseOnce(JSON.stringify({requestId: 'f0e7bfff-001a-b000-0000-01682bcf4565'}), {status: 202, headers: { 'Content-Type': 'application/json; charset=UTF-8' }}); | ||
|
||
const sink = new NewRelicSink('FAKE_LICENSE_KEY', 'https://newrelic.fake'); | ||
const metrics = [ | ||
new Metric('test_metric_one', MetricType.Counter, 1, { test: 'one' }), | ||
new Metric('test_metric_two', MetricType.Counter, 5, { test: 'two' }), | ||
new Metric('test_metric_three', MetricType.Gauge, 2876176, { test: 'three' }), | ||
]; | ||
|
||
const expectedFetchCall = { | ||
body: JSON.stringify([{metrics: [ | ||
{ name: 'test_metric_one', type: 'count', value: 1, attributes: {test: 'one'}, timestamp: 1672531200000, 'interval.ms': 1 }, | ||
{ name: 'test_metric_two', type: 'count', value: 5, attributes: {test: 'two'}, timestamp: 1672531200000, 'interval.ms': 1 }, | ||
{ name: 'test_metric_three', type: 'gauge', value: 2876176, attributes: {test: 'three'}, timestamp: 1672531200000, 'interval.ms': 1 } | ||
]}]), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-License-Key': 'FAKE_LICENSE_KEY' | ||
}, | ||
method: 'POST' | ||
}; | ||
|
||
await sink.send(metrics); | ||
expect(fetchMock.mock.calls.length).toEqual(1); | ||
expect(fetchMock.mock.calls[0][0]).toEqual('https://newrelic.fake'); | ||
expect(fetchMock.mock.calls[0][1]).toEqual(expectedFetchCall); | ||
}); | ||
|
||
it('Sends metrics to New Relic - Response KO', async function() { | ||
enableFetchMocks(); | ||
fetchMock.mockResponseOnce('', { status: 400 }); | ||
const sink = new NewRelicSink('', 'https://newrelic.fake'); | ||
expect(async () => {await sink.send([]);}).rejects.toThrowError('Failed to send metrics to New Relic Metrics API: 400 Bad Request'); | ||
}); | ||
}); |