5

我正在尝试获取 Jaeger 使用 Node.js 工作的一个小示例,但我无法让 Jaeger UI 显示任何数据或显示任何内容。

我读过这个问题:uber/jaeger-client-node: backend wont receive data但这对我来说没有帮助。

我在 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);
});

任何关于我做错了什么的帮助将不胜感激!

4

2 回答 2

2

您需要在配置选项中添加更多属性。对于部署在本地主机和本地采样器策略上的记者:

var config = {
  'serviceName': 'my-awesome-service',
  'reporter': {
    'logSpans': true,
    'agentHost': 'localhost',
    'agentPort': 6832
  },
  'sampler': {
    'type': 'probabilistic',
    'param': 1.0
  }
};

替换localhost为服务器或路由名称以针对 Jeager 运行时的另一个主机。

于 2017-12-11T15:35:05.267 回答
0

我花了将近两天的时间来解决这个问题。当您通过单独的 docker-compose 命令运行容器时,它不适用于“'agentHost':'localhost'”。我有一个 nodeJS 微服务和一个用于 jaeger 的容器,以下组合对我有用。

  1. 第一步是创建一个网络,用于启动所有服务以及 Jaeger 容器。

    docker network create -d bridge my_network (replace my_network with your desired network name)
    
  2. 创建在同一网络中运行的 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
    
  3. 创建一个文件夹并创建 app.js 文件并粘贴以下代码以创建 nodeJS 微服务。

     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();
    }
    

我已经从“lab-jaeger-nodejs/solution/service-a/app.js”的https://github.com/ibm-cloud-architecture/learning-distributed-tracing-101 repo中引用了上述代码文件夹。

  1. 在创建上述 app.js 文件的同一 nodeJS 文件夹中创建“package.json”,代码如下

     {
      "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"
     }
    }
    
  2. 使用以下代码在同一 nodeJS 文件夹中创建一个“DOCKERfile”

     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" ]
    
  3. 在同一文件夹中创建“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
    
  4. 现在从终端运行以下命令。这将在 docker 容器中运行 nodeJS 应用程序

       docker-compose build
       docker-compose up
    
  5. 现在运行 http://localhost:8082/sayHello/John。您将看到响应。然后打开 http://localhost:16686/,您将能够看到 Jaeger UI 的搜索。搜索“service-a”,您将能够找到痕迹。

于 2021-07-29T06:05:50.503 回答