Skip to content

Commit

Permalink
Merge branch 'master' into metrics-new
Browse files Browse the repository at this point in the history
  • Loading branch information
obecny committed Aug 31, 2020
2 parents 0844748 + c6d0251 commit f7887d6
Show file tree
Hide file tree
Showing 139 changed files with 6,685 additions and 238 deletions.
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ node_unit_tests: &node_unit_tests
- npm-cache-01-{{ .Environment.CIRCLE_JOB }}-{{ checksum "/tmp/checksums.txt" }}
- run:
name: Install Root Dependencies
command: npm install --ignore-scripts
command: npm install
- run:
name: Boostrap dependencies
command: npx lerna bootstrap --no-ci
command: npx lerna bootstrap
- save_cache:
<<: *cache_1
- run:
Expand Down Expand Up @@ -115,10 +115,10 @@ browsers_unit_tests: &browsers_unit_tests
- npm-cache-01-{{ .Environment.CIRCLE_JOB }}-{{ checksum "/tmp/checksums.txt" }}
- run:
name: Install Root Dependencies
command: npm install --ignore-scripts
command: npm install
- run:
name: Boostrap dependencies
command: npx lerna bootstrap --no-ci
command: npx lerna bootstrap
- save_cache:
<<: *cache_1
- run:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ OpenTelemetry can collect tracing data automatically using plugins. Vendors/User
- [@opentelemetry/plugin-document-load][otel-plugin-document-load]
- [@opentelemetry/plugin-xml-http-request][otel-plugin-xml-http-request]
- [@opentelemetry/plugin-user-interaction][otel-plugin-user-interaction]
- [@opentelemetry/plugin-react-load][otel-plugin-react-load]

## Contributing

Expand All @@ -92,6 +93,7 @@ Apache 2.0 - See [LICENSE][license-url] for more information.
[otel-plugin-https]: https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-plugin-https
[otel-plugin-dns]: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/node/opentelemetry-plugin-dns
[otel-plugin-document-load]: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/web/opentelemetry-plugin-document-load
[otel-plugin-react-load]: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/web/opentelemetry-plugin-react-load
[otel-plugin-ioredis]: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/node/opentelemetry-plugin-ioredis
[otel-plugin-mongodb]: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/node/opentelemetry-plugin-mongodb
[otel-plugin-mysql]: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/node/opentelemetry-plugin-mysql
Expand Down
76 changes: 76 additions & 0 deletions examples/hapi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Overview

OpenTelemetry Hapi Instrumentation allows the user to automatically collect trace data and export them to the backend of choice (we can use Zipkin or Jaeger for this example), to give observability to distributed systems.

This is a simple example that demonstrates tracing calls made in a Hapi application. The example shows key aspects of tracing such as
- Root Span (on Client)
- Child Span (on Client)
- Span Attributes
- Instrumentation for routes and request extension points
- Instrumentation of Hapi plugins

## Installation

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

