Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return new Promise((resolve, reject) => {
const id = request.getId();
const name = request.getName();
const hops = request.getServiceHopsList();
// Adds reqId attribute
const span = tracing.tracer.currentRootSpan;
if (span) {
span.addAttribute('reqId', id);
}
// Creates a test response
const response = new interop.TestResponse();
response.setId(id);
if (hops.length === 0) {
resolve(setSuccessStatus(response));
} else {
// Extracts data from first service hop.
const firstHop = hops[0];
const host = firstHop.getService().getHost() || constants.DEFAULT_HOST;
const port = firstHop.getService().getPort();
const restHops = hops.slice(1);
const transport = firstHopreturn new Promise(async (res,rej)=>{
const fibSpan = tracing.tracer.startChildSpan('Fibonacci Calculation')
fibSpan.parentSpanId = span.id
const num = fibonacci(Math.floor(Math.random() * 30));
fibSpan.end()
// Random sleep! Because everyone needs their sleep!
const sleepSpan = tracing.tracer.startChildSpan('Sleep')
sleepSpan.parentSpanId = span.id
await sleep()
sleepSpan.end()
res({
isOdd: Boolean(num%2),
num
})
})
}app.get('/', async(req, res) => {
const begin = Date.now()
// Calculate a Fibbonacci Number and check if it is Odd or Even
const childSpan = tracing.tracer.startChildSpan('Fibonacci Odd Or Even')
const isOddOrEven = await oddOrEven(childSpan)
childSpan.end()
let up
try {
up = await request({url: upstream_uri})
} catch (error) {
up = error
}
const timeSpent = (Date.now() - begin) / 1000 + "secs (opencensus full)"
res.end(`${service_name} - ${timeSpent} - num: ${isOddOrEven.num} - isOdd: ${isOddOrEven.isOdd}\n${upstream_uri} -> ${up}`)
})public getTracer(): TracerBase {
return tracing.tracer;
}private static extractSamplingProbability(): number {
const samplingProbability = tracing.tracer.sampler.description;
if (samplingProbability === 'always') {
return 1;
} else if (samplingProbability === 'never') {
return 0;
}
const probability = samplingProbability.match(/\((.*)\)/);
return probability ? Number(probability[1]) : 1 / 10000;
}
}private saveChanges(query: Partial): void {
/** restore the config to default */
if (query.change === 'restore_default') {
tracing.tracer.sampler = SamplerBuilder.getSampler(
this.defaultConfig!.samplingRate
);
return;
}
/** change the sampling probability value */
if (query.samplingprobability) {
tracing.tracer.sampler = SamplerBuilder.getSampler(
query.samplingprobability
);
}
}