Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 62e1ac0

Browse files
authored
Add Express example (#577)
* Add Express example * fix review comments
1 parent e249c86 commit 62e1ac0

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

examples/express/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Overview
2+
3+
OpenCensus HTTP Instrumentation allows the user to automatically collect trace data and export them to the backend of choice (we are using Zipkin for this example), to give observability to distributed systems.
4+
5+
> NOTE: Please ensure that you start the OpenCensus tracer BEFORE initializing your express app, if you want to enable automatic tracing for built-in plugins (HTTP in this case). https://github.com/census-instrumentation/opencensus-node#plugins
6+
7+
## Installation
8+
9+
```sh
10+
$ # from this directory
11+
$ npm install
12+
```
13+
14+
Setup [Zipkin Tracing](https://opencensus.io/codelabs/zipkin/#0)
15+
16+
## Run the Application
17+
18+
- Run the server
19+
20+
```sh
21+
$ # from this directory
22+
$ npm start
23+
```
24+
25+
- Execute the request
26+
27+
Please visit http://localhost:3000/users to submit the request.
28+
29+
- Viewing traces
30+
31+
On navigating to the Zipkin UI at http://localhost:9411
32+
33+
![Screenshot](./zipkin.png)
34+
35+
## Useful links
36+
- For more information on OpenCensus, visit: <https://opencensus.io/>
37+
- To checkout the OpenCensus for Node.js, visit: <https://github.com/census-instrumentation/opencensus-node>
38+
- For more information on Zipkin, visit https://zipkin.io/
39+
40+
## LICENSE
41+
42+
Apache License 2.0

examples/express/index.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
/**
20+
* The trace instance needs to be initialized first, if you want to enable
21+
* automatic tracing for built-in plugins (HTTP in this case).
22+
* https://github.com/census-instrumentation/opencensus-node#plugins
23+
*/
24+
25+
const port = process.env.APP_PORT || 3000;
26+
const path = require('path');
27+
const cookieParser = require('cookie-parser');
28+
const tracing = require('@opencensus/nodejs');
29+
const propagation = require('@opencensus/propagation-b3');
30+
31+
// Creates Zipkin exporter
32+
const zipkin = require('@opencensus/exporter-zipkin');
33+
const exporter = new zipkin.ZipkinTraceExporter({
34+
url: 'http://localhost:9411/api/v2/spans',
35+
serviceName: 'opencensus-express'
36+
});
37+
38+
// NOTE: Please ensure that you start the tracer BEFORE initializing express app
39+
// Starts tracing and set sampling rate, exporter and propagation
40+
tracing.start({
41+
exporter,
42+
samplingRate: 1, // For demo purposes, always sample
43+
propagation: new propagation.B3Format(),
44+
logLevel: 1 // show errors, if any
45+
});
46+
47+
const express = require('express');
48+
const rp = require('request-promise');
49+
const app = express();
50+
app.use(express.json());
51+
app.use(express.urlencoded({ extended: false }));
52+
app.use(cookieParser());
53+
app.use(express.static(path.join(__dirname, 'public')));
54+
55+
app.get('/users', function (req, res) {
56+
console.log(`user headers: ${JSON.stringify(req.headers)}`);
57+
rp('http://localhost:3000/sample').then(data => {
58+
res.send(data);
59+
}, err => {
60+
console.error(`${err.message}`);
61+
});
62+
});
63+
64+
app.get('/sample', function (req, res) {
65+
console.log(`sample headers: ${JSON.stringify(req.headers)}`);
66+
rp('https://jsonplaceholder.typicode.com/todos/1').then(data => {
67+
res.send(data);
68+
});
69+
});
70+
71+
const http = require('http');
72+
app.set('port', port);
73+
const server = http.createServer(app);
74+
server.listen(port);

examples/express/package.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "express-example",
3+
"version": "0.0.1",
4+
"description": "Example of Express integration with OpenCensus",
5+
"repository": "census-instrumentation/opencensus-node",
6+
"keywords": [
7+
"opencensus",
8+
"http",
9+
"tracing",
10+
"stats",
11+
"metrics"
12+
],
13+
"author": "OpenCensus Authors",
14+
"license": "Apache-2.0",
15+
"engines": {
16+
"node": ">=8"
17+
},
18+
"scripts": {
19+
"start": "node index.js",
20+
"lint": "semistandard *.js",
21+
"fix": "semistandard --fix"
22+
},
23+
"dependencies": {
24+
"@opencensus/exporter-zipkin": "^0.0.14",
25+
"@opencensus/nodejs": "^0.0.14",
26+
"@opencensus/propagation-b3": "^0.0.14",
27+
"cookie-parser": "^1.4.4",
28+
"express": "^4.17.0",
29+
"http": "*",
30+
"method-override": "^3.0.0",
31+
"request": "^2.88.0",
32+
"request-promise": "^4.2.4"
33+
},
34+
"devDependencies": {
35+
"semistandard": "^13.0.1"
36+
}
37+
}

examples/express/zipkin.png

168 KB
Loading

0 commit comments

Comments
 (0)