我正在尝试为使用 Express.js 用 Node.js 编写的微服务设置 Jaeger 跟踪。
我在我的 express 应用程序中添加了一个简单的 get 请求处理程序,当我通过 curl 访问端点时,我可以看到日志中生成了一个 span,但我在 Jaeger UI 中看不到我的服务名称。
// server.js
const initTracer = require('./tracing').initTracer;
const {Tags, FORMAT_HTTP_HEADERS} = require('opentracing')
const tracer = initTracer('demo-service')
app.get('/service/model/jaeger', function(req, res) {
var requestEndpoint = req.originalUrl
console.log('[services.server.js] GET received on:', requestEndpoint)
const parentSpanContext = tracer.extract(FORMAT_HTTP_HEADERS, req.headers)
const span = tracer.startSpan('http_get_' + requestEndpoint, {
childOf : parentSpanContext,
tags : {[Tags.SPAN_KIND]: Tags.SPAN_KIND_RPC_SERVER}
});
span.log({'event': `http_get_${requestEndpoint}_end`, 'value' : 'some_value_here'});
span.finish()
res.send('Hello Jaeger bro!')
})
// 跟踪.js
'use strict'
var initJaegerTracer = require('jaeger-client').initTracer
function initTracer(serviceName) {
var config = {
'serviceName': serviceName,
'sampler': {
'type': 'const',
'param': 1
},
'reporter': {
'logSpans': true,
}
}
var options = {
'logger': {
'info': function logInfo(msg) {
console.log('INFO ', msg)
},
'error': function logError(msg) {
console.log('ERROR', msg)
}
}
}
const tracer = initJaegerTracer(config, options)
//hook up nodejs process exit event
process.on('exit', () => {
console.log('flush out remaining span')
tracer.close()
})
//handle ctrl+c
process.on('SIGINT', () => {
process.exit()
})
return tracer
}
exports.initTracer = initTracer
我在日志中看到:
2018-03-08T01:03:34.519134479Z INFO Reporting span 9b88812951bcd52f:9b88812951bcd52f:0:1