forked from thesandlord/Istio101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
127 lines (95 loc) · 3.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Copyright 2018 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const port = process.env.PORT || 3000
const upstream_uri = process.env.UPSTREAM_URI || 'http://worldclockapi.com/api/json/utc/now'
const service_name = process.env.SERVICE_NAME || 'opencensus-test-1-v1'
const express = require('express')
const app = express()
const request = require('request-promise-native')
// Start of OpenCensus setup ----------------------------------------------------------------------------
const opencensus = require('@opencensus/core')
const tracing = require('@opencensus/nodejs')
const propagation = require('@opencensus/propagation-b3')
const b3 = new propagation.B3Format()
// Set up jaeger
const jaeger = require('@opencensus/exporter-jaeger')
const jaeger_host = process.env.JAEGER_HOST || 'localhost'
const jaeger_port = process.env.JAEGER_PORT || '6832'
const exporter = new jaeger.JaegerTraceExporter({
host: jaeger_host,
port: jaeger_port,
serviceName: service_name,
});
tracing.start({
propagation: b3,
samplingRate: 1.0,
exporter: exporter
});
// Set up Prometheus
const prometheus = require('@opencensus/exporter-prometheus');
const prometheusExporter = new prometheus.PrometheusStatsExporter({
startServer: true
})
// Set up custom stats
const stats = new opencensus.Stats()
const tags = {ServiceName: service_name};
const tagKeys = Object.keys(tags);
const fibCount = stats.createMeasureInt64('fib function invocation', '1')
stats.createView('fib_count', fibCount, 0, tagKeys,'number of fib functions calls over time', null)
stats.registerExporter(prometheusExporter)
// End of OpenCensus setup ----------------------------------------------------------------------------
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}`)
})
app.listen(port, () => {
console.log(`${service_name} listening on port ${port}!`)
})
function oddOrEven(span) {
return 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
})
})
}
function fibonacci(num) {
stats.record({measure: fibCount, tags, value: 1})
if (num <= 1) return 1;
return fibonacci(num - 1) + fibonacci(num - 2);
}
function sleep() {
return new Promise((res, rej) => {
setTimeout(res,Math.random*2000)
})
}