Skip to content

Commit

Permalink
feat: add exemplar
Browse files Browse the repository at this point in the history
  • Loading branch information
wallet77 committed Dec 7, 2023
1 parent 0b0b742 commit 8a1acbd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const http = require('http')
const HTTPHook = require('./src/http_hook')
let METRICS_ROUTE = '/metrics'

client.register.setContentType(
client.Registry.OPENMETRICS_CONTENT_TYPE
)

const requestListener = async (req, res) => {
if (req.url === METRICS_ROUTE) {
try {
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
},
"license": "MIT",
"dependencies": {
"@opentelemetry/api": "^1.7.0",
"debug": "4.3.4",
"prom-client": "15.0.0",
"url-value-parser": "^2.2.0"
Expand Down
19 changes: 15 additions & 4 deletions src/http_hook.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
'use strict'
const Http = require('http')
const { normalizeEndpoint } = require('./normalize/endpoint')
const opentelemetry = require('@opentelemetry/api')

const init = function (client, config) {
const opts = {
histogram: {
name: 'http_request_duration_seconds',
help: 'request duration in seconds',
labelNames: ['status', 'method', 'route'],
enableExemplars: true,
buckets: config.HTTP_DURATION_BUCKETS || [0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2]
},
summary: {
Expand All @@ -21,13 +23,14 @@ const init = function (client, config) {
const routeHist = new client.Histogram(opts.histogram)
const routeSum = new client.Summary(opts.summary)

const exemplarLabels = {}

const emit = Http.Server.prototype.emit
Http.Server.prototype.emit = function (type) {
if (type === 'request') {
const [req, res] = [arguments[1], arguments[2]]

const hist = routeHist.startTimer()
const sum = routeSum.startTimer()
const start = process.hrtime()

res.on('finish', () => {
const url = new URL('http://' + req.headers.host + req.url)
Expand All @@ -37,8 +40,16 @@ const init = function (client, config) {
route: config.NORMALIZE_ENDPOINT ? normalizeEndpoint(req.url) : urlPath,
status: res.statusCode
}
sum(labels)
hist(labels)

const spanContext = opentelemetry.trace.getSpanContext(opentelemetry.context.active())
const traceId = spanContext && spanContext.traceId
exemplarLabels.traceID = traceId

const delta = process.hrtime(start)
const value = delta[0] + delta[1] / 1e9

routeSum.observe(value)
routeHist.observe({ labels, value, exemplarLabels })
})
}
return emit.apply(this, arguments)
Expand Down
2 changes: 1 addition & 1 deletion tests/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ describe('retry', () => {
})
apm.init()
const data = await apm.client.register.metrics()
expect(data.indexOf('TYPE myApp_process_cpu_user_seconds_total counter') > -1).toEqual(true)
expect(data.indexOf('TYPE myApp_process_cpu_user_seconds counter') > -1).toEqual(true)
})
})

0 comments on commit 8a1acbd

Please sign in to comment.