Skip to content

Commit

Permalink
feat: API endpoint for sending gauge metrics to New Relic
Browse files Browse the repository at this point in the history
Implements #440
  • Loading branch information
mountaindude committed May 14, 2022
1 parent d957494 commit 13063a0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/api/newrelic_metric.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ const apiPostNewRelicMetric = {
type: 'string',
description: 'Metric name.',
example: 'memory.heap',
maxLength: 254,
},
type: {
type: 'string',
description: 'Metric type. Valid options are count, distribution, gauge, summary.',
description: 'Metric type.',
example: 'gauge',
enum: ['gauge'],
},
value: {
type: 'number',
Expand All @@ -28,7 +30,7 @@ const apiPostNewRelicMetric = {
},
interval: {
type: 'number',
description: 'The length of the time window. Required for count and summary metric types.',
description: 'The length of the time window (millisec). Required for count and summary metric types.',
},
attributes: {
type: 'array',
Expand All @@ -39,6 +41,7 @@ const apiPostNewRelicMetric = {
name: {
type: 'string',
example: 'host.name',
maxLength: 254,
},
value: {
type: 'string',
Expand Down
2 changes: 1 addition & 1 deletion src/config/production_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ Butler:

restServerEndpointsConfig:
newRelic:
postNewRelictMetric: # Setings used by post metric to New Relic API endpoint
postNewRelicMetric: # Setings used by post metric to New Relic API endpoint
# Note that the URL path should *not* be included in the url setting below!
# As of this writing the valid options are
# https://insights-collector.eu01.nr-data.net
Expand Down
13 changes: 9 additions & 4 deletions src/routes/newrelic_metric.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ async function handlerPostNewRelicMetric(request, reply) {
}

// Add attributes passed as parameters
if (request.body.attributes && request.body.attributes.length > 0) {
// eslint-disable-next-line no-restricted-syntax
for (const item of request.body.attributes) {
attributes[item.name] = item.value;
}
}

// Build New Relic metric common block
const common = {
Expand Down Expand Up @@ -65,7 +71,7 @@ async function handlerPostNewRelicMetric(request, reply) {
};

// eslint-disable-next-line no-restricted-syntax
for (const header of globals.config.get('Butler.restServerEndpointsConfig.newRelic.postNewRelictMetric.header')) {
for (const header of globals.config.get('Butler.restServerEndpointsConfig.newRelic.postNewRelicMetric.header')) {
headers[header.name] = header.value;
}

Expand All @@ -75,11 +81,10 @@ async function handlerPostNewRelicMetric(request, reply) {
if (res.status === 202) {
// Posting done without error
globals.logger.verbose(`NEWRELIC METRIC: Sent metric to New Relic`);
reply.code(202).send('ok');
reply.type('text/plain').code(202).send(res.statusText);
// reply.type('application/json; charset=utf-8').code(201).send(JSON.stringify(request.body));
} else {
// reply.send(httpErrors(500, 'Failed publishing MQTT message'));
reply.send(httpErrors(res.status, 'Failed posting metric to New Relic'));
reply.send(httpErrors(res.status, `Failed posting metric to New Relic: ${res.statusText}`));
}

// Required parameter is missing
Expand Down

0 comments on commit 13063a0

Please sign in to comment.