diff --git a/index.js b/index.js index dd57f23..5382354 100644 --- a/index.js +++ b/index.js @@ -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 { diff --git a/package-lock.json b/package-lock.json index 8ba5d36..77eabc0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.2.0", "license": "MIT", "dependencies": { + "@opentelemetry/api": "^1.7.0", "debug": "4.3.4", "prom-client": "15.0.0", "url-value-parser": "^2.2.0" diff --git a/package.json b/package.json index a146127..dc1240a 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/http_hook.js b/src/http_hook.js index 1c23f30..57b5da1 100644 --- a/src/http_hook.js +++ b/src/http_hook.js @@ -1,6 +1,7 @@ 'use strict' const Http = require('http') const { normalizeEndpoint } = require('./normalize/endpoint') +const opentelemetry = require('@opentelemetry/api') const init = function (client, config) { const opts = { @@ -8,6 +9,7 @@ const init = function (client, config) { 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: { @@ -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) @@ -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) diff --git a/tests/main.test.js b/tests/main.test.js index f82a92d..6e80ae1 100644 --- a/tests/main.test.js +++ b/tests/main.test.js @@ -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) }) })