|
| 1 | +# OpenCensus Node.js Example |
| 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 | +## Setup |
| 11 | + |
| 12 | +1. Clone the OpenCensus Node repository **TODO link to repository** |
| 13 | +<> |
| 14 | + |
| 15 | +2. Switch to branch `dev` with: |
| 16 | +```bash |
| 17 | +git checkout dev |
| 18 | +``` |
| 19 | + |
| 20 | +3. Navigate to the OpenCensus Node project folder and install the dependencies with: |
| 21 | +```bash |
| 22 | +npm install |
| 23 | +``` |
| 24 | + |
| 25 | +4. Compile the TypeScript code into JavaScript with: |
| 26 | +``` |
| 27 | +node_modules/.bin/tsc |
| 28 | +``` |
| 29 | + |
| 30 | +5. Clone the application we will instrument (EasyNotes Application) |
| 31 | +<https://github.com/callicoder/node-easy-notes-app> |
| 32 | + |
| 33 | +6. Navigate to the application folder and install the dependencies with: |
| 34 | +```bash |
| 35 | +npm install |
| 36 | +``` |
| 37 | + |
| 38 | +7. Navigate to the `node_modules` folder inside the EasyNotes application and create a link to OpenCensus Node project folder with: |
| 39 | +```bash |
| 40 | +ln -s <your path>/opencensus-node/build/src opencensus-nodejs |
| 41 | +``` |
| 42 | + |
| 43 | + |
| 44 | +## Instrument using Stackdriver |
| 45 | + |
| 46 | +1. Make sure you enabled Stackdriver Tracing on Google Cloud Platform. More info at <https://cloud.google.com/trace/docs/quickstart> |
| 47 | + |
| 48 | +2. Enable Application Default Credentials for authentication with: |
| 49 | +```bash |
| 50 | +export GOOGLE_APPLICATION_CREDENTIALS=path/to/your/credential.json |
| 51 | +``` |
| 52 | +More information at <https://cloud.google.com/docs/authentication/getting-started> |
| 53 | + |
| 54 | +3. Open the `server.js` file in the EasyNotes application and insert this code on top: |
| 55 | +```javascript |
| 56 | +var traceMng = require("opencensus-nodejs") |
| 57 | + .addStackdriver("your-project-id") |
| 58 | + .start(); |
| 59 | +``` |
| 60 | + |
| 61 | +## Instrument using Zipkin |
| 62 | + |
| 63 | +1. Download Zipkin choosing one 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: |
| 64 | +```bash |
| 65 | + wget -O zipkin.jar 'https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec' |
| 66 | + java -jar zipkin.jar |
| 67 | +``` |
| 68 | + |
| 69 | +2. Open the `server.js` file in the EasyNotes application and insert this code on top: |
| 70 | +```javascript |
| 71 | +var tracing = require("opencensus-nodejs") |
| 72 | + .addZipkin("http://localhost:9411/api/v2/spans", "easy-notes") |
| 73 | + .start() |
| 74 | +``` |
| 75 | + |
| 76 | +## Instrumenting with multiple Exporters |
| 77 | + |
| 78 | +It is possible to instrument with more than one code. To achieve this, simply add more than one Exporter in series. |
| 79 | + |
| 80 | +```javascript |
| 81 | +var tracing = require("opencensus-nodejs") |
| 82 | + .addZipkin(“http://localhost:9411/api/v2/spans“, "easy-notes") |
| 83 | + .addStackdriver("your-project") |
| 84 | + .start() |
| 85 | +``` |
| 86 | +
|
| 87 | +## Running the Instrumented Application |
| 88 | +
|
| 89 | +It is possible both to run with debugging information and without it. To run with debugging information use: |
| 90 | +```bash |
| 91 | +DEBUG=opencensus node server.js |
| 92 | +``` |
| 93 | +To run without debugging information, simply use: |
| 94 | +```bash |
| 95 | +node server.js |
| 96 | +``` |
0 commit comments