Last active
September 10, 2019 00:04
-
-
Save kjin/8958108430d88e5c9e2d89c4bc40a6c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const propagation = require('@opencensus/propagation-b3'); | |
const tracer = require('@google-cloud/trace-agent').start({ | |
rootSpanNameOverride: ((path) => `http://helloworld${path}`), | |
ignoreUrls: ['/metrics', '/api_version'], | |
samplingRate: 0.1, | |
propagation: new propagation.B3Format(), | |
disableUntracedModulesWarning: true | |
}); | |
const log = console.log; | |
const axios = require('axios'); | |
const bodyParser = require('body-parser'); | |
const express = require('express'); | |
const PORT = 8080; | |
const app = express(); | |
// // Purposely lose context | |
// let cbs = []; | |
// setInterval(() => { | |
// cbs.forEach(cb => cb()); | |
// cbs = []; | |
// }, 10); | |
// | |
// app.use((req, res, next) => { | |
// cbs.push(next); | |
// }); | |
app.use(bodyParser.json()); | |
app.get('/', async (req, res) => { | |
log('======'); | |
log(req.headers); | |
if (tracer.getCurrentRootSpan().getTraceContext()) { | |
log(tracer.getCurrentRootSpan().getTraceContext()); | |
} else { | |
log("No trace context"); | |
} | |
const prev = req.query.prev || ''; | |
const [ss, sns] = process.hrtime(); | |
const input = parseInt(req.query.n); | |
const output = input >= 2 ? (await Promise.all([ | |
axios.get(`http://localhost:${PORT}/?prev=${prev}%20${input}&n=${input - 1}`), | |
axios.get(`http://localhost:${PORT}/?prev=${prev}%20${input}&n=${input - 2}`) | |
])).reduce((a, b) => a + parseInt(b.data), 0) : input; | |
const [es, ens] = process.hrtime(); | |
if (!prev) { // An outermost request | |
const time = 1e9 * (es - ss) + ens - sns; | |
console.log(`Running Time: ${time}`); | |
} | |
res.send(`${output}`); | |
}); | |
app.get('/shutdown', () => { | |
process.exit(); | |
}); | |
app.listen(PORT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment