5

I'm trying to get a little example of Jaeger working using Node.js, but I can't get the Jaeger UI to display any data or show anything.

I have read this question: uber/jaeger-client-node: backend wont receive data but this hasn't helped in my case.

I'm running the Jaeger back end in a docker container using:

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

The code for my example is:

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

Any help as to what I am doing wrong would be much appreciated!

ChilledPanda
  • 75
  • 1
  • 6

2 Answers2

2

You need to add some more properties to your config options. For reporter deployed on localhost and local sampler strategy :

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

Replace localhost by server or route name to target another host for Jeager runtime.

lbroudoux
  • 101
  • 6
  • Thank you, I had thought after inspecting the code for the jaeger-client-node package that if none of these fields was specified then it would create a sampler and reporter using default settings but this has fixed my problem. Thanks again! – ChilledPanda Dec 12 '17 at 19:23
  • If you are like me and use docker; don't forget to make sure that the udp port is accessible from your node js app! It doesn't complain much when that doesn't work. – worldsayshi Feb 10 '18 at 22:39
  • @worldsayshi Can you please show us how you made UDP port accessible from your node js app. I am new to Docker and I think I am facing the same issue, I can see spans generated in the logs but I can not see my service in Jaeger UI dropdown. Thank you! – Abhishek Mar 08 '18 at 01:26
  • 2
    @Abhishek It might not help much but I've declared this under my jaeger image in docker-compose.yml: `+ ports: - "5775:5775/udp" - "6831:6831/udp" - "6832:6832/udp" - "5778:5778" - "16686:16686" - "14268:14268" - "9411:9411"` – worldsayshi Mar 12 '18 at 10:12
  • 1
    And I also added corresponding port declarations in my docker-compose.yml for my node.js image: ` - "6831:6831/udp" - "6832:6832/udp"` – worldsayshi Mar 12 '18 at 10:13
  • Thank you @worldsayshi. My problem was I was not specifying `agentHost` in my client. I added `'agentHost': '192.168.99.100', 'agentPort': 6832` and now I can see my service in Jaeger UI. – Abhishek Mar 12 '18 at 22:25
  • In case you are deploying your service to Kubernetes make sure to specify the agentHost as simply 'jaeger-agent' (if you are using default all-in-one image) and not prefix it with either http:// or https://. – Ultimate_93 Apr 12 '20 at 12:44
0

I spent almost two days figuring this out. It does not work with "'agentHost': 'localhost'" when you are running your containers via separate docker-compose commands. I had one nodeJS microservice and one container for jaeger and the following combination worked for me.

  1. First step is to create a network that will be used to launch all your services as well as the Jaeger container.

    docker network create -d bridge my_network (replace my_network with your desired network name)
    
  2. Create a Jaeger container that runs in the same network

    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. Create a folder and create app.js file and paste the following code to create a 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();
    }
    

I have taken the reference of the above code from https://github.com/ibm-cloud-architecture/learning-distributed-tracing-101 repo from "lab-jaeger-nodejs/solution/service-a/app.js" folder.

  1. Create "package.json" in the same nodeJS folder where the above app.js file is created with the following code

     {
      "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. Create a "DOCKERfile" inside the same nodeJS folder with following code

     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. Create "docker-compose.yaml" file within the same folder.

     version: "3.7"
      services:
       service-a:
        build: ./service-a
        ports:
         - "8082:8082"
        networks:
         - my_network
      networks:
       my_network:
        external: true
        name: my_network
    
  4. Now run the following commands from terminal. This will run the nodeJS applicatin in the docker container

       docker-compose build
       docker-compose up
    
  5. Now run the http://localhost:8082/sayHello/John. You will see the response. Then open http://localhost:16686/ and you will be able to see the Jaeger UI with search. Search for "service-a" and you will be able to find the traces.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nitesh Malviya
  • 794
  • 1
  • 9
  • 17