Skip to content

ChoTotOSS/jaeger-client-nodejs

Repository files navigation

Internal Client for Jaeger using nodejs

  • Ref: https://github.com/jaegertracing/jaeger-client-node

Node.js Package Node.js CI

How to use

1. Create jeager.js file on your project (libs directory recommended)

const { initTracer } = require('jaeger-client');

const config = {
  serviceName: 'your-service-name': String,
  reporter: {
    name: 'your-service-name-reporter' : String,
    logSpans: true,
    agentHost: 'your-reporter-host': String,
    agentPort: 'your-report-port': Number
  },
  sampler: {
    type: 'const',
    param: 1.0
  }
};

const options = {
  tags: {
    'service': 'your-service-tag': String,
    'version': 'your-service-version': String,
  }
}
module.exports = initTracer(config,options);

example:

import { initTracer } from "jaeger-client";

const config = {
  serviceName: "pricer",
  reporter: {
    name: "pricer-reporter",
    logSpans: true,
    agentHost: process.env.JAEGER_AGENT_HOST || "localhost",
    agentPort: process.env.JAEGER_AGENT_PORT || 6832
  },
  sampler: {
    type: "const",
    param: 1.0
  }
};
const options = {
  tags: {
    service: "pricer",
    version: process.env.npm_package_version || "1.0.0"
  }
};

export default initTracer(config, options);

2. Create Middleware file (middlewares directory recommended)

const jaeger = require("path-to-lib/jaeger.js");
const JaegerMiddleware = require("@chototoss/jaeger-client-nodejs");
const jaegerMiddleware = new JaegerMiddleware(jaeger);
module.exports = jaegerMiddleware;

After created, your structure should look like

├── libs
│   └── jaeger.js
├── middlewares
│   └── jaeger-middleware.js
├── package.json
├── routes
│   └── index.js
├── server.js
└── workers
    └── main-worker.js

3. Use for API

app.js

const jaegerMiddleware = require("path-to-middleware/jaeger-middleware.js");
app.use(jaegerMiddleware.handleLogBeforeResponse);
app.use(
  `your_route_prefix`,
  jaegerMiddleware.createSpanAfterReceivedRequest,
  your_router
);

alternative ways to use

const jaegerMiddleware = require("path-to-middleware/jaeger-middleware.js");
app.use(
  `your_end_point`,
  jaegerMiddleware.handleLogBeforeResponse,
  jaegerMiddleware.createSpanAfterReceivedRequest,
  your_end_point_handler
);

4. Use for worker

main-worker.js

const jaegerMiddleware = require("path-to-middleware/jaeger-middleware.js");
const span = jaegerMiddleware.initSpanForWorker("worker-name",data,data.headers);
span.log(something)
span.finish()

Q&A