Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcoedo/7bdc2162fa568b34ad898ffdcfdabbf5 to your computer and use it in GitHub Desktop.
Save rcoedo/7bdc2162fa568b34ad898ffdcfdabbf5 to your computer and use it in GitHub Desktop.
const express = require("express");
const app = express();
const port = process.env.PORT || 3001;
app.get("/time", (req, res) => {
res.json({ currentDate: new Date().getTime() });
});
app.listen(port, () => console.log(`Date service listening on port ${port}`));
const express = require("express");
// Import zipkin stuff
const { Tracer, ExplicitContext, BatchRecorder, jsonEncoder } = require("zipkin");
const { HttpLogger } = require("zipkin-transport-http");
const zipkinMiddleware = require("zipkin-instrumentation-express").expressMiddleware;
const ZIPKIN_ENDPOINT = process.env.ZIPKIN_ENDPOINT || "http://localhost:9411";
// Get ourselves a zipkin tracer
const tracer = new Tracer({
ctxImpl: new ExplicitContext(),
recorder: new BatchRecorder({
logger: new HttpLogger({
endpoint: `${ZIPKIN_ENDPOINT}/api/v2/spans`,
jsonEncoder: jsonEncoder.JSON_V2,
}),
}),
localServiceName: "date-service",
});
const app = express();
const port = process.env.PORT || 3001;
// Add zipkin express middleware
app.use(zipkinMiddleware({ tracer }));
app.get("/time", (req, res) => {
res.json({ currentDate: new Date().getTime() });
});
app.listen(port, () => console.log(`Date service listening on port ${port}`));
// Our date service controller will look like this
app.get("/time", async (req, res) => {
await tracer.local("awaiting 200ms delay", () => delay(200));
res.json({ currentDate: new Date().getTime() });
});
// We can define a delay function with this one line
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
version: "2"
services:
storage:
image: openzipkin/zipkin-mysql
container_name: zipkin-playground-mysql
environment:
- MYSQL_HOST=mysql
zipkin:
image: openzipkin/zipkin
container_name: zipkin-playground-zipkin
environment:
- STORAGE_TYPE=mysql
- MYSQL_HOST=zipkin-playground-mysql
ports:
- 9411:9411
depends_on:
- storage
This gist contains the examples for the article "Tracing Express Services with zipkin-js".
- https://medium.com/trabe/tracing-express-services-with-zipkin-js-6e5c5680467e
- https://rcoedo.com/blog/2019/07/08/tracing-express-services-with-zipkin-js
// This lives in views/index.pug
html
head
title= "What time is it?"
body
h1= date
const express = require("express");
// Import axios and axios instrumentation
const axios = require("axios");
const zipkinInstrumentationAxios = require("zipkin-instrumentation-axios");
// Import zipkin stuff
const { Tracer, ExplicitContext, BatchRecorder, jsonEncoder } = require("zipkin");
const { HttpLogger } = require("zipkin-transport-http");
const zipkinMiddleware = require("zipkin-instrumentation-express").expressMiddleware;
const ZIPKIN_ENDPOINT = process.env.ZIPKIN_ENDPOINT || "http://localhost:9411";
const API_ENDPOINT = process.env.API_ENDPOINT || "http://localhost:3001";
// Get ourselves a zipkin tracer
const tracer = new Tracer({
ctxImpl: new ExplicitContext(),
recorder: new BatchRecorder({
logger: new HttpLogger({
endpoint: `${ZIPKIN_ENDPOINT}/api/v2/spans`,
jsonEncoder: jsonEncoder.JSON_V2,
}),
}),
localServiceName: "web-service",
});
const app = express();
const port = process.env.PORT || 3000;
// Add zipkin express middleware
app.use(zipkinMiddleware({ tracer }));
// Add axios instrumentation
const zipkinAxios = zipkinInstrumentationAxios(axios, { tracer, serviceName: "axios-client" });
// We use pug to render the template
app.set("view engine", "pug");
app.get("/", async (req, res, next) => {
try {
const result = await zipkinAxios.get(`${API_ENDPOINT}/time`);
res.render("index", { date: new Date(result.data.currentDate).toLocaleTimeString() });
} catch (error) {
next(error);
}
});
app.listen(port, () => console.log(`Web service listening on port ${port}`));
// Our web service controller will look like this
app.get("/", async (req, res, next) => {
try {
await tracer.local("awaiting 100ms delay", () => delay(100));
const result = await zipkinAxios.get(`${API_ENDPOINT}/time`);
res.render("index", { date: new Date(result.data.currentDate).toLocaleTimeString() });
} catch (error) {
next(error);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment