|
| 1 | +/** |
| 2 | + * Copyright 2019, OpenCensus Authors |
| 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 | + * https://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 | +'use strict'; |
| 18 | + |
| 19 | +/** |
| 20 | + * The trace instance needs to be initialized first, if you want to enable |
| 21 | + * automatic tracing for built-in plugins (HTTP in this case). |
| 22 | + * https://github.com/census-instrumentation/opencensus-node#plugins |
| 23 | + */ |
| 24 | + |
| 25 | +const port = process.env.APP_PORT || 3000; |
| 26 | +const path = require('path'); |
| 27 | +const cookieParser = require('cookie-parser'); |
| 28 | +const tracing = require('@opencensus/nodejs'); |
| 29 | +const propagation = require('@opencensus/propagation-b3'); |
| 30 | + |
| 31 | +// Creates Zipkin exporter |
| 32 | +const zipkin = require('@opencensus/exporter-zipkin'); |
| 33 | +const exporter = new zipkin.ZipkinTraceExporter({ |
| 34 | + url: 'http://localhost:9411/api/v2/spans', |
| 35 | + serviceName: 'opencensus-express' |
| 36 | +}); |
| 37 | + |
| 38 | +// NOTE: Please ensure that you start the tracer BEFORE initializing express app |
| 39 | +// Starts tracing and set sampling rate, exporter and propagation |
| 40 | +tracing.start({ |
| 41 | + exporter, |
| 42 | + samplingRate: 1, // For demo purposes, always sample |
| 43 | + propagation: new propagation.B3Format(), |
| 44 | + logLevel: 1 // show errors, if any |
| 45 | +}); |
| 46 | + |
| 47 | +const express = require('express'); |
| 48 | +const rp = require('request-promise'); |
| 49 | +const app = express(); |
| 50 | +app.use(express.json()); |
| 51 | +app.use(express.urlencoded({ extended: false })); |
| 52 | +app.use(cookieParser()); |
| 53 | +app.use(express.static(path.join(__dirname, 'public'))); |
| 54 | + |
| 55 | +app.get('/users', function (req, res) { |
| 56 | + console.log(`user headers: ${JSON.stringify(req.headers)}`); |
| 57 | + rp('http://localhost:3000/sample').then(data => { |
| 58 | + res.send(data); |
| 59 | + }, err => { |
| 60 | + console.error(`${err.message}`); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +app.get('/sample', function (req, res) { |
| 65 | + console.log(`sample headers: ${JSON.stringify(req.headers)}`); |
| 66 | + rp('https://jsonplaceholder.typicode.com/todos/1').then(data => { |
| 67 | + res.send(data); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +const http = require('http'); |
| 72 | +app.set('port', port); |
| 73 | +const server = http.createServer(app); |
| 74 | +server.listen(port); |
0 commit comments