diff options
author | Elizabeth Hunt <me@liz.coffee> | 2025-06-29 17:31:30 -0700 |
---|---|---|
committer | Elizabeth Hunt <me@liz.coffee> | 2025-06-29 17:31:30 -0700 |
commit | 58be1809c46cbe517a18d86d0af52179dcc5cbf6 (patch) | |
tree | 9ccc678b3fd48c1a52fe501600dd2c2051740a55 /u/trace/metric/metric.ts | |
parent | d4791f3d357634daf506fb8f91cc5332a794c421 (diff) | |
download | ci-58be1809c46cbe517a18d86d0af52179dcc5cbf6.tar.gz ci-58be1809c46cbe517a18d86d0af52179dcc5cbf6.zip |
Move to nodejs and also lots of significant refactoring that should've been broken up but idgaf
Diffstat (limited to 'u/trace/metric/metric.ts')
-rw-r--r-- | u/trace/metric/metric.ts | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/u/trace/metric/metric.ts b/u/trace/metric/metric.ts new file mode 100644 index 0000000..8ef339f --- /dev/null +++ b/u/trace/metric/metric.ts @@ -0,0 +1,54 @@ +import { EmittableMetric, IMetric, IMetricTag, IResultMetric, Unit } from './index.js'; + +class _Tagged { + protected constructor(public readonly _tag = IMetricTag) {} +} + +export class Metric extends _Tagged implements IMetric { + private static DELIM = '.'; + + protected constructor( + public readonly name: string, + public readonly parent: undefined | IMetric = undefined, + public readonly count = new EmittableMetric(Metric.join(name, 'count'), Unit.COUNT), + public readonly time = new EmittableMetric(Metric.join(name, 'time'), Unit.MILLISECONDS), + ) { + super(); + } + + public child(_name: string): Metric { + const childName = Metric.join(this.name, _name); + return new Metric(childName, this); + } + + public asResult() { + return ResultMetric.from(this); + } + + static join(...name: Array<string>) { + return name.join(Metric.DELIM); + } + + static fromName(name: string): Metric { + return new Metric(name); + } +} + +export class ResultMetric extends Metric implements IResultMetric { + protected constructor( + public readonly name: string, + public readonly parent: undefined | IMetric = undefined, + public readonly failure: IMetric, + public readonly success: IMetric, + public readonly warn: IMetric, + ) { + super(name, parent); + } + + static from(metric: Metric) { + const failure = metric.child('failure'); + const success = metric.child('success'); + const warn = metric.child('warn'); + return new ResultMetric(metric.name, metric.parent, failure, success, warn); + } +} |