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 3 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
76 changes: 76 additions & 0 deletions examples/opentracing-shim/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Overview

OpenTelemetry Opentracing shim allows user to report to OpenTelemetry with existing OpenTracing instrumentation.
hieven marked this conversation as resolved.
Show resolved Hide resolved

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

## 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");
require("./tracer").init("http_client_service");

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);
process.exit(0);
});
}
)
.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://git@github.com/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"
}
}
55 changes: 55 additions & 0 deletions examples/opentracing-shim/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"use strict";

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

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();
}
26 changes: 26 additions & 0 deletions examples/opentracing-shim/tracer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use strict";

const { NodeTracer } = require("@opentelemetry/node");
hieven marked this conversation as resolved.
Show resolved Hide resolved
const { SimpleSpanProcessor } = require("@opentelemetry/tracing");
const { JaegerExporter } = require("@opentelemetry/exporter-jaeger");
const { ZipkinExporter } = require("@opentelemetry/exporter-zipkin");
const { TracerShim } = require("@opentelemetry/shim-opentracing");

const opentracing = require("opentracing");

function init(serviceName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that this file does OpenTracing and OpenTelemetry things together might be confusing, especially since the names are so similar. I would rather have one file that sets up open tracing the way you would normally set it up with open tracing, and another file called something like shim.js that sets up the shim. This way the example more clearly shows where the shim happens.

// shim.js

exports.shim = function shim(serviceName) {
  const tracer = new NodeTracer();
  tracer.addSpanProcessor(new SimpleSpanProcessor(getExporter()));

  return new TracerShim(tracer);
}

function getExporter() {
  const type = process.env.EXPORTER || "jaeger";
  if (type.match(/^z/)) {
    return new ZipkinExporter({ serviceName });
  }
  return new JaegerExporter({ serviceName, flushInterval: 100 });
}
// client.js
const shim = require("./shim").shim("http_client_service");

opentracing.initGlobalTracer(shim);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was initially imagining people usually have a tracer package which does initialization for open tracing and vendor specific instance (e.g. Zipkin or Jaeger). In this case, open telemetry is just another vendor so I made this decision to put all of them together.

I'm wondering if this makes sense to you and if adding some comments in the code can help or it will still be better to be separated as you suggest?

Many thanks for the suggestion! 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think i can go either way. I would still rather have the shim separate from the opentracing tracer so it is clear where the break is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, as your suggestion, I've created a shim file to make thing more clear. Thanks!

let exporter;
if (process.env.EXPORTER.toLowerCase().startsWith("z")) {
exporter = new ZipkinExporter({ serviceName });
} else {
exporter = new JaegerExporter({ serviceName, flushInterval: 100 });
}

const tracer = new NodeTracer();

tracer.addSpanProcessor(new SimpleSpanProcessor(exporter));

opentracing.initGlobalTracer(new TracerShim(tracer));
}

exports.init = init;
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;