|
| 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 | +At this momment the automatic instrumetation is only for apps using http and mongo-db. |
| 9 | + |
| 10 | +___ |
| 11 | + |
| 12 | +## 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 | +cd opencensus-node |
| 18 | +``` |
| 19 | + |
| 20 | +2. Switch to branch `dev` with: |
| 21 | +```bash |
| 22 | +git checkout dev |
| 23 | +``` |
| 24 | + |
| 25 | +3. Install the dependencies with: |
| 26 | +```bash |
| 27 | +npm install |
| 28 | +``` |
| 29 | + |
| 30 | +4. Compile the TypeScript code into JavaScript with: |
| 31 | +``` |
| 32 | +node_modules/.bin/tsc |
| 33 | +``` |
| 34 | + |
| 35 | +5. In a different folder, clone the example application to be instrumented (EasyNotes Application) |
| 36 | +```bash |
| 37 | + git clone https://github.com/callicoder/node-easy-notes-app |
| 38 | +``` |
| 39 | + |
| 40 | +6. Navigate to the application folder and install the dependencies with: |
| 41 | +```bash |
| 42 | +cd node-easy-notes-app |
| 43 | +npm install |
| 44 | +``` |
| 45 | + |
| 46 | +7. Check if the app is running. PS.: a mongodb installation is required |
| 47 | +```bash |
| 48 | +$ node server.js |
| 49 | +Server is listening on port 3000 |
| 50 | +Successfully connected to the database |
| 51 | +``` |
| 52 | + |
| 53 | +## Add opencensus instrumentation |
| 54 | + |
| 55 | +To add opencensus instrumetation, follow the steps below: |
| 56 | + |
| 57 | +1. Navigate to the `node_modules` folder inside the EasyNotes application and create a link to OpenCensus Node project folder with: |
| 58 | +```bash |
| 59 | +cd node_modules |
| 60 | +ln -s <your path>/opencensus-node/build/src opencensus-nodejs |
| 61 | +cd .. |
| 62 | +``` |
| 63 | + |
| 64 | +2. Edit server.js and add the following line, as the first line of the file: |
| 65 | +```javascript |
| 66 | + var tracing = require("opencensus-nodejs").start() |
| 67 | + ... |
| 68 | + var express = require('express'); |
| 69 | +``` |
| 70 | + |
| 71 | +## Running the Instrumented Application |
| 72 | + |
| 73 | +Save the file server.js and run the app with debugging option. |
| 74 | + |
| 75 | +```bash |
| 76 | +$ DEBUG=opencensus node server.js |
| 77 | +opencensus useAsyncHooks = true +0ms |
| 78 | +opencensus patching http@9.8.0 module +75ms |
| 79 | +opencensus patching http.Server.prototype.emit function +7ms |
| 80 | +.... |
| 81 | +Server is listening on port 3000 |
| 82 | +Successfully connected to the database |
| 83 | +``` |
| 84 | +This options uses a default exporter to console. |
| 85 | + |
| 86 | +To test de api you can use the commands: |
| 87 | +```bash |
| 88 | +#To insert a note: |
| 89 | +curl -X POST http://localhost:3000/notes --data '{"title": "Note 1", "content": "this is the note content"}' -H "Content-Type: application/json" |
| 90 | + |
| 91 | +#To get notes: |
| 92 | +curl http://localhost:3000/notes |
| 93 | +``` |
| 94 | + |
| 95 | +## Exporting to Zipkins |
| 96 | + |
| 97 | +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: |
| 98 | +```bash |
| 99 | + wget -O zipkin.jar 'https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec' |
| 100 | + java -jar zipkin.jar |
| 101 | +``` |
| 102 | + |
| 103 | +2. Open the `server.js` file in the EasyNotes application and insert this code on top: |
| 104 | +```javascript |
| 105 | +var tracing = require("opencensus-nodejs") |
| 106 | + .addZipkin("http://localhost:9411/api/v2/spans", "easy-notes") |
| 107 | + .start() |
| 108 | +``` |
| 109 | + |
| 110 | + |
| 111 | +## Exporting to Stackdriver |
| 112 | + |
| 113 | +1. Make sure you enabled Stackdriver Tracing on Google Cloud Platform. More info at <https://cloud.google.com/trace/docs/quickstart> |
| 114 | + |
| 115 | +2. Enable Application Default Credentials for authentication with: |
| 116 | +```bash |
| 117 | +export GOOGLE_APPLICATION_CREDENTIALS=path/to/your/credential.json |
| 118 | +``` |
| 119 | +More information at <https://cloud.google.com/docs/authentication/getting-started> |
| 120 | + |
| 121 | +3. Open the `server.js` file in the EasyNotes application and insert this code on top: |
| 122 | +```javascript |
| 123 | +var traceMng = require("opencensus-nodejs") |
| 124 | + .addStackdriver("your-project-id") |
| 125 | + .start(); |
| 126 | +``` |
| 127 | + |
| 128 | + |
| 129 | +## Exporting to multiple Exporters |
| 130 | + |
| 131 | +It is possible to instrument with more than one code. To achieve this, simply add more than one Exporter in series. |
| 132 | + |
| 133 | +```javascript |
| 134 | +var tracing = require("opencensus-nodejs") |
| 135 | + .addZipkin(“http://localhost:9411/api/v2/spans“, "easy-notes") |
| 136 | + .addStackdriver("your-project") |
| 137 | + .start() |
| 138 | +``` |
0 commit comments