-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.js
More file actions
125 lines (105 loc) · 2.64 KB
/
example.js
File metadata and controls
125 lines (105 loc) · 2.64 KB
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
const { monitor, Span, Metric, PullMetric } = require('./')
const TransactionAggregator = require('./lib/aggregators/spans/transaction')
const SpanAggregator = require('./lib/aggregators/spans/span')
//
// Test watchers
//
const watch = monitor.watch({
type: 'span',
namespace: 'test',
data: { foo: 'bar' }
}, () => {})
const span = new Span('test', { foo: 'bar' })
const start = span.start()
console.log('is watching', start, monitor.watching(start))
watch.stop()
console.log('is watching', start, monitor.watching(start))
//
// Pull-based metric collectors
//
// const rss = new PullMetric('rss', null, 1000, () => {
// return process.memoryUsage().rss
// })
// const heapTotal = new PullMetric('heap-total', null, 1000, () => {
// return process.memoryUsage().heapTotal
// })
// const heapUsed = new PullMetric('heap-used', null, 1000, () => {
// return process.memoryUsage().heapUsed
// })
//
// Spans
//
// const transactions = new TransactionAggregator(transaction => {
// console.log('done', JSON.stringify(transaction, null, 2))
// })
// monitor.watch({ type: 'span' }, span => {
// transactions.send(span)
// })
const spans = new SpanAggregator(span => {
console.log('span', JSON.stringify(span, null, 2))
})
monitor.watch({ type: 'span' }, span => {
spans.send(span)
})
async function makeSpan() {
let span
if (monitor.watching({ type: 'span', namespace: 'some-span' })) {
span = new Span('some-span', {
data: 'here'
})
span.start()
}
await delay(randScale(1000))
if (monitor.watching({ type: 'span', namespace: 'another-span' })) {
let inner = new Span('another-span', {
foo: 'bar'
})
inner.start(span.$start)
inner.end()
}
if (span) {
for (let n = 0; n < 5; n++) {
span.log('message #%d', n)
await delay(randScale(100))
}
span.annotate({ foo: 'bar' })
}
await delay(randScale(1000))
if (span) span.end()
}
Promise.all(parallel(5, trackActive('active-requests', makeSpan))).then(
value => console.log({ value }),
error => console.error(error.stack)
)
//
// Some helpers
//
function randScale(scale) {
return Math.floor(Math.random() * scale)
}
function delay(ms) {
return new Promise(pass => setTimeout(pass, ms))
}
function* range(b, a = 0) {
while (a < b) yield a++
}
function trackActive(name, fn) {
const counter = new Metric(name)
return async function tracked() {
counter.incr()
const ret = await fn()
counter.decr()
return ret
}
}
function repeat(ms, fn) {
return async function repeated() {
while (true) {
await fn()
await delay(ms)
}
}
}
function parallel(n, fn) {
return Array.from(range(n)).map(fn)
}