@libp2p/prometheus-metrics 4.3.21 → 4.3.22

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 (49) hide show
  1. package/dist/src/counter-group.d.ts +2 -5
  2. package/dist/src/counter-group.d.ts.map +1 -1
  3. package/dist/src/counter-group.js +4 -13
  4. package/dist/src/counter-group.js.map +1 -1
  5. package/dist/src/counter.d.ts +2 -5
  6. package/dist/src/counter.d.ts.map +1 -1
  7. package/dist/src/counter.js +1 -10
  8. package/dist/src/counter.js.map +1 -1
  9. package/dist/src/histogram-group.d.ts +2 -5
  10. package/dist/src/histogram-group.d.ts.map +1 -1
  11. package/dist/src/histogram-group.js +4 -13
  12. package/dist/src/histogram-group.js.map +1 -1
  13. package/dist/src/histogram.d.ts +1 -3
  14. package/dist/src/histogram.d.ts.map +1 -1
  15. package/dist/src/histogram.js +1 -11
  16. package/dist/src/histogram.js.map +1 -1
  17. package/dist/src/index.d.ts.map +1 -1
  18. package/dist/src/index.js +30 -49
  19. package/dist/src/index.js.map +1 -1
  20. package/dist/src/metric-group.d.ts +2 -5
  21. package/dist/src/metric-group.d.ts.map +1 -1
  22. package/dist/src/metric-group.js +4 -13
  23. package/dist/src/metric-group.js.map +1 -1
  24. package/dist/src/metric.d.ts +1 -3
  25. package/dist/src/metric.d.ts.map +1 -1
  26. package/dist/src/metric.js +1 -10
  27. package/dist/src/metric.js.map +1 -1
  28. package/dist/src/summary-group.d.ts +2 -5
  29. package/dist/src/summary-group.d.ts.map +1 -1
  30. package/dist/src/summary-group.js +4 -13
  31. package/dist/src/summary-group.js.map +1 -1
  32. package/dist/src/summary.d.ts +1 -3
  33. package/dist/src/summary.d.ts.map +1 -1
  34. package/dist/src/summary.js +1 -11
  35. package/dist/src/summary.js.map +1 -1
  36. package/dist/src/utils.d.ts +0 -4
  37. package/dist/src/utils.d.ts.map +1 -1
  38. package/dist/src/utils.js.map +1 -1
  39. package/package.json +7 -7
  40. package/src/counter-group.ts +6 -18
  41. package/src/counter.ts +3 -16
  42. package/src/histogram-group.ts +6 -18
  43. package/src/histogram.ts +2 -14
  44. package/src/index.ts +33 -65
  45. package/src/metric-group.ts +6 -18
  46. package/src/metric.ts +2 -14
  47. package/src/summary-group.ts +6 -18
  48. package/src/summary.ts +2 -14
  49. package/src/utils.ts +0 -6
@@ -1,35 +1,27 @@
1
1
  import { Gauge } from 'prom-client'
2
2
  import { normalizeString } from './utils.js'
3
3
  import type { PrometheusCalculatedMetricOptions } from './index.js'
4
- import type { CalculatedMetric } from './utils.js'
5
- import type { CalculateMetric, MetricGroup, StopTimer } from '@libp2p/interface'
4
+ import type { MetricGroup, StopTimer } from '@libp2p/interface'
6
5
  import type { CollectFunction } from 'prom-client'
7
6
 
