-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alejandro Frías Díaz
committed
Nov 21, 2023
1 parent
a5bd77e
commit 9282f99
Showing
16 changed files
with
462 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Labels, Metric, MetricValue } from './types'; | ||
import { findExistingMetric } from './utils'; | ||
|
||
export abstract class Collector<Value extends MetricValue> { | ||
private readonly data: Metric<Value>[]; | ||
|
||
constructor() { | ||
this.data = []; | ||
} | ||
|
||
get(labels?: Labels): Metric<Value> | undefined { | ||
return findExistingMetric<Value>(labels, this.data); | ||
} | ||
|
||
set(value: Value, labels?: Labels): this { | ||
const existing = findExistingMetric(labels, this.data); | ||
if (existing) { | ||
existing.value = value; | ||
} else { | ||
this.data.push({ | ||
labels, | ||
value, | ||
}); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
collect(labels?: Labels): Metric<Value>[] { | ||
if (!labels) { | ||
return this.data; | ||
} | ||
return this.data.filter((item) => { | ||
if (!item.labels) { | ||
return false; | ||
} | ||
const entries = Object.entries(labels); | ||
for (let i = 0; i < entries.length; i += 1) { | ||
const [label, value] = entries[i]; | ||
if (item.labels[label] !== value) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}); | ||
} | ||
|
||
resetAll(): this { | ||
for (let i = 0; i < this.data.length; i += 1) { | ||
this.reset(this.data[i].labels); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
abstract reset(labels?: Labels): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Collector } from './collector'; | ||
import { CounterValue, Labels } from './types'; | ||
|
||
export class Counter extends Collector<CounterValue> { | ||
inc(labels?: Labels): this { | ||
this.add(1, labels); | ||
return this; | ||
} | ||
|
||
add(amount: number, labels?: Labels): this { | ||
if (amount < 0) { | ||
throw new Error( | ||
`Expected increment amount to be greater than -1. Received: ${amount}`, | ||
); | ||
} | ||
const metric = this.get(labels); | ||
this.set(metric ? metric.value + amount : amount, labels); | ||
|
||
return this; | ||
} | ||
|
||
reset(labels?: Labels): void { | ||
this.set(0, labels); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Counter } from './counter'; | ||
import { Labels } from './types'; | ||
|
||
export class Gauge extends Counter { | ||
dec(labels?: Labels): this { | ||
const metric = this.get(labels); | ||
this.set(metric ? metric.value - 1 : 0, labels); | ||
return this; | ||
} | ||
|
||
sub(amount: number, labels?: Labels): this { | ||
const metric = this.get(labels); | ||
this.set(metric ? metric.value - amount : 0, labels); | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Collector } from './collector'; | ||
import { HistogramValue, HistogramValueEntries, Labels } from './types'; | ||
|
||
function findMinBucketIndex(ary: number[], num: number): number | undefined { | ||
if (num < ary[ary.length - 1]) { | ||
for (let i = 0; i < ary.length; i += 1) { | ||
if (num <= ary[i]) { | ||
return i; | ||
} | ||
} | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
function getInitialValue(buckets: number[]): HistogramValue { | ||
// Make the skeleton to which values will be saved. | ||
const entries = buckets.reduce( | ||
(result, b) => { | ||
// eslint-disable-next-line no-param-reassign | ||
result[b.toString()] = 0; | ||
return result; | ||
}, | ||
{ '+Inf': 0 } as HistogramValueEntries, | ||
); | ||
|
||
return { | ||
entries, | ||
sum: 0, | ||
count: 0, | ||
raw: [], | ||
}; | ||
} | ||
|
||
export class Histogram extends Collector<HistogramValue> { | ||
private readonly buckets: number[]; | ||
|
||
constructor(buckets: number[] = []) { | ||
super(); | ||
// Sort to get smallest -> largest in order. | ||
this.buckets = buckets.sort((a, b) => (a > b ? 1 : -1)); | ||
this.set(getInitialValue(this.buckets)); | ||
this.observe = this.observe.bind(this); | ||
} | ||
|
||
observe(value: number, labels?: Labels): this { | ||
let metric = this.get(labels); | ||
if (metric == null) { | ||
// Create a metric for the labels. | ||
metric = this.set(getInitialValue(this.buckets), labels).get(labels)!; | ||
} | ||
|
||
metric.value.raw.push(value); | ||
metric.value.entries['+Inf'] += 1; | ||
|
||
const minBucketIndex = findMinBucketIndex(this.buckets, value); | ||
|
||
if (minBucketIndex != null) { | ||
for (let i = minBucketIndex; i < this.buckets.length; i += 1) { | ||
const val = metric.value.entries[this.buckets[i].toString()]; | ||
metric.value.entries[this.buckets[i].toString()] = val + 1; | ||
} | ||
} | ||
|
||
metric.value.sum = metric.value.raw.reduce((sum, v) => sum + v, 0); | ||
metric.value.count += 1; | ||
|
||
return this; | ||
} | ||
|
||
reset(labels?: Labels): void { | ||
this.set(getInitialValue(this.buckets), labels); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Registry } from './registry'; | ||
|
||
export * from './types'; | ||
|
||
export const createRegistry = () => new Registry(); |
Oops, something went wrong.