-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
202 lines (179 loc) · 5.16 KB
/
Copy pathindex.js
File metadata and controls
202 lines (179 loc) · 5.16 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const { isBare } = require('which-runtime')
if (isBare) require('bare-process/global')
const process = require('process')
const HyperswarmStats = require('hyperswarm-stats')
const HypercoreStats = require('hypercore-stats')
const HyperDhtStats = require('hyperdht-stats')
const ReadyResource = require('ready-resource')
const DhtPromClient = require('dht-prom-client')
const promClient = require('bare-prom-client')
class HyperInstrumentation extends ReadyResource {
constructor({
swarm,
corestore,
dht,
scraperPublicKey,
scraperSecret,
prometheusAlias,
prometheusServiceName,
moduleVersions = null,
version = null
}) {
super()
if (swarm && dht) throw new Error('Exactly 1 of dht or swarm should be specified')
if (swarm) dht = swarm.dht
if (!moduleVersions) {
moduleVersions = [
'udx-native',
'dht-rpc',
'hyperdht',
'hyperswarm',
'hypercore',
'corestore',
'hyperbee',
'autobase',
'hyperdb'
]
}
promClient.collectDefaultMetrics()
if (version) registerPackageVersion(version)
registerModuleVersions(moduleVersions)
registerProcessId()
maybeRegisterPm2Name()
this.swarmStats = null
this.dhtStats = null
if (swarm) {
this.swarmStats = new HyperswarmStats(swarm)
this.swarmStats.registerPrometheusMetrics(promClient)
} else {
this.dhtStats = new HyperDhtStats(dht)
this.dhtStats.registerPrometheusMetrics(promClient)
}
this.hypercoreStats = null
if (corestore) {
this.hypercoreStats = HypercoreStats.fromCorestore(corestore)
this.hypercoreStats.registerPrometheusMetrics(promClient)
// TODO: these corestore-level stats should be defined in hypecore-stats
registerExtraCorestoreStats(corestore)
}
this.dhtPromClient = new DhtPromClient(
dht,
promClient,
scraperPublicKey,
prometheusAlias,
scraperSecret,
prometheusServiceName
)
}
get promClient() {
return this.dhtPromClient.promClient
}
async _open() {
await this.dhtPromClient.ready()
}
async _close() {
await this.dhtPromClient.close()
}
registerLogger(logger = console) {
this.dhtPromClient.registerLogger(logger)
if (this.hypercoreStats) {
this.hypercoreStats.on('internal-error', (e) => {
console.warn(`Hypercore stats internal error: ${e.stack}`)
})
}
}
}
function registerPackageVersion(version) {
// Gauges expect a number, so we set the version as label instead
return new promClient.Gauge({
name: 'package_version',
help: 'Package version in config.json',
labelNames: ['version'],
collect() {
this.labels(version).set(1)
}
})
}
function registerModuleVersions(names) {
for (const name of names) {
const normName = name.replace('@', '').replaceAll('/', '_').replaceAll('-', '_')
try {
const v = require(`${name}/package.json`).version
new promClient.Gauge({
// eslint-disable-line no-new
name: `${normName}_version`,
help: `${name} version`,
labelNames: [`${normName}_version`],
collect() {
this.labels(v).set(1)
}
})
} catch {} // dependency not found or version can't be extracted
}
}
function registerProcessId() {
return new promClient.Gauge({
name: 'process_pid',
help: 'Process id on the operating system',
collect() {
this.set(process.pid)
}
})
}
function maybeRegisterPm2Name() {
if (!process.env.name) return
new promClient.Gauge({
name: 'pm2_name',
help: 'name of the pm2 process',
labelNames: ['pm2_name'],
collect() {
this.labels(process.env.name).set(1)
}
})
}
function registerExtraCorestoreStats(corestore) {
if (!corestore.storage.stats?.treeCache) return
new promClient.Gauge({
name: 'corestore_tree_cache_hits',
help: 'cache hits in the hypercore storage tree-node cache',
collect() {
this.set(corestore.storage.stats.treeCache.hits)
}
})
new promClient.Gauge({
name: 'corestore_tree_cache_misses',
help: 'cache misses in the hypercore storage tree-node cache',
collect() {
this.set(corestore.storage.stats.treeCache.misses)
}
})
new promClient.Gauge({
name: 'corestore_tree_cache_parallel',
help: 'parallel cache hits in the hypercore storage tree-node cache',
collect() {
this.set(corestore.storage.stats.treeCache.parallel)
}
})
new promClient.Gauge({
name: 'corestore_tree_cache_skips',
help: 'cache skips in the hypercore storage tree-node cache',
collect() {
this.set(corestore.storage.stats.treeCache.skips)
}
})
new promClient.Gauge({
name: 'corestore_tree_cache_max_size',
help: 'max size of the hypercore storage tree-node cache',
collect() {
this.set(corestore.storage.treeCache.maxSize)
}
})
new promClient.Gauge({
name: 'corestore_tree_cache_entries',
help: 'number of current entries in the the hypercore storage tree-node cache',
collect() {
this.set(corestore.storage.treeCache.size || 0) // only defined in xache 1.3.0 and higher
}
})
}
module.exports = HyperInstrumentation