8
- export class PrometheusMetricGroup implements MetricGroup, CalculatedMetric<Record<string, number>> {
7
+ export class PrometheusMetricGroup implements MetricGroup {
9
8
  private readonly gauge: Gauge
10
9
  private readonly label: string
11
- private readonly calculators: Array<CalculateMetric<Record<string, number>>>
12
10
 
13
11
  constructor (name: string, opts: PrometheusCalculatedMetricOptions<Record<string, number>>) {
14
12
  name = normalizeString(name)
15
13
  const help = normalizeString(opts.help ?? name)
16
14
  const label = this.label = normalizeString(opts.label ?? name)
17
15
  let collect: CollectFunction<Gauge<any>> | undefined
18
- this.calculators = []
19
16
 
20
17
  // calculated metric
21
18
  if (opts?.calculate != null) {
22
- this.calculators.push(opts.calculate)
23
- const self = this
24
-
25
19
  collect = async function () {
26
- await Promise.all(self.calculators.map(async calculate => {
27
- const values = await calculate()
20
+ const values = await opts.calculate()
28
21
 
29
- Object.entries(values).forEach(([key, value]) => {
30
- this.set({ [label]: key }, value)
31
- })
32
- }))
22
+ Object.entries(values).forEach(([key, value]) => {
23
+ this.set({ [label]: key }, value)
24
+ })
33
25
  }
34
26
  }
35
27
 
@@ -42,10 +34,6 @@ export class PrometheusMetricGroup implements MetricGroup, CalculatedMetric<Reco
42
34
  })
43
35
  }
44
36
 
