Skip to content

Commit 998c874

Browse files
author
Simone Sanfratello
authored
fix: telemetry (#4)
* fix: telemetry * release: v0.2.2 * chore: comments
1 parent 32e4dba commit 998c874

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "e-ipfs-core-lib",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "E-IPFS core library",
55
"license": "(Apache-2.0 AND MIT)",
66
"homepage": "https://github.com/elastic-ipfs/core-lib",
@@ -14,7 +14,7 @@
1414
"node": "18.12"
1515
},
1616
"dependencies": {
17-
"@aws-sdk/util-dynamodb": "^3.200.0",
17+
"@aws-sdk/util-dynamodb": "^3.216.0",
1818
"bl": "^6.0.0",
1919
"hdr-histogram-js": "^3.0.0",
2020
"js-yaml": "^4.1.0",
@@ -32,7 +32,7 @@
3232
"dedent": "^0.7.0",
3333
"split2": "^4.1.0",
3434
"standard": "^17.0.0",
35-
"tap": "^16.3.0"
35+
"tap": "^16.3.2"
3636
},
3737
"type": "module",
3838
"exports": "./src/index.js",

src/telemetry.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
11

22
import { readFileSync } from 'fs'
33
import { load as ymlLoad } from 'js-yaml'
4-
import { build as buildHistogram } from 'hdr-histogram-js'
4+
import * as hdr from 'hdr-histogram-js'
55

66
const PERCENTILES = [0.001, 0.01, 0.1, 1, 2.5, 10, 25, 50, 75, 90, 97.5, 99, 99.9, 99.99, 99.999]
77

88
class Aggregator {
9-
constructor (category, description, metric) {
9+
constructor (category, description, metric, type) {
1010
this.tag = `${category}-${metric}`
1111
this.description = `${description} (${metric})`
1212
this.exportName = this.tag.replaceAll('-', '_')
1313

14-
if (category.match(/active|pending/)) {
15-
this.type = 'gauge'
16-
} else if (metric === 'durations') {
17-
this.type = 'histogram'
14+
// type is optional
15+
if (!type) {
16+
// set the type by the metric
17+
if (metric === 'durations') {
18+
this.type = 'histogram'
19+
} else {
20+
this.type = 'counter'
21+
this.exportName += '_total'
22+
}
1823
} else {
19-
this.type = 'counter'
20-
this.exportName += '_total'
24+
this.type = type
2125
}
2226

2327
this.sum = 0
24-
this.histogram = buildHistogram({
28+
hdr.initWebAssemblySync()
29+
this.histogram = hdr.build({
2530
lowestDiscernibleValue: 1,
2631
highestTrackableValue: 1e9,
27-
numberOfSignificantValueDigits: 5
32+
numberOfSignificantValueDigits: 5,
33+
useWebAssembly: true
2834
})
2935
}
3036

@@ -103,8 +109,8 @@ class Telemetry {
103109
this.metrics.clear()
104110
}
105111

106-
createMetric (category, description, metric) {
107-
const instance = new Aggregator(category, description, metric)
112+
createMetric (category, description, metric, type) {
113+
const instance = new Aggregator(category, description, metric, type)
108114

109115
this.metrics.set(instance.tag, instance)
110116
}

test/telemetry.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,34 +168,34 @@ t.test('Telemetry', async t => {
168168
const telemetry = new Telemetry({ configFile, logger })
169169
telemetry.clear()
170170
telemetry.createMetric('c1', 'COUNTER', 'durations')
171-
telemetry.createMetric('c2-active', 'GAUGE', 'count')
171+
telemetry.createMetric('c2', 'GAUGE', 'count', 'gauge')
172172
telemetry.createMetric('c3', 'HISTOGRAM', 'durations')
173173

174174
t.equal(telemetry.export(), dedent`
175-
# HELP c2_active_count GAUGE (count)
176-
# TYPE c2_active_count gauge
177-
c2_active_count 0 now`.trim()
175+
# HELP c2_count GAUGE (count)
176+
# TYPE c2_count gauge
177+
c2_count 0 now`.trim()
178178
)
179179
})
180180

181181
t.test('should get the metrics result with registered metrics and values', async t => {
182182
const telemetry = new Telemetry({ configFile, logger })
183183
telemetry.clear()
184184
telemetry.createMetric('c1', 'COUNTER', 'count')
185-
telemetry.createMetric('c2-active', 'GAUGE', 'count')
185+
telemetry.createMetric('c2', 'GAUGE', 'count', 'gauge')
186186
telemetry.createMetric('c3', 'HISTOGRAM', 'durations')
187187

188188
telemetry.increaseCount('c1', 1)
189-
telemetry.increaseCount('c2-active', 2)
189+
telemetry.increaseCount('c2', 2)
190190
telemetry.ensureMetric('c3', 'durations').record(3)
191191

192192
t.equal(telemetry.export(), dedent`
193193
# HELP c1_count_total COUNTER (count)
194194
# TYPE c1_count_total counter
195195
c1_count_total 1 now
196-
# HELP c2_active_count GAUGE (count)
197-
# TYPE c2_active_count gauge
198-
c2_active_count 2 now
196+
# HELP c2_count GAUGE (count)
197+
# TYPE c2_count gauge
198+
c2_count 2 now
199199
# HELP c3_durations HISTOGRAM (durations)
200200
# TYPE c3_durations histogram
201201
c3_durations_count 1 now

0 commit comments

Comments
 (0)