我正在尝试获得使用node.js工作的一些示例,但是我无法让jaeger ui显示任何数据或显示任何内容。
我已经阅读了以下问题:Uber/jaeger-client节点:后端不会接收数据,但这在我的情况下没有帮助。
我使用以下方式在Docker容器中运行Jaeger的后端:
docker run -d -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 -p5775:5775/udp -p6831:6831/udp -p6832:6832/udp -p5778:5778 -p16686:16686 -p14268:14268 -p9411:9411 jaegertracing/all-in-one:latest
我的示例代码是:
var initTracer = require('jaeger-client').initTracer;
// See schema https://github.com/jaegertracing/jaeger-client-node/blob/master/src/configuration.js#L37
var config = {
'serviceName': 'my-awesome-service'
};
var options = {
'tags': {
'my-awesome-service.version': '1.1.2'
}
//'metrics': metrics,
//'logger': logger
};
var tracer = initTracer(config, options);
const express = require('express');
const app = express();
// Handle a GET request on the root path
app.get('/', (req, res) => {
const span = tracer.startSpan('http_request');
res.send('Hello Jaeger');
span.log({'event': 'request_end'});
span.finish();
});
// Set up server
const server = app.listen(8000, () => {
let host = server.address().address;
let port = server.address().port;
console.log('Service_1 listening at http://%s:%s', host, port);
});
关于我做错了什么的任何帮助将不胜感激!
您需要在配置选项中添加更多属性。用于部署Local主机和本地采样器策略的记者:
var config = {
'serviceName': 'my-awesome-service',
'reporter': {
'logSpans': true,
'agentHost': 'localhost',
'agentPort': 6832
},
'sampler': {
'type': 'probabilistic',
'param': 1.0
}
};
用服务器或路由名称替换localhost
,以定位JEAGER运行时的另一个主机。
我花了将近两天的时间来弄清楚这一点。它与" AgentHost":" localhost'"不使用。当您通过单独的Docker-Compose命令运行容器时。我有一个Nodejs微服务和一个容器,用于Jaeger,以下组合对我有用。
-
第一步是创建一个网络,该网络将用于启动您的所有服务以及Jaeger容器。
docker network create -d bridge my_network (replace my_network with your desired network name)
-
创建一个在同一网络中运行的Jaeger容器
docker run --network my_network --name jaeger -d -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 -p5775:5775/udp -p6831:6831/udp -p6832:6832/udp -p5778:5778 -p16686:16686 -p14268:14268 -p9411:9411 jaegertracing/all-in-one:latest
-
创建一个文件夹并创建app.js文件并粘贴以下代码以创建nodejs microservice。
const express = require("express"); var http = require("http"); const app = express(); const port = 8082; const serviceName = process.env.SERVICE_NAME || "service-a"; // Initialize the Tracer const tracer = initTracer(serviceName); const opentracing = require("opentracing"); opentracing.initGlobalTracer(tracer); // Instrument every incoming request app.use(tracingMiddleWare); // Let's capture http error span app.get("/error", (req, res) => { res.status(500).send("some error (ノ ゜Д゜)ノ ︵ ┻━┻"); }); app.get("/sayHello/:name", (req, res) => { const span = tracer.startSpan("say-hello", , { childOf: req.span }); // Parse the handler input const name = req.params.name // show how to do a log in the span span.log({ event: 'name', message: `this is a log message for name ${name}` }) // show how to set a baggage item for context propagation (be careful is expensive) span.setBaggageItem('my-baggage', name) span.finish() res.send(response); }); app.disable("etag"); app.listen(port, () => console.log(`Service ${serviceName} listening on port ${port}!`) ); function initTracer(serviceName) { var initTracer1 = require("jaeger-client").initTracer; // See schema https://github.com/jaegertracing/jaeger-client-node/blob/master/src/configuration.js#L37 var config = { serviceName: serviceName, reporter: { logSpans: true, agentHost: "jaeger", agentPort: 6832, }, sampler: { type: "probabilistic", param: 1.0, }, }; var options = { logger: { info: function logInfo(msg) { console.log("INFO ", msg); }, error: function logError(msg) { console.log("ERROR", msg); }, }, }; return initTracer1(config, options); } function tracingMiddleWare(req, res, next) { const tracer = opentracing.globalTracer(); const wireCtx = tracer.extract(opentracing.FORMAT_HTTP_HEADERS, req.headers); // Creating our span with context from incoming request const span = tracer.startSpan(req.path, { childOf: wireCtx }); // Use the log api to capture a log span.log({ event: "request_received" }); // Use the setTag api to capture standard span tags for http traces span.setTag(opentracing.Tags.HTTP_METHOD, req.method); span.setTag( opentracing.Tags.SPAN_KIND, opentracing.Tags.SPAN_KIND_RPC_SERVER ); span.setTag(opentracing.Tags.HTTP_URL, req.path); // include trace ID in headers so that we can debug slow requests we see in // the browser by looking up the trace ID found in response headers const responseHeaders = {}; tracer.inject(span, opentracing.FORMAT_HTTP_HEADERS, responseHeaders); res.set(responseHeaders); // add the span to the request object for any other handler to use the span Object.assign(req, { span }); // finalize the span when the response is completed const finishSpan = () => { if (res.statusCode >= 500) { // Force the span to be collected for http errors span.setTag(opentracing.Tags.SAMPLING_PRIORITY, 1); // If error then set the span to error span.setTag(opentracing.Tags.ERROR, true); // Response should have meaning info to futher troubleshooting span.log({ event: "error", message: res.statusMessage }); } // Capture the status code span.setTag(opentracing.Tags.HTTP_STATUS_CODE, res.statusCode); span.log({ event: "request_end" }); span.finish(); }; res.on("finish", finishSpan); next(); }
我从https://github.com/ibm-cloud-architecture/learning-distributed-tracing-101 repo中获取了上述代码的参考app.js;文件夹。
-
创建"软件包"在同一nodejs文件夹中,以下代码创建上述app.js文件
{ "name": "service", "version": "0.0.0", "scripts": { "start": "node app.js", "debug": "npm start", }, "dependencies": { "express": "^4.17.1", "jaeger-client": "^3.15.0" }, "devDependencies": { "standard": "^14.0.2" } }
-
创建A dockerfile&quot&quot在同一nodejs文件夹中,带有以下代码
FROM node:12.9.1 # Create app directory WORKDIR /usr/src/app COPY package.json . RUN npm install # If you are building your code for production # RUN npm ci --only=production # Bundle app source COPY . . EXPOSE 8082 CMD [ "npm", "start" ]
-
创建" docker-compose.yaml"在同一文件夹中文件。
version: "3.7" services: service-a: build: ./service-a ports: - "8082:8082" networks: - my_network networks: my_network: external: true name: my_network
-
现在从终端运行以下命令。这将在Docker容器中运行Nodejs Applicatin
docker-compose build docker-compose up
-
现在运行http://localhost:8082/sayhello/john。您会看到回应。然后打开http://localhost:16686/,您将可以通过搜索看到Jaeger UI。搜索" Service-A"您将能够找到痕迹。