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

Commit c473424

Browse files
WIP: Refactor examples for both exporters
1 parent 667d31a commit c473424

10 files changed

Lines changed: 180 additions & 415 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# OpenCensus Node.js Automatic Tracing
2+
3+
4+
Note: This code was tested on the following Node versions:
5+
- v6.10.0 (for console exporter only)
6+
- v9.8.0 (for Stackdriver and Zipkin exporters)
7+
8+
___
9+
10+
In this example we'll build a simple http server that can return `Hello World`. We're also going to instrument it using OpenCensus, to be able to collect traces and send them to different services.
11+
12+
## OpenCensus Setup
13+
14+
1. Clone the OpenCensus Node repository < https://github.com/census-instrumentation/opencensus-node.git>
15+
```bash
16+
git clone https://github.com/census-instrumentation/opencensus-node.git
17+
```
18+
19+
2. Switch to branch `dev` with:
20+
```bash
21+
git checkout dev
22+
```
23+
24+
3. Navigate to the OpenCensus Node project folder and install the dependencies with:
25+
```bash
26+
cd opencensus-node
27+
npm install
28+
```
29+
30+
4. Compile the TypeScript code into JavaScript with:
31+
```
32+
node_modules/.bin/tsc
33+
```
34+
35+
___
36+
37+
## Instrumented Application Setup
38+
39+
1. Navigate to the `automatic_tracing` folder with:
40+
```
41+
cd examples/automatic_tracing
42+
```
43+
44+
2. **TODO Create a node_modules folder or npm install <folder> command?**
45+
46+
47+
### Using Stackdriver Exporter
48+
49+
To use Stackdriver as your exporter, make sure you have enabled [Stackdriver Tracing](https://cloud.google.com/trace/docs/quickstart) on Google Cloud Platform. Enable your [Application Default Credentials](https://cloud.google.com/docs/authentication/getting-started) for authentication with:
50+
```bash
51+
export GOOGLE_APPLICATION_CREDENTIALS=path/to/your/credential.json
52+
```
53+
54+
Open the `stackdriver.js` file and, in the `.addStackdriver()` method, pass your Project ID as follows:
55+
```javascript
56+
var tracing = require('opencensus-nodejs').addStackdriver('your-project-id').start();
57+
```
58+
59+
### Using Zipkin Exporter
60+
61+
To use Zipkin as your exporter, first, download from any of the three available options on [Quickstart](https://zipkin.io/pages/quickstart.html): through Docker, on Java or manually compiling the source code. Tests were executed running Zipkin with Java, through the following commands on terminal:
62+
```bash
63+
wget -O zipkin.jar 'https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec'
64+
java -jar zipkin.jar
65+
```
66+
67+
Open the `zipkin.js` file and , in the `.addZipkin()` method, pass your *URL* and *service name* as follows:
68+
```javascript
69+
var tracing = require('opencensus-nodejs').addZipkin('http://localhost:9411/api/v2/spans', 'service_name');
70+
```
71+
72+
___
73+
74+
## Running the Instrumented Application
75+
76+
It is possible to run the application both with or without debugging information. To run with debugging information use:
77+
```bash
78+
DEBUG=opencensus node server.js
79+
```
80+
81+
To run without debugging information, simply use:
82+
```bash
83+
node server.js
84+
```
85+
86+
Go to `http://localhost:8080` to make a request or use a REST Application to do so.
87+
88+
Now, just go to the service used to send the traces and see the requests you just made.
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
2-
"name": "stackdriver",
2+
"name": "automatic_tracing",
33
"version": "1.0.0",
44
"description": "",
5-
"main": "index.js",
5+
"main": "stackdriver.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"
88
},
9-
"author": "Google Inc.",
10-
"license": "Apache-2.0"
9+
"author": "",
10+
"license": "ISC"
1111
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
var fs = require('fs');
18-
var tracing = require('opencensus-nodejs').addStackdriver('project-id').start();
18+
var tracing = require('opencensus-nodejs').addStackdriver('your-project-id').start();
1919

2020
var http = require('http');
2121
http.createServer(function (req, res) {

examples/custom_tracing/README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# OpenCensus Node.js Custom Tracing
2+
3+
4+
Note: This code was tested on the following Node versions:
5+
- v6.10.0 (for console exporter only)
6+
- v9.8.0 (for Stackdriver and Zipkin exporters)
7+
8+
___
9+
10+
In these example we're going to build custom spans for `GET` requests and file writing operations and send them to different services.
11+
12+
## OpenCensus Setup
13+
14+
1. Clone the OpenCensus Node repository < https://github.com/census-instrumentation/opencensus-node.git>
15+
```bash
16+
git clone https://github.com/census-instrumentation/opencensus-node.git
17+
```
18+
19+
2. Switch to branch `dev` with:
20+
```bash
21+
git checkout dev
22+
```
23+
24+
3. Navigate to the OpenCensus Node project folder and install the dependencies with:
25+
```bash
26+
cd opencensus-node
27+
npm install
28+
```
29+
30+
4. Compile the TypeScript code into JavaScript with:
31+
```
32+
node_modules/.bin/tsc
33+
```
34+
35+
___
36+
37+
## Instrumented Application Setup
38+
39+
1. Navigate to the `automatic_tracing` folder with:
40+
```
41+
cd examples/automatic_tracing
42+
```
43+
44+
2. **TODO Create a node_modules folder or npm install <folder> command?**
45+
46+
47+
### Using Stackdriver Exporter
48+
49+
To use Stackdriver as your exporter, make sure you have enabled [Stackdriver Tracing](https://cloud.google.com/trace/docs/quickstart) on Google Cloud Platform. Enable your [Application Default Credentials](https://cloud.google.com/docs/authentication/getting-started) for authentication with:
50+
```bash
51+
export GOOGLE_APPLICATION_CREDENTIALS=path/to/your/credential.json
52+
```
53+
54+
Open the `stackdriver.js` file and, in the `.addStackdriver()` method, pass your Project ID as follows:
55+
```javascript
56+
var tracing = require('opencensus-nodejs').addStackdriver('your-project-id').start();
57+
```
58+
59+
### Using Zipkin Exporter
60+
61+
To use Zipkin as your exporter, first, download from any of the three available options on [Quickstart](https://zipkin.io/pages/quickstart.html): through Docker, on Java or manually compiling the source code. Tests were executed running Zipkin with Java, through the following commands on terminal:
62+
```bash
63+
wget -O zipkin.jar 'https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec'
64+
java -jar zipkin.jar
65+
```
66+
67+
Open the `zipkin.js` file and , in the `.addZipkin()` method, pass your *URL* and *service name* as follows:
68+
```javascript
69+
var tracing = require('opencensus-nodejs').addZipkin('http://localhost:9411/api/v2/spans', 'service_name');
70+
```
71+
72+
___
73+
74+
## Running the Instrumented Application
75+
76+
It is possible to run the application both with or without debugging information. To run with debugging information use:
77+
```bash
78+
DEBUG=opencensus node server.js
79+
```
80+
81+
To run without debugging information, simply use:
82+
```bash
83+
node server.js
84+
```
85+
86+
Go to the service used to send the traces and see the spans for the operations you just made.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var http = require('http');
1919
var fs = require('fs');
2020

2121
// Register trace exporters to export the collected data.
22-
var tracing = require('opencensus-nodejs').addStackdriver('project-id').start();
22+
var tracing = require('opencensus-nodejs').addStackdriver('your-project-id').start();
2323
var tracer = tracing.Tracer;
2424

2525
var options = {

examples/stackdriver/package-lock.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)