Skip to content

Commit

Permalink
Add description of new relic integration on README. Fix importing fun…
Browse files Browse the repository at this point in the history
…ction problem about newrelic on server.js
  • Loading branch information
Berk Safranbolulu committed Apr 13, 2022
1 parent 969958e commit ab7c472
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ Voltran requires following configurations:
| [routing](#routing) | Object |
| [webpackConfiguration](#webpackConfiguration) | Object |
| [sassResources](#sassResources) | Array |
| [criticalCssDisabled](#criticalCssDisabled) | Boolean
| [criticalCssDisabled](#criticalCssDisabled) | Boolean |

#### appConfigFile

Expand Down Expand Up @@ -339,6 +339,12 @@ You can add sass resources to this field as string array. sass-resource-loader g

You can check [sass-resource-loader](https://github.com/shakacode/sass-resources-loader) for usage.

## New Relic Integration

Add `newrelicEnabled: true` on your config.

If you throw an error like `throw new Error({message: "Service error", code: 500})` from your fragments, Voltran detects the fields and sends each field to New Relic as a custom attribute. These fields appear with `_a` prefix to place in the first of rows on your new relic.

## Tech

Voltran uses a number of open source projects to work properly:
Expand Down
4 changes: 2 additions & 2 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* istanbul ignore file */
import newrelic from './universal/tools/newrelic/newrelic';
import newrelic, { addCustomAttrsToNewrelic } from './universal/tools/newrelic/newrelic';

import cookieParser from 'cookie-parser';
import { compose } from 'compose-middleware';
Expand Down Expand Up @@ -117,7 +117,7 @@ const cors = async (req, res, next) => {

const utils = async (req, res, next) => {
res.json = json => {
addCustomAttrsToNewrelic(json.message)
addCustomAttrsToNewrelic(json.message);
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.end(JSON.stringify(json));
};
Expand Down
24 changes: 13 additions & 11 deletions src/universal/tools/newrelic/newrelic.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,32 @@ let newrelic = null;
if (newrelicEnabled) {
// eslint-disable-next-line global-require
newrelic = require('newrelic');
console.log(" ---- NewRelic is ENABLED! ----")
console.log(' ---- NewRelic is ENABLED! ----');
} else {
console.log(" ---- NewRelic is DISABLED! ----")
console.log(' ---- NewRelic is DISABLED! ----');
}

const isJsonValid = (str) => {
const isJsonValid = str => {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
};

export const addCustomAttrsToNewrelic = (message) => {
const isValidJson = isJsonValid(message)
export const addCustomAttrsToNewrelic = message => {
if (!newrelic) return;
const isValidJson = isJsonValid(message);
if (!isValidJson) return;
const parsedMessage = JSON.parse(message)
const parsedMessage = JSON.parse(message);
// eslint-disable-next-line no-restricted-syntax
for (const key in parsedMessage) {
if (Object.hasOwnProperty.call(parsedMessage, key)) {
newrelic?.addCustomAttribute(`a_${key}`, parsedMessage[key])
newrelic?.addCustomAttribute(`a_${key}`, parsedMessage[key]);
}
}
newrelic?.addCustomAttribute("a_voltran.error.message", message)
}
newrelic?.addCustomAttribute('a_voltran.error.message', message);
};

export default newrelic;
export default newrelic;

0 comments on commit ab7c472

Please sign in to comment.