45
- addCalculator (calculator: CalculateMetric<Record<string, number>>): void {
46
- this.calculators.push(calculator)
47
- }
48
-
49
37
  update (values: Record<string, number>): void {
50
38
  Object.entries(values).forEach(([key, value]) => {
51
39
  this.gauge.set({ [this.label]: key }, value)
package/src/metric.ts CHANGED
@@ -1,30 +1,22 @@
1
1
  import { Gauge } from 'prom-client'
2
2
  import { normalizeString } from './utils.js'
3
3
  import type { PrometheusCalculatedMetricOptions } from './index.js'
4
- import type { Metric, StopTimer, CalculateMetric } from '@libp2p/interface'
4
+ import type { Metric, StopTimer } from '@libp2p/interface'
5
5
  import type { CollectFunction } from 'prom-client'
6
6
 
7
7
  export class PrometheusMetric implements Metric {
8
8
  private readonly gauge: Gauge
9
- private readonly calculators: CalculateMetric[]
10
9
 
11
10
  constructor (name: string, opts: PrometheusCalculatedMetricOptions) {
12
11
  name = normalizeString(name)
13
12
  const help = normalizeString(opts.help ?? name)
14
13
  const labels = opts.label != null ? [normalizeString(opts.label)] : []
15
14
  let collect: CollectFunction<Gauge<any>> | undefined
16
- this.calculators = []
17
15
 
18
16
  // calculated metric
19
17
  if (opts?.calculate != null) {
20
- this.calculators.push(opts.calculate)
21
- const self = this
22
-
23
18
  collect = async function () {
24
- const values = await Promise.all(self.calculators.map(async calculate => calculate()))
25
- const sum = values.reduce((acc, curr) => acc + curr, 0)
26
-
27
- this.set(sum)
19
+ this.set(await opts.calculate())
28
20
  }
29
21
  }
30
22
 
@@ -37,10 +29,6 @@ export class PrometheusMetric implements Metric {
37
29
  })
38
30
  }
39
31
 
40
- addCalculator (calculator: CalculateMetric): void {
41
- this.calculators.push(calculator)
42
- }
43
-
44
32
  update (value: number): void {
45
33
  this.gauge.set(value)
46
34
  }
@@ -1,35 +1,27 @@
1
1
  import { Summary as PromSummary } from 'prom-client'
2
2
  import { normalizeString } from './utils.js'
3
3
  import type { PrometheusCalculatedSummaryOptions } from './index.js'
4
- import type { CalculatedMetric } from './utils.js'
5
- import type { CalculateMetric, SummaryGroup, StopTimer } from '@libp2p/interface'
4
+ import type { SummaryGroup, StopTimer } from '@libp2p/interface'
6
5
  import type { CollectFunction } from 'prom-client'
7
6
 
8
- export class PrometheusSummaryGroup implements SummaryGroup, CalculatedMetric<Record<string, number>> {
7
+ export class PrometheusSummaryGroup implements SummaryGroup {
9
8
  private readonly summary: PromSummary
10
9
  private readonly label: string
11
- private readonly calculators: Array<CalculateMetric<Record<string, number>>>
12
10
 
13
11
  constructor (name: string, opts: PrometheusCalculatedSummaryOptions<Record<string, number>>) {
14
12
  name = normalizeString(name)
15
13
  const help = normalizeString(opts.help ?? name)
16
14
  const label = this.label = normalizeString(opts.label ?? name)
17
15
  let collect: CollectFunction<PromSummary<any>> | undefined
18
- this.calculators = []
19
16
 
20
17
  // calculated metric
21
18
  if (opts?.calculate != null) {
22
- this.calculators.push(opts.calculate)
23
- const self = this
24
-
25
19
  collect = async function () {
26
- await Promise.all(self.calculators.map(async calculate => {
27
- const values = await calculate()
20
+ const values = await opts.calculate()
28
21
 
29
- Object.entries(values).forEach(([key, value]) => {
30
- this.observe({ [label]: key }, value)
31
- })
32
- }))
22
+ Object.entries(values).forEach(([key, value]) => {
23
+ this.observe({ [label]: key }, value)
24
+ })
33
25
  }
34
26
  }
35
27
 
@@ -47,10 +39,6 @@ export class PrometheusSummaryGroup implements SummaryGroup, CalculatedMetric<Re
47
39
  })
48
40
  }
49
41
 
50
- addCalculator (calculator: CalculateMetric<Record<string, number>>): void {
51
- this.calculators.push(calculator)
52
- }
53
-
54
42
  observe (values: Record<string, number>): void {
55
43
  Object.entries(values).forEach(([key, value]) => {
56
44
  this.summary.observe({ [this.label]: key }, value)
package/src/summary.ts CHANGED
@@ -1,30 +1,22 @@
1
1
  import { Summary as PromSummary } from 'prom-client'
2
2
  import { normalizeString } from './utils.js'
3
3
  import type { PrometheusCalculatedSummaryOptions } from './index.js'
4
- import type { StopTimer, CalculateMetric, Summary } from '@libp2p/interface'
4
+ import type { StopTimer, Summary } from '@libp2p/interface'
5
5
  import type { CollectFunction } from 'prom-client'
6
6
 
7
7
  export class PrometheusSummary implements Summary {
8
8
  private readonly summary: PromSummary
9
- private readonly calculators: CalculateMetric[]
10
9
 
11
10
  constructor (name: string, opts: PrometheusCalculatedSummaryOptions) {
12
11
  name = normalizeString(name)
13
12
  const help = normalizeString(opts.help ?? name)
14
13
  const labels = opts.label != null ? [normalizeString(opts.label)] : []
15
14
  let collect: CollectFunction<PromSummary<any>> | undefined
16
- this.calculators = []
17
15
 
18
16
  // calculated metric
19
17
  if (opts?.calculate != null) {
20
- this.calculators.push(opts.calculate)
21
- const self = this
22
-
23
18
  collect = async function () {
24
- const values = await Promise.all(self.calculators.map(async calculate => calculate()))
25
- for (const value of values) {
26
- this.observe(value)
27
- }
19
+ this.observe(await opts.calculate())
28
20
  }
29
21
  }
30
22
 
@@ -42,10 +34,6 @@ export class PrometheusSummary implements Summary {
42
34
  })
43
35
  }
44
36
 
45
- addCalculator (calculator: CalculateMetric): void {
46
- this.calculators.push(calculator)
47
- }
48
-
49
37
  observe (value: number): void {
50
38
  this.summary.observe(value)
51
39
  }
package/src/utils.ts CHANGED
@@ -1,9 +1,3 @@
1
- import type { CalculateMetric } from '@libp2p/interface'
2
-
3
- export interface CalculatedMetric <T = number> {
4
- addCalculator(calculator: CalculateMetric<T>): void
5
- }
6
-
7
1
  export const ONE_SECOND = 1000
8
2
  export const ONE_MINUTE = 60 * ONE_SECOND
9
3