hyperswarm-stats 1.3.1 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +61 -48
  2. package/package.json +17 -5
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  const HyperDhtStats = require('hyperdht-stats')
2
2
 
3
3
  class HyperswarmStats {
4
- constructor (swarm) {
4
+ constructor(swarm) {
5
5
  this.swarm = swarm
6
6
  this.dhtStats = new HyperDhtStats(this.swarm.dht)
7
7
 
@@ -13,7 +13,7 @@ class HyperswarmStats {
13
13
  this._fastRecoveriesOfClosedSwarmStreams = 0
14
14
  this._rtoCountOfClosedSwarmStreams = 0
15
15
 
16
- const onconnection = conn => {
16
+ const onconnection = (conn) => {
17
17
  conn.on('close', () => {
18
18
  this._bytesTransmittedOverClosedSwarmStreams += conn.rawStream?.bytesTransmitted || 0
19
19
  this._packetsTransmittedOverClosedSwarmStreams += conn.rawStream?.packetsTransmitted || 0
@@ -29,15 +29,15 @@ class HyperswarmStats {
29
29
  swarm.on('connection', onconnection)
30
30
  }
31
31
 
32
- get connects () {
32
+ get connects() {
33
33
  return this.swarm.stats.connects
34
34
  }
35
35
 
36
- get updates () {
36
+ get updates() {
37
37
  return this.swarm.stats.updates
38
38
  }
39
39
 
40
- getAvgCongestionWindow () {
40
+ getAvgCongestionWindow() {
41
41
  let totalCongestionWindow = 0
42
42
  let count = 0
43
43
  for (const conn of this.swarm.connections) {
@@ -50,7 +50,7 @@ class HyperswarmStats {
50
50
  return totalCongestionWindow / count
51
51
  }
52
52
 
53
- getAvgMTU () {
53
+ getAvgMTU() {
54
54
  let totalMTU = 0
55
55
  let count = 0
56
56
  for (const conn of this.swarm.connections) {
@@ -63,7 +63,7 @@ class HyperswarmStats {
63
63
  return totalMTU / count
64
64
  }
65
65
 
66
- getRetransmitsAcrossAllStreams () {
66
+ getRetransmitsAcrossAllStreams() {
67
67
  let countFromCurrentConns = 0
68
68
  for (const conn of this.swarm.connections) {
69
69
  countFromCurrentConns += conn.rawStream?.retransmits || 0
@@ -72,7 +72,7 @@ class HyperswarmStats {
72
72
  return countFromCurrentConns + this._retransmitsOfClosedSwarmStreams
73
73
  }
74
74
 
75
- getFastRecoveriesAcrossAllStreams () {
75
+ getFastRecoveriesAcrossAllStreams() {
76
76
  let countFromCurrentConns = 0
77
77
  for (const conn of this.swarm.connections) {
78
78
  countFromCurrentConns += conn.rawStream?.fastRecoveries || 0
@@ -81,7 +81,7 @@ class HyperswarmStats {
81
81
  return countFromCurrentConns + this._fastRecoveriesOfClosedSwarmStreams
82
82
  }
83
83
 
84
- getRTOCountAcrossAllStreams () {
84
+ getRTOCountAcrossAllStreams() {
85
85
  let countFromCurrentConns = 0
86
86
  for (const conn of this.swarm.connections) {
87
87
  countFromCurrentConns += conn.rawStream?.rtoCount || 0
@@ -90,7 +90,7 @@ class HyperswarmStats {
90
90
  return countFromCurrentConns + this._rtoCountOfClosedSwarmStreams
91
91
  }
92
92
 
93
- getBytesTransmittedAcrossAllStreams () {
93
+ getBytesTransmittedAcrossAllStreams() {
94
94
  let bytesFromCurrentConns = 0
95
95
  for (const conn of this.swarm.connections) {
96
96
  bytesFromCurrentConns += conn.rawStream?.bytesTransmitted || 0
@@ -99,7 +99,7 @@ class HyperswarmStats {
99
99
  return bytesFromCurrentConns + this._bytesTransmittedOverClosedSwarmStreams
100
100
  }
101
101
 
102
- getBytesReceivedAcrossAllStreams () {
102
+ getBytesReceivedAcrossAllStreams() {
103
103
  let bytesFromCurrentConns = 0
104
104
  for (const conn of this.swarm.connections) {
105
105
  bytesFromCurrentConns += conn.rawStream?.bytesReceived || 0
@@ -108,7 +108,7 @@ class HyperswarmStats {
108
108
  return bytesFromCurrentConns + this._bytesReceivedOverClosedSwarmStreams
109
109
  }
110
110
 
111
- getPacketsTransmittedAcrossAllStreams () {
111
+ getPacketsTransmittedAcrossAllStreams() {
112
112
  let packetsFromCurrentConns = 0
113
113
  for (const conn of this.swarm.connections) {
114
114
  packetsFromCurrentConns += conn.rawStream?.packetsTransmitted || 0
@@ -117,7 +117,7 @@ class HyperswarmStats {
117
117
  return packetsFromCurrentConns + this._packetsTransmittedOverClosedSwarmStreams
118
118
  }
119
119
 
120
- getPacketsReceivedAcrossAllStreams () {
120
+ getPacketsReceivedAcrossAllStreams() {
121
121
  let packetsFromCurrentConns = 0
122
122
  for (const conn of this.swarm.connections) {
123
123
  packetsFromCurrentConns += conn.rawStream?.packetsReceived || 0
@@ -126,14 +126,19 @@ class HyperswarmStats {
126
126
  return packetsFromCurrentConns + this._packetsReceivedOverClosedSwarmStreams
127
127
  }
128
128
 
129
- get nrPeers () {
129
+ get nrPeers() {
130
130
  return this.swarm.peers.size
131
131
  }
132
132
 
133
- toJson () {
133
+ get nrConnections() {
134
+ return this.swarm.connections.size
135
+ }
136
+
137
+ toJson() {
134
138
  return {
135
139
  dht: this.dhtStats.toJson(),
136
140
  nrPeers: this.nrPeers,
141
+ nrConnections: this.nrConnections,
137
142
  connects: JSON.parse(JSON.stringify(this.connects)), // quick-and-dirty deep copy
138
143
  bytesTransmittedOverSwarmStreams: this.getBytesTransmittedAcrossAllStreams(),
139
144
  bytesReceivedOverSwarmStreams: this.getBytesReceivedAcrossAllStreams(),
@@ -147,9 +152,10 @@ class HyperswarmStats {
147
152
  }
148
153
  }
149
154
 
150
- toString () {
155
+ toString() {
151
156
  return `Hyperswarm Stats
152
157
  - hyperswarm_nr_peers: ${this.nrPeers}
158
+ - hyperswarm_nr_connections: ${this.nrConnections}
153
159
  - hyperswarm_client_connections_opened: ${this.connects.client.opened}
154
160
  - hyperswarm_client_connections_closed: ${this.connects.client.closed}
155
161
  - hyperswarm_client_connections_attempted: ${this.connects.client.attempted}
@@ -167,118 +173,125 @@ class HyperswarmStats {
167
173
  ${this.dhtStats.toString()}`
168
174
  }
169
175
 
170
- registerPrometheusMetrics (promClient) {
176
+ registerPrometheusMetrics(promClient) {
171
177
  this.dhtStats.registerPrometheusMetrics(promClient)
172
178
 
173
179
  const self = this
174
- new promClient.Gauge({ // eslint-disable-line no-new
180
+ new promClient.Gauge({
175
181
  name: 'hyperswarm_nr_peers',
176
- help: 'Number of peers this swarm is connected to',
177
- collect () {
182
+ help: 'Number of swarm peers',
183
+ collect() {
178
184
  this.set(self.nrPeers)
179
185
  }
180
186
  })
187
+ new promClient.Gauge({
188
+ name: 'hyperswarm_nr_connections',
189
+ help: 'Number of current connections of the swarm',
190
+ collect() {
191
+ this.set(self.nrConnections)
192
+ }
193
+ })
181
194
 
182
- new promClient.Gauge({ // eslint-disable-line no-new
195
+ new promClient.Gauge({
183
196
  name: 'hyperswarm_client_connections_opened',
184
197
  help: 'Total number of client connections opened by the swarm',
185
- collect () {
198
+ collect() {
186
199
  this.set(self.connects.client.opened)
187
200
  }
188
201
  })
189
- new promClient.Gauge({ // eslint-disable-line no-new
202
+ new promClient.Gauge({
190
203
  name: 'hyperswarm_client_connections_closed',
191
204
  help: 'Total number of client connections closed by the swarm',
192
- collect () {
205
+ collect() {
193
206
  this.set(self.connects.client.closed)
194
207
  }
195
208
  })
196
- new promClient.Gauge({ // eslint-disable-line no-new
209
+ new promClient.Gauge({
197
210
  name: 'hyperswarm_client_connections_attempted',
198
211
  help: 'Total number of client connections attempted by the swarm',
199
- collect () {
212
+ collect() {
200
213
  this.set(self.connects.client.attempted)
201
214
  }
202
215
  })
203
216
 
204
- new promClient.Gauge({ // eslint-disable-line no-new
217
+ new promClient.Gauge({
205
218
  name: 'hyperswarm_server_connections_opened',
206
219
  help: 'Total number of server connections opened by the swarm',
207
- collect () {
220
+ collect() {
208
221
  this.set(self.connects.server.opened)
209
222
  }
210
223
  })
211
- new promClient.Gauge({ // eslint-disable-line no-new
224
+ new promClient.Gauge({
212
225
  name: 'hyperswarm_server_connections_closed',
213
226
  help: 'Total number of server connections closed by the swarm',
214
- collect () {
227
+ collect() {
215
228
  this.set(self.connects.server.closed)
216
229
  }
217
230
  })
218
231
 
219
- new promClient.Gauge({ // eslint-disable-line no-new
232
+ new promClient.Gauge({
220
233
  name: 'hyperswarm_total_bytes_transmitted_over_swarm_streams',
221
234
  help: 'Total bytes transmitted over the streams exposed explicitly by hyperswarm connections',
222
- collect () {
235
+ collect() {
223
236
  this.set(self.getBytesTransmittedAcrossAllStreams())
224
237
  }
225
238
  })
226
- new promClient.Gauge({ // eslint-disable-line no-new
239
+ new promClient.Gauge({
227
240
  name: 'hyperswarm_total_bytes_received_over_swarm_streams',
228
241
  help: 'Total bytes received over the streams exposed explicitly by hyperswarm connections',
229
- collect () {
242
+ collect() {
230
243
  this.set(self.getBytesReceivedAcrossAllStreams())
231
244
  }
232
245
  })
233
- new promClient.Gauge({ // eslint-disable-line no-new
246
+ new promClient.Gauge({
234
247
  name: 'hyperswarm_total_packets_transmitted_over_swarm_streams',
235
248
  help: 'Total packets transmitted over the streams exposed explicitly by hyperswarm connections',
236
- collect () {
249
+ collect() {
237
250
  this.set(self.getPacketsTransmittedAcrossAllStreams())
238
251
  }
239
252
  })
240
- new promClient.Gauge({ // eslint-disable-line no-new
253
+ new promClient.Gauge({
241
254
  name: 'hyperswarm_total_packets_received_over_swarm_streams',
242
255
  help: 'Total packets received over the streams exposed explicitly by hyperswarm connections',
243
- collect () {
256
+ collect() {
244
257
  this.set(self.getPacketsReceivedAcrossAllStreams())
245
258
  }
246
259
  })
247
260
 
248
- new promClient.Gauge({ // eslint-disable-line no-new
261
+ new promClient.Gauge({
249
262
  name: 'hyperswarm_avg_congestion_window',
250
263
  help: 'Average size of the congestion window (over all hyperswarm connections)',
251
- collect () {
264
+ collect() {
252
265
  this.set(self.getAvgCongestionWindow())
253
266
  }
254
267
  })
255
268
 
256
- new promClient.Gauge({ // eslint-disable-line no-new
269
+ new promClient.Gauge({
257
270
  name: 'hyperswarm_avg_mtu',
258
271
  help: 'Average size of the Maximum Transmission Unit (over all hyperswarm connections)',
259
- collect () {
272
+ collect() {
260
273
  this.set(self.getAvgMTU())
261
274
  }
262
275
  })
263
276
 
264
- new promClient.Gauge({ // eslint-disable-line no-new
277
+ new promClient.Gauge({
265
278
  name: 'hyperswarm_total_retransmits_over_swarm_streams',
266
279
  help: 'Total UDX retransmits (after a lost packet), summed across the streams exposed explicitly by hyperswarm connections',
267
- collect () {
280
+ collect() {
268
281
  this.set(self.getRetransmitsAcrossAllStreams())
269
282
  }
270
283
  })
271
- new promClient.Gauge({ // eslint-disable-line no-new
284
+ new promClient.Gauge({
272
285
  name: 'hyperswarm_total_fast_recoveries_over_swarm_streams',
273
286
  help: 'Total UDX fast recoveries summed across the streams exposed explicitly by hyperswarm connections',
274
- collect () {
287
+ collect() {
275
288
  this.set(self.getFastRecoveriesAcrossAllStreams())
276
289
  }
277
290
  })
278
- new promClient.Gauge({ // eslint-disable-line no-new
291
+ new promClient.Gauge({
279
292
  name: 'hyperswarm_total_rto_count_over_swarm_streams',
280
293
  help: 'Total UDX retransmission time-outs, summed across the streams exposed explicitly by hyperswarm connections',
281
- collect () {
294
+ collect() {
282
295
  this.set(self.getRTOCountAcrossAllStreams())
283
296
  }
284
297
  })
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "hyperswarm-stats",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "description": "Hyperswarm stats with prometheus support",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "standard && brittle test.js"
7
+ "format": "prettier --write .",
8
+ "lint": "lunte && prettier --check .",
9
+ "test": "npm run lint && brittle-node test.js",
10
+ "test:bare": "brittle-bare test.js"
8
11
  },
9
12
  "repository": {
10
13
  "type": "git",
@@ -26,12 +29,21 @@
26
29
  ],
27
30
  "devDependencies": {
28
31
  "b4a": "^1.6.6",
29
- "brittle": "^3.6.1",
32
+ "bare-events": "^2.9.2",
33
+ "bare-prom-client": "^15.1.9",
34
+ "brittle": "^4.0.0",
30
35
  "hyperswarm": "^4.8.2",
31
- "prom-client": "^15.1.3",
32
- "standard": "^17.1.0"
36
+ "lunte": "^1.8.6",
37
+ "prettier": "^3.9.10",
38
+ "prettier-config-holepunch": "^2.0.0"
33
39
  },
34
40
  "dependencies": {
35
41
  "hyperdht-stats": "^1.7.0"
42
+ },
43
+ "imports": {
44
+ "events": {
45
+ "bare": "bare-events",
46
+ "default": "events"
47
+ }
36
48
  }
37
49
  }