|
| 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. |
0 commit comments