diff options
Diffstat (limited to 'u/trace/metrics.ts')
-rw-r--r-- | u/trace/metrics.ts | 36 |
1 files changed, 21 insertions, 15 deletions
diff --git a/u/trace/metrics.ts b/u/trace/metrics.ts index f57b725..69322b9 100644 --- a/u/trace/metrics.ts +++ b/u/trace/metrics.ts @@ -15,9 +15,9 @@ export enum Unit { export interface IMetric { readonly count: IEmittableMetric; readonly time: IEmittableMetric; - readonly failure: IMetric; - readonly success: IMetric; - readonly warn: IMetric; + readonly failure?: IMetric; + readonly success?: IMetric; + readonly warn?: IMetric; readonly children: Supplier<Array<IMetric>>; readonly _tag: "IMetric"; @@ -52,23 +52,25 @@ export class Metric implements IMetric { constructor( public readonly count: IEmittableMetric, public readonly time: IEmittableMetric, - public readonly failure: Metric, - public readonly success: Metric, - public readonly warn: Metric, + public readonly failure?: Metric, + public readonly success?: Metric, + public readonly warn?: Metric, public readonly _tag: "IMetric" = "IMetric", ) {} public children() { - return [this.failure, this.success, this.warn]; + return [this.failure, this.success, this.warn].filter( + (x) => x, + ) as IMetric[]; } - static fromName(name: string): Metric { + static fromName(name: string, addChildren = true): Metric { return new Metric( new EmittableMetric(`${name}.count`, Unit.COUNT), new EmittableMetric(`${name}.elapsed`, Unit.MILLISECONDS), - Metric.fromName(`${name}.failure`), - Metric.fromName(`${name}.success`), - Metric.fromName(`${name}.warn`), + addChildren ? Metric.fromName(`${name}.failure`, false) : undefined, + addChildren ? Metric.fromName(`${name}.success`, false) : undefined, + addChildren ? Metric.fromName(`${name}.warn`, false) : undefined, ); } } @@ -86,7 +88,9 @@ export const isMetricValue = (t: unknown): t is MetricValue => export const isMetricsTraceSupplier = (t: unknown): t is MetricsTraceSupplier => isMetricValue(t) || isIMetric(t); -export type MetricsTraceSupplier = ITraceWith<IMetric | MetricValue>; +export type MetricsTraceSupplier = ITraceWith< + IMetric | MetricValue | undefined +>; type MetricTracingTuple = [IMetric, Date]; export class MetricsTrace implements ITrace<MetricsTraceSupplier> { constructor( @@ -96,12 +100,13 @@ export class MetricsTrace implements ITrace<MetricsTraceSupplier> { ) {} public addTrace(trace: MetricsTraceSupplier) { - if (isMetricValue(trace) || typeof trace === "string") return this; + if (!isIMetric(trace)) return this; return new MetricsTrace(this.metricConsumer)._nowTracing(trace); } public trace(metric: MetricsTraceSupplier) { - if (typeof metric === "string") return this; + if (typeof metric === "undefined" || typeof metric === "string") + return this; if (isMetricValue(metric)) { this.metricConsumer([metric]); return this; @@ -138,7 +143,8 @@ export class MetricsTrace implements ITrace<MetricsTraceSupplier> { ]; } - private _nowTracing(metric: IMetric): MetricsTrace { + private _nowTracing(metric?: IMetric): MetricsTrace { + if (!metric) return this; this.tracing.push([metric, new Date()]); return this; } |