Last active
June 8, 2018 22:39
-
-
Save noorulhaq/bb0af9be1813c47a6c30f503fd39ecd1 to your computer and use it in GitHub Desktop.
Zipkin distributed tracing between express.js based aggregator and spring based backends
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const app = express() | |
const request = require('request-promise'); | |
const wrapRequest = require('zipkin-instrumentation-request'); | |
const { | |
Tracer, | |
BatchRecorder, | |
jsonEncoder: {JSON_V2} | |
} = require('zipkin'); | |
const zipkinExpressMiddleware = require('zipkin-instrumentation-express').expressMiddleware | |
const CLSContext = require('zipkin-context-cls'); | |
const {HttpLogger} = require('zipkin-transport-http'); | |
const ctxImpl = new CLSContext(); | |
const recorder = new BatchRecorder({ | |
logger: new HttpLogger({ | |
endpoint: 'http://localhost:9411/api/v2/spans', | |
jsonEncoder: JSON_V2 | |
}) | |
}); | |
const serviceName = 'gateway-app'; | |
const tracer = new Tracer({ctxImpl, recorder}); | |
const zipkinRequest = wrapRequest(request, { tracer: tracer }); | |
// Add the Zipkin middleware | |
app.use(zipkinExpressMiddleware({tracer,serviceName})) | |
app.get('/', async (req, res) => { | |
try { | |
const traceMe1 = await zipkinRequest('http://localhost:8080/traceMe1') | |
const traceMe2 = await zipkinRequest('http://localhost:8080/traceMe2') | |
res.send(traceMe1+ " " +traceMe2) | |
} catch (e) { | |
console.log(e) | |
res.sendStatus(500) | |
} | |
}); | |
app.listen(3000, () => console.log('Example app listening on port 3000!')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spring.zipkin.service.name=tracing-sample | |
spring.zipkin.sender.type=web | |
spring.zipkin.baseUrl=http://localhost:9411 | |
spring.sleuth.sampler.percentage=1.0 | |
spring.sleuth.sampler.probability=1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "gateway", | |
"version": "1.0.0", | |
"description": "", | |
"main": "aggregator.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "noor", | |
"license": "ISC", | |
"dependencies": { | |
"express": "^4.16.3", | |
"express-http-proxy": "^1.2.0", | |
"request": "^2.87.0", | |
"request-promise": "^4.2.2", | |
"zipkin": "^0.12.0", | |
"zipkin-context-cls": "^0.11.0", | |
"zipkin-instrumentation-express": "^0.12.0", | |
"zipkin-instrumentation-request": "^0.12.0", | |
"zipkin-transport-http": "^0.12.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.tracing.sample</groupId> | |
<artifactId>tracing-sample</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>tracing-sample</name> | |
<description>Demo project for Spring Boot</description> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>2.0.2.RELEASE</version> | |
<relativePath/> <!-- lookup parent from repository --> | |
</parent> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |
<java.version>1.8</java.version> | |
<spring-cloud.version>Finchley.RC2</spring-cloud.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-webflux</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.cloud</groupId> | |
<artifactId>spring-cloud-starter-zipkin</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.cloud</groupId> | |
<artifactId>spring-cloud-stream</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>io.projectreactor</groupId> | |
<artifactId>reactor-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.cloud</groupId> | |
<artifactId>spring-cloud-stream-test-support</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.cloud</groupId> | |
<artifactId>spring-cloud-dependencies</artifactId> | |
<version>${spring-cloud.version}</version> | |
<type>pom</type> | |
<scope>import</scope> | |
</dependency> | |
</dependencies> | |
</dependencyManagement> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
<repositories> | |
<repository> | |
<id>spring-milestones</id> | |
<name>Spring Milestones</name> | |
<url>https://repo.spring.io/milestone</url> | |
<snapshots> | |
<enabled>false</enabled> | |
</snapshots> | |
</repository> | |
</repositories> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tracing.sample.tracingsample; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
@SpringBootApplication | |
public class TracingSampleApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(TracingSampleApplication.class, args); | |
} | |
} | |
@RestController | |
class TracingController { | |
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(TracingController.class); | |
@GetMapping("/traceMe1") | |
public String traceMe1(){ | |
LOG.info("hello"); | |
return "hello"; | |
} | |
@GetMapping("/traceMe2") | |
public String traceMe2(){ | |
LOG.info("world"); | |
return "world"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment