Created
October 17, 2024 15:16
-
-
Save Kilowhisky/60818b140782defcd241474254b7e689 to your computer and use it in GitHub Desktop.
NewRelic EventAPI NodeJs RecordCustomEvent
This file contains hidden or 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
export async function recordCustomEvent(type: string, event: Record<string, string | number>) { | |
try { | |
const ACCOUNT_ID = process.env.NEW_RELIC_ACCOUNT_ID; | |
const LICENSE_KEY = process.env.NEW_RELIC_LICENSE_KEY; | |
const APP_NAME = process.env.NEW_RELIC_APP_NAME; | |
if (!ACCOUNT_ID || !LICENSE_KEY || !APP_NAME) { | |
console.warn("NEW_RELIC_ACCOUNT_ID or NEW_RELIC_LICENSE_KEY or NEW_RELIC_APP_NAME not found in ENV"); | |
return; | |
} | |
const url = `https://insights-collector.newrelic.com/v1/accounts/${ACCOUNT_ID}/events` | |
const response = await fetch(url, { | |
method: 'POST', | |
headers: { | |
"Api-Key": LICENSE_KEY, | |
"Content-Type": "application/json" | |
}, | |
body: JSON.stringify({ | |
...event, | |
eventType: type, | |
entityName: APP_NAME | |
}) | |
}); | |
if (!response.ok) { | |
console.warn('got non success response code', response.status, await response.text()); | |
} | |
} catch (err) { | |
console.warn('Failed to send event to newrelic', type, err); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment