Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save anderson-marques/44e56a4529cbdccd22f7e54764e0b457 to your computer and use it in GitHub Desktop.
Save anderson-marques/44e56a4529cbdccd22f7e54764e0b457 to your computer and use it in GitHub Desktop.
Generate MetricData from api-call-event
// https://repl.it/@marquesanderson/generate-metric-data-from-api-call-event
const RxJS = require('rxjs')
const map = require('rxjs/operators').map
const concatAll = require('rxjs/operators').concatAll
const toArray = require('rxjs/operators').toArray
let apiCalls$ = RxJS.from([
{
accountId: 1,
method: 'POST',
requestHeaders: {},
responseHeaders: {},
responseTime: 250,
responseCode: 200
},
{
accountId: 1,
method: 'GET',
requestHeaders: {},
responseHeaders: {},
responseTime: 542,
responseCode: 503
},
])
const metricNames = [
{ range: 500, name: '5xx Server Error' },
{ range: 400, name: '4xx Bad Request' },
{ range: 300, name: '3xx Redirect' },
{ range: 200, name: '2xx Success' },
{ range: 100, name: '1xx Info' },
]
apiCalls$.pipe(
map((apiCall) => {
let metrics = []
metrics.push({
MetricName: metricNames.find((r) => apiCall.responseCode >= r.range ).name,
Unit: 'Count',
Value: 1
})
metrics.push({
MetricName: 'Response Time',
Unit: 'Milliseconds',
Value: apiCall.responseTime
})
return metrics
}),
concatAll(),
toArray()
).subscribe((data) => console.log('data', data))
/*
Result:
data [ { MetricName: '2xx Success', Unit: 'Count', Value: 1 },
{ MetricName: 'Response Time', Unit: 'Milliseconds', Value: 250 },
{ MetricName: '5xx Server Error', Unit: 'Count', Value: 1 },
{ MetricName: 'Response Time', Unit: 'Milliseconds', Value: 542 } ]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment