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

Commit 135ec64

Browse files
committed
chord: merge commit
2 parents 25fa96f + a664b6e commit 135ec64

7 files changed

Lines changed: 339 additions & 0 deletions

File tree

examples/README.md renamed to examples/automatic_instrumentation/README.md

File renamed without changes.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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. Create a folder named `node_modules` and make a symlink inside of it, running the following command:
45+
```
46+
cd node_modules
47+
ln -s <opencensus-node-dir>/build/src opencensus-nodejs
48+
```
49+
50+
### Using Stackdriver Exporter
51+
52+
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:
53+
```bash
54+
export GOOGLE_APPLICATION_CREDENTIALS=path/to/your/credential.json
55+
```
56+
57+
Open the `stackdriver.js` file and, in the `.addStackdriver()` method, pass your Project ID as follows:
58+
```javascript
59+
var tracing = require('opencensus-nodejs').addStackdriver('your-project-id').start();
60+
```
61+
62+
### Using Zipkin Exporter
63+
64+
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:
65+
```bash
66+
wget -O zipkin.jar 'https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec'
67+
java -jar zipkin.jar
68+
```
69+
70+
Open the `zipkin.js` file and , in the `.addZipkin()` method, pass your *URL* and *service name* as follows:
71+
```javascript
72+
var tracing = require('opencensus-nodejs').addZipkin('http://localhost:9411/api/v2/spans', 'service_name');
73+
```
74+
75+
___
76+
77+
## Running the Instrumented Application
78+
79+
It is possible to run the application both with or without debugging information. To run with debugging information use:
80+
```bash
81+
DEBUG=opencensus node server.js
82+
```
83+
84+
To run without debugging information, simply use:
85+
```bash
86+
node server.js
87+
```
88+
89+
Go to `http://localhost:8080` to make a request or use a REST Application to do so.
90+
91+
Now, just go to the service used to send the traces and see the requests you just made.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2018 Google Inc. All Rights Reserved.
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+
* http://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+
var fs = require('fs');
18+
var tracing = require('opencensus-nodejs').addStackdriver('your-project-id').start();
19+
20+
var http = require('http');
21+
http.createServer(function (req, res) {
22+
res.writeHead(200, { 'Content-Type': 'text/html' });
23+
res.write('Hello World!');
24+
res.end();
25+
}).listen(8080);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright 2018 Google Inc. All Rights Reserved.
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+
* http://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+
var fs = require('fs');
18+
var tracing = require('opencensus-nodejs').addZipkin('http://localhost:9411/api/v2/spans', 'service_name');
19+
tracing.start();
20+
21+
var http = require('http');
22+
http.createServer(function (req, res) {
23+
res.writeHead(200, { 'Content-Type': 'text/html' });
24+
res.write('Hello World!');
25+
res.end();
26+
}).listen(8080);

examples/custom_tracing/README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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. Create a folder named `node_modules` and make a symlink inside of it, running the following command:
45+
```
46+
cd node_modules
47+
ln -s <opencensus-node-dir>/build/src opencensus-nodejs
48+
```
49+
50+
51+
### Using Stackdriver Exporter
52+
53+
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:
54+
```bash
55+
export GOOGLE_APPLICATION_CREDENTIALS=path/to/your/credential.json
56+
```
57+
58+
Open the `stackdriver.js` file and, in the `.addStackdriver()` method, pass your Project ID as follows:
59+
```javascript
60+
var tracing = require('opencensus-nodejs').addStackdriver('your-project-id').start();
61+
```
62+
63+
### Using Zipkin Exporter
64+
65+
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:
66+
```bash
67+
wget -O zipkin.jar 'https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec'
68+
java -jar zipkin.jar
69+
```
70+
71+
Open the `zipkin.js` file and , in the `.addZipkin()` method, pass your *URL* and *service name* as follows:
72+
```javascript
73+
var tracing = require('opencensus-nodejs').addZipkin('http://localhost:9411/api/v2/spans', 'service_name');
74+
```
75+
76+
___
77+
78+
## Running the Instrumented Application
79+
80+
It is possible to run the application both with or without debugging information. To run with debugging information use:
81+
```bash
82+
DEBUG=opencensus node server.js
83+
```
84+
85+
To run without debugging information, simply use:
86+
```bash
87+
node server.js
88+
```
89+
90+
Go to the service used to send the traces and see the spans for the operations you just made.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright 2018 Google Inc. All Rights Reserved.
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+
* http://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+
var opencensus = require('opencensus-nodejs');
18+
var http = require('http');
19+
var fs = require('fs');
20+
21+
// Register trace exporters to export the collected data.
22+
var tracing = require('opencensus-nodejs').addStackdriver('your-project-id').start();
23+
var tracer = tracing.Tracer;
24+
25+
var options = {
26+
name: 'fs.writeFileSync'
27+
};
28+
29+
// Create root span for GET request
30+
tracer.startRootSpan(options, (span) => {
31+
http.get('http://httpbin.org/image/jpeg', function (response) {
32+
33+
var data = [];
34+
35+
response.on('data', (chunk) => {
36+
data.push(chunk);
37+
});
38+
39+
response.on('end', () => {
40+
var filename = 'file';
41+
// Create a child span for file writing operation
42+
var childSpan = tracer.startSpan(filename);
43+
var buffer = Buffer.concat(data);
44+
fs.writeFileSync(filename + '.jpeg', buffer, 'utf-8');
45+
46+
// Finish both root span and child span, since the operations
47+
// has ended.
48+
childSpan.end();
49+
span.end();
50+
});
51+
console.log('No more data in response.');
52+
});
53+
});

examples/custom_tracing/zipkin.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright 2018 Google Inc. All Rights Reserved.
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+
* http://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+
var opencensus = require('opencensus-nodejs');
18+
var http = require('http');
19+
var fs = require('fs');
20+
21+
// Register trace exporters to export the collected data.
22+
var tracing = opencensus.addZipkin('http://localhost:9411/api/v2/spans', 'service_name').start();
23+
var tracer = tracing.Tracer;
24+
25+
var options = {
26+
name: 'fs.writeFileSync'
27+
};
28+
29+
// Create root span for GET request
30+
tracer.startRootSpan(options, (span) => {
31+
http.get('http://httpbin.org/image/jpeg', function (response) {
32+
33+
var data = [];
34+
35+
response.on('data', (chunk) => {
36+
data.push(chunk);
37+
});
38+
39+
response.on('end', () => {
40+
var filename = 'file';
41+
// Create a child span for file writing operation
42+
var childSpan = tracer.startSpan(filename);
43+
var buffer = Buffer.concat(data);
44+
fs.writeFileSync(filename + '.jpeg', buffer, 'utf-8');
45+
46+
// Finish both root span and child span, since the operations
47+
// has ended.
48+
childSpan.end();
49+
span.end();
50+
});
51+
console.log('No more data in response.');
52+
});
53+
});
54+

0 commit comments

Comments
 (0)