|
| 1 | +# OpenCensus Node.js Automatic Tracing |
| 2 | + |
| 3 | +In this example we'll build a simple http server that returns `Hello World`. We're also going to instrument it using OpenCensus, to be able to collect traces and send them to different services. |
| 4 | + |
| 5 | +## Installing OpenCensus |
| 6 | + |
| 7 | +Install OpenCensus with: |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install @opencensus/nodejs |
| 11 | +``` |
| 12 | + |
| 13 | +## Instrumenting the Application |
| 14 | + |
| 15 | +OpenCensus is able to automatically trace HTTP requests, therefore, you just need to require OpenCensus in your application with: |
| 16 | + |
| 17 | +```javascript |
| 18 | +var tracing = require('@opencensus/nodejs'); |
| 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); |
| 27 | +``` |
| 28 | + |
| 29 | +### Using Stackdriver Exporter |
| 30 | + |
| 31 | +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: |
| 32 | + |
| 33 | +```bash |
| 34 | +export GOOGLE_APPLICATION_CREDENTIALS=path/to/your/credential.json |
| 35 | +``` |
| 36 | + |
| 37 | +In your code, instanciate a Stackdriver Exporter and pass it to `tracing.start()`. |
| 38 | + |
| 39 | +```javascript |
| 40 | +var stackdriver = require('@opencensus/exporter-stackdriver'); |
| 41 | + |
| 42 | +// Add your project id to the Stackdriver options |
| 43 | +exporter = new stackdriver.StackdriverTraceExporter({projectId: "your-project-id"}); |
| 44 | + |
| 45 | +tracing.start({'exporter': exporter}); |
| 46 | +``` |
| 47 | + |
| 48 | +### Using Zipkin Exporter |
| 49 | + |
| 50 | +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: |
| 51 | + |
| 52 | +```bash |
| 53 | + wget -O zipkin.jar 'https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec' |
| 54 | + java -jar zipkin.jar |
| 55 | +``` |
| 56 | + |
| 57 | +In your code, instanciate a Zipkin Exporter and pass it to `tracing.start()`. |
| 58 | + |
| 59 | +```javascript |
| 60 | +var zipkin = require('@opencensus/exporter-zipkin'); |
| 61 | + |
| 62 | +// Add your zipkin url and service name to the Zipkin options |
| 63 | +var options = { |
| 64 | + url: 'your-zipkin-url', |
| 65 | + serviceName: 'your-service-name' |
| 66 | +} |
| 67 | +var exporter = new zipkin.ZipkinTraceExporter(options); |
| 68 | + |
| 69 | +tracing.start({'exporter': exporter}); |
| 70 | +``` |
| 71 | + |
| 72 | +## Running the Instrumented Application |
| 73 | + |
| 74 | +Run the application with: |
| 75 | + |
| 76 | +```bash |
| 77 | +node server.js |
| 78 | +``` |
| 79 | + |
| 80 | +Go to `http://localhost:8080` to make a request or use a REST Application to do so. |
| 81 | + |
| 82 | +Now, just go to the service used to send the traces and see the requests you just made. |
0 commit comments