-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for tracing to the api-server service.
- Added a tracing backend service powered by Jaeger. - Instrumented api-server service using the jaeger-client Opentracing library.
- Loading branch information
Uzziah Eyee
committed
May 4, 2019
1 parent
fc7f7a7
commit cb6a189
Showing
5 changed files
with
224 additions
and
14 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,60 @@ | ||
const app = require('express')() | ||
const uuid = require('uuid/v1') | ||
const axios = require('axios') | ||
|
||
// For portability, we initialize tracer from envars instead of local options. | ||
// See: https://www.npmjs.com/package/jaeger-client#environment-variables | ||
var initTracer = require('jaeger-client').initTracerFromEnv; | ||
var tracer = initTracer(); | ||
|
||
app.use('/api/v1/tokens', (req, res) => { | ||
const span = tracer.startSpan('token-request') | ||
|
||
console.log(`Handling request for token`) | ||
res.json({token: uuid()}) | ||
|
||
span.finish() | ||
}) | ||
app.use('/api/v1/whereami', (req, res) => { | ||
// TODO: get geolocation info from IP address | ||
// TODO: get weather info from geolocation | ||
// TODO: return everything | ||
let data = {'info': null} | ||
res.json(data) | ||
|
||
app.use('/api/v1/whereami', async (req, res, next) => { | ||
const parentSpan = tracer.startSpan('whereami-request') | ||
|
||
try { | ||
// get location of IP Address | ||
const IP = '23.16.76.104' | ||
const locSpan = tracer.startSpan('get-location', {childOf: parentSpan}) | ||
const location = await axios.get(`http://ip-api.com/json/${IP}`) | ||
locSpan.finish() | ||
const {lat, lon, city, country} = location.data | ||
|
||
// do some other async task | ||
const fakeSpan = tracer.startSpan('fake-fetch', {childOf: parentSpan}) | ||
const _ = await fakeFetch(100, 0.7) | ||
fakeSpan.finish() | ||
|
||
// return results | ||
const data = {lat: lat, lon: lon, city: city, country: country} | ||
res.json(data) | ||
} catch(err) { | ||
parentSpan.setTag('ERROR', err) | ||
next(err) | ||
} | ||
|
||
parentSpan.finish() | ||
}) | ||
|
||
const server = app.listen(process.env.PORT || 3000, () => { | ||
console.log(`Listening on ${ server.address().address }:${ server.address().port }`) | ||
}) | ||
}) | ||
|
||
function fakeFetch(msDelay, successRate) { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
if (Math.random() <= successRate) { | ||
resolve() | ||
} else { | ||
reject('Fake fetch failed randomly.') | ||
} | ||
}, msDelay) | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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