Skip to content

Instantly share code, notes, and snippets.

@icpenguins
Last active January 27, 2021 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icpenguins/a2f120f391105b41725dd51501092f7e to your computer and use it in GitHub Desktop.
Save icpenguins/a2f120f391105b41725dd51501092f7e to your computer and use it in GitHub Desktop.
Fetch tolerating, frustrated, satisfactory count, and APDEX threshold on an hourly time slice for key transactions using New Relic APIs
// Use your own methods for GET and POST requests; I created my own HTTP handler
const { getJSON: gj } = require('./common/http')
const { sendData: sd } = require('./common/http')
// Specify the account ID you would like to query
const accountId = -1
// Get or create an API key for the account ID (https://docs.newrelic.com/docs/apis/get-started/intro-apis/types-new-relic-api-keys#user-api-key)
const apiKey = 'NRAK-xxxxxxxxxxxx'
// Get the key transactions from the account
// https://rpm.newrelic.com/api/explore/key_transactions/list
async function getKeyTransactions() {
let r = await gj(
apiKey,
'https://api.newrelic.com/v2/key_transactions.json'
)
.catch(e => {
console.log(e)
})
return r
}
// Make a request to NerdGraph for the dataset details on the key transaction.
// Play with NerdGraph at the following location: https://api.newrelic.com/graphiql
async function getNerdGraph(request) {
// Update the NRQL query SINCE xxx and even add UNTIL xxx if you need a specific time window
// Then change the TIMESERIES value to create windows of aggregation during the timespan (https://docs.newrelic.com/docs/query-your-data/nrql-new-relic-query-language/get-started/nrql-syntax-clauses-functions)
let graph = {
"query": `{
actor {
account(id: ${request.accountId}) {
nrql(query: "SELECT apdex(duration, t: ${request.apdexThreshold}) FROM Transaction WHERE name LIKE '%${request.transaction}' AND appId = ${request.appId} SINCE 1 day ago TIMESERIES 1 hour", timeout: 5) {
nrql
metadata {
timeWindow {
begin
end
}
}
results
}
}
}
}`
}
let r = await sd(
'https://api.newrelic.com/graphql',
{
headers: {
"Content-Type": "application/json",
"X-Api-Key": apiKey
}
},
graph
)
.catch(e => {
console.log(e)
})
return r.data
}
async function printTransaction() {
let t = await getKeyTransactions()
for (let i = 0; i < t.data.key_transactions.length; i++) {
let ds = await getNerdGraph({
accountId: accountId,
appId: t.data.key_transactions[i].links.application,
// If there is no custom APDEX set for the transaction, it will be undefined. Handle that case and use a default APDEX threshold.
apdexThreshold: t.data.key_transactions[i].application_summary !== undefined ? t.data.key_transactions[i].application_summary.apdex_target : 0.5,
transaction: t.data.key_transactions[i].transaction_name
})
console.log(JSON.stringify(ds, null, 2))
}
}
// Report on transactions
printTransaction()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment