-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
54 lines (42 loc) · 1.47 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict'
const http = require('http')
const { Tags, FORMAT_HTTP_HEADERS } = require('opentracing')
const MetricsTracer = require('../src')
const prometheusReporter = new MetricsTracer.PrometheusReporter()
const metricsTracer = new MetricsTracer('my-server', [prometheusReporter])
const PORT = process.env.PORT || 3000
const server = http.createServer((req, res) => {
// Instrumentation
const requestSpan = metricsTracer.startSpan('http_request', {
childOf: metricsTracer.extract(FORMAT_HTTP_HEADERS, req.headers)
})
const headers = {}
metricsTracer.inject(requestSpan, FORMAT_HTTP_HEADERS, headers)
requestSpan.setTag(Tags.HTTP_URL, req.url)
requestSpan.setTag(Tags.HTTP_METHOD, req.method || 'GET')
requestSpan.setTag(Tags.HTTP_STATUS_CODE, 200)
requestSpan.setTag(Tags.SPAN_KIND_RPC_CLIENT, true)
// Dummy router: GET /metrics
if (req.url === '/metrics') {
requestSpan.finish()
res.writeHead(200, {
'Content-Type': MetricsTracer.PrometheusReporter.Prometheus.register.contentType
})
res.end(prometheusReporter.metrics())
return
}
// My child operation like DB access
const childOperationSpan = metricsTracer.startSpan('my_operation', {
childOf: requestSpan
})
setTimeout(() => {
childOperationSpan.finish()
res.writeHead(200, headers)
res.end('Ok')
requestSpan.finish()
}, 30)
})
server.listen(PORT, (err) => {
// eslint-disable-next-line
console.log(err || `Server is listening on ${PORT}`)
})