Setup [Zipkin Tracing](https://zipkin.io/pages/quickstart.html)
or
Setup [Jaeger Tracing](https://www.jaegertracing.io/docs/latest/getting-started/#all-in-one)

## Run the Application

### Zipkin

- Run the server

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

- Run the client

```sh
# from this directory
npm run zipkin:client
```

#### Zipkin UI
`zipkin:server` script should output the `traceid` in the terminal (e.g `traceid: 4815c3d576d930189725f1f1d1bdfcc6`).
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.jpg?raw=true"/></p>

### Jaeger

- Run the server

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

- Run the client

```sh
# from this directory
npm run jaeger:client
```

#### Jaeger UI

`jaeger:server` script should output the `traceid` in the terminal (e.g `traceid: 4815c3d576d930189725f1f1d1bdfcc6`).
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.jpg?raw=true"/></p>

## Useful links
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
- For more information on OpenTelemetry for Node.js, visit: <https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-node>

## LICENSE

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

// eslint-disable-next-line import/order
const tracer = require('./tracer')('example-hapi-client');
const api = require('@opentelemetry/api');
const axios = require('axios').default;

function makeRequest() {
const span = tracer.startSpan('client.makeRequest()', {
parent: tracer.getCurrentSpan(),
kind: api.SpanKind.CLIENT,
});

tracer.withSpan(span, async () => {
try {
const res = await axios.get('http://localhost:8081/run_test');
span.setStatus({ code: api.CanonicalCode.OK });
console.log(res.statusText);
} catch (e) {
span.setStatus({ code: api.CanonicalCode.UNKNOWN, message: e.message });
}
span.end();
console.log('Sleeping 5 seconds before shutdown to ensure all records are flushed.');
setTimeout(() => { console.log('Completed.'); }, 5000);
});
}

makeRequest();
Binary file added examples/hapi/images/jaeger.jpg
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/hapi/images/zipkin.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions examples/hapi/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "hapi-example",
"private": true,
"version": "0.9.0",
"description": "Example of Hapi auto-instrumentation with OpenTelemetry",
"main": "index.js",
"scripts": {
"zipkin:server": "cross-env EXPORTER=zipkin node ./server.js",
"zipkin:client": "cross-env EXPORTER=zipkin node ./client.js",
"jaeger:server": "cross-env EXPORTER=jaeger node ./server.js",
"jaeger:client": "cross-env EXPORTER=jaeger node ./client.js",
"lint": "eslint . --ext .js",
"lint:fix": "eslint . --ext .js --fix"
},
"repository": {
"type": "git",
"url": "git+ssh://[email protected]/open-telemetry/opentelemetry-js-contrib.git"
},
"keywords": [
"opentelemetry",
"hapi",
"tracing",
"instrumentation"
],
"engines": {
"node": ">=8"
},
"author": "OpenTelemetry Authors",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/open-telemetry/opentelemetry-js-contrib/issues"
},
"dependencies": {
"@hapi/hapi": "^19.2.0",
"@opentelemetry/api": "^0.10.2",
"@opentelemetry/exporter-jaeger": "^0.10.2",
"@opentelemetry/exporter-zipkin": "^0.10.2",
"@opentelemetry/hapi-instrumentation": "^0.9.0",
"@opentelemetry/node": "^0.10.2",
"@opentelemetry/plugin-http": "^0.9.0",
"@opentelemetry/tracing": "^0.10.2",
"axios": "^0.19.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib#readme",
"devDependencies": {
"cross-env": "^6.0.0",
"eslint": "^7.4.0"
}
}
95 changes: 95 additions & 0 deletions examples/hapi/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
'use strict';

const tracer = require('./tracer')('example-hapi-server');
// eslint-disable-next-line
const Hapi = require('@hapi/hapi');

const PORT = 8081;
const server = Hapi.server({
port: PORT,
host: 'localhost',
});

const BlogPostPlugin = {
name: 'blog-post-plugin',
version: '1.0.0',
async register(serverClone) {
console.log('Registering basic hapi plugin');

serverClone.route([
{
method: 'GET',
path: '/post/new',
handler: addPost,
},
{
method: 'GET',
path: '/post/{id}',
handler: showNewPost,
}]);
},
};

async function setUp() {
await server.register(
{ plugin: BlogPostPlugin },
);

server.route(
{
method: 'GET',
path: '/run_test',
handler: runTest,
},
);

server.ext('onRequest', async (request, h) => {
console.log('No-op Hapi lifecycle extension method');
const syntheticDelay = 50;
await new Promise((r) => setTimeout(r, syntheticDelay));
return h.continue;
});

await server.start();
console.log('Server running on %s', server.info.uri);
console.log(`Listening on http://localhost:${PORT}`);
}

/**
* Blog Post functions: list, add, or show posts
*/
const posts = ['post 0', 'post 1', 'post 2'];

function addPost(_, h) {
posts.push(`post ${posts.length}`);
const currentSpan = tracer.getCurrentSpan();
currentSpan.addEvent('Added post');
currentSpan.setAttribute('Date', new Date());
console.log(`Added post: ${posts[posts.length - 1]}`);
return h.redirect('/post/3');
}

async function showNewPost(request) {
const { id } = request.params;
console.log(`showNewPost with id: ${id}`);
const post = posts[id];
if (!post) throw new Error('Invalid post id');
const syntheticDelay = 200;
await new Promise((r) => setTimeout(r, syntheticDelay));
return post;
}

function runTest(_, h) {
const currentSpan = tracer.getCurrentSpan();
const { traceId } = currentSpan.context();
console.log(`traceid: ${traceId}`);
console.log(`Jaeger URL: http://localhost:16686/trace/${traceId}`);
console.log(`Zipkin URL: http://localhost:9411/zipkin/traces/${traceId}`);
return h.redirect('/post/new');
}

setUp();
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
38 changes: 38 additions & 0 deletions examples/hapi/tracer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

const opentelemetry = require('@opentelemetry/api');
const { NodeTracerProvider } = require('@opentelemetry/node');
const { SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { JaegerExporter } = require('@opentelemetry/exporter-jaeger');
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin');

const EXPORTER = process.env.EXPORTER || '';

module.exports = (serviceName) => {
const provider = new NodeTracerProvider({
plugins: {
'@hapi/hapi': {
enabled: true,
path: '@opentelemetry/hapi-instrumentation',
enhancedDatabaseReporting: true,
},
http: {
enabled: true,
path: '@opentelemetry/plugin-http',
},
},
});

let exporter;
if (EXPORTER === 'jaeger') {
exporter = new JaegerExporter({ serviceName });
} else {
exporter = new ZipkinExporter({ serviceName });
}
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
provider.register();

return opentelemetry.trace.getTracer('hapi-example');
};
Loading

0 comments on commit f7887d6

Please sign in to comment.