Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add OpenTracing example #581

Merged
merged 7 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions examples/opentracing-shim/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Overview

OpenTracing shim allows existing OpenTracing instrumentation to report to OpenTelemetry.

This is a simple example that demonstrates how existing OpenTracing instrumentation can be integrated with OpenTelemetry.

The example shows key aspects of tracing such as

- Root Span (on client)
hieven marked this conversation as resolved.
Show resolved Hide resolved
- Child Span from a remote parent (on server)
- Span Tag
- Span Log
- Make a shim between OpenTracing and OpenTelemetry tracers

## Installation
```sh
# from this directory
$ npm install
```

## Run the Application

### Zipkin
- Setup [Zipkin Tracing UI](https://zipkin.io/pages/quickstart.html)

- Run the server
```sh
# from this directory
$ npm run zipkin:server
```

- Run the client
```sh
# from this directory
$ npm run zipkin:client
```

- Check trace

`zipkin:client` should output the `traceId` in the terminal.

Go to Zipkin with your browser [http://localhost:9411/zipkin/traces/(your-trace-id)]() (e.g http://localhost:9411/zipkin/traces/4815c3d576d930189725f1f1d1bdfcc6)


<p align="center"><img src="./images/zipkin-ui.png?raw=true"/></p>

### Jaeger
- Setup [Jaeger Tracing UI](https://www.jaegertracing.io/docs/latest/getting-started/#all-in-one)

- Run the server
```sh
# from this directory
$ npm run jaeger:server
```

- Run the client
```sh
# from this directory
$ npm run jaeger:client
```

- Check trace

`jaeger:client` should output the `traceId` in the terminal.

Go to Jaeger with your browser [http://localhost:16686/trace/(your-trace-id)]() (e.g http://localhost:16686/trace/4815c3d576d930189725f1f1d1bdfcc6)

<p align="center"><img src="images/jaeger-ui.png?raw=true"/></p>

## Useful links
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
hieven marked this conversation as resolved.
Show resolved Hide resolved
- For more information on OpenTelemetry for Node.js, visit: <https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-node>
- For more information on OpenTracing, visit: <https://opentracing.io/>

## LICENSE

Apache License 2.0
45 changes: 45 additions & 0 deletions examples/opentracing-shim/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"use strict";

const http = require("http");
const opentracing = require("opentracing");
const shim = require("./shim").shim("http_client_service");

opentracing.initGlobalTracer(shim);
const tracer = opentracing.globalTracer();

makeRequest();

async function makeRequest() {
const span = tracer.startSpan("make_request");

const headers = {};
tracer.inject(span, opentracing.FORMAT_HTTP_HEADERS, headers);

http
.get(
{
host: "localhost",
port: 3000,
path: "/",
headers
},
resp => {
let data = "";

resp.on("data", chunk => {
data += chunk;
});

resp.on("end", async () => {
console.log(JSON.parse(data));
span.finish();

console.log("Sleeping 5 seconds before shutdown to ensure all records are flushed.");
setTimeout(() => { console.log("Completed."); }, 5000);
});
}
)
.on("error", err => {
console.log("Error: " + err.message);
});
}
Binary file added examples/opentracing-shim/images/jaeger-ui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/opentracing-shim/images/zipkin-ui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions examples/opentracing-shim/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "opentracing-shim",
"private": true,
"version": "0.2.0",
"description": "Example of using @opentelemetry/shim-opentracing in Node.js",
"main": "index.js",
"scripts": {
"zipkin:client": "cross-env EXPORTER=zipkin node ./client.js",
"zipkin:server": "cross-env EXPORTER=zipkin node ./server.js",
"jaeger:client": "cross-env EXPORTER=jaeger node ./client.js",
"jaeger:server": "cross-env EXPORTER=jaeger node ./server.js"
},
"repository": {
"type": "git",
"url": "git+ssh://[email protected]/open-telemetry/opentelemetry-js.git"
},
"keywords": [
"opentelemetry",
"http",
"tracing",
"opentracing"
],
"engines": {
"node": ">=8"
},
"author": "OpenTelemetry Authors",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/open-telemetry/opentelemetry-js/issues"
},
"dependencies": {
"@opentelemetry/exporter-jaeger": "^0.2.0",
"@opentelemetry/exporter-zipkin": "^0.2.0",
"@opentelemetry/node": "^0.2.0",
"@opentelemetry/shim-opentracing": "^0.2.0",
"opentracing": "^0.14.4"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js#readme",
"devDependencies": {
"cross-env": "^6.0.0"
}
}
58 changes: 58 additions & 0 deletions examples/opentracing-shim/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"use strict";

const http = require("http");
const opentracing = require("opentracing");
const utils = require("./utils");
const shim = require("./shim").shim("http_server_service");

opentracing.initGlobalTracer(shim);
const tracer = opentracing.globalTracer();

startServer(3000);

function startServer(port) {
const server = http.createServer(handleRequest);

server.listen(port, err => {
if (err) throw err;

console.log(`Server is listening on ${port}`);
});
}

async function handleRequest(req, res) {
const parentSpan = tracer.extract(
opentracing.FORMAT_HTTP_HEADERS,
req.headers
);

const span = tracer.startSpan("handle_request", {
childOf: parentSpan
});

span.setTag("custom", "tag value");
span.setTag("alpha", "1000");

await doSomething(span);

res.writeHead(200, { "Content-Type": "application/json" });
res.write(
JSON.stringify({ status: "OK", traceId: span.context().toTraceId() })
);

res.end();
span.finish();
}

async function doSomething(parentSpan) {
const span = tracer.startSpan("do_something", { childOf: parentSpan });

span.setTag("alpha", "200");
span.setTag("beta", "50");
span.log({ state: "waiting" });

// deliberately sleeping to mock some action.
await utils.sleep(1000);

span.finish();
}
27 changes: 27 additions & 0 deletions examples/opentracing-shim/shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";

const { NodeTracer } = require("@opentelemetry/node");
const { SimpleSpanProcessor } = require("@opentelemetry/tracing");
const { JaegerExporter } = require("@opentelemetry/exporter-jaeger");
const { ZipkinExporter } = require("@opentelemetry/exporter-zipkin");
const { TracerShim } = require("@opentelemetry/shim-opentracing");

function shim(serviceName) {
const tracer = new NodeTracer();

tracer.addSpanProcessor(new SimpleSpanProcessor(getExporter(serviceName)));

return new TracerShim(tracer);
}

function getExporter(serviceName) {
const type = process.env.EXPORTER.toLowerCase() || "jaeger";

if (type.startsWith("z")) {
return new ZipkinExporter({ serviceName });
}

return new JaegerExporter({ serviceName, flushInterval: 100 });
}

exports.shim = shim;
7 changes: 7 additions & 0 deletions examples/opentracing-shim/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";

async function sleep(ms) {
hieven marked this conversation as resolved.
Show resolved Hide resolved
return new Promise(resolve => setTimeout(resolve, ms));
}

exports.sleep = sleep;