All files / src/types/metrics Histogram.ts

100% Statements 15/15
80% Branches 8/10
100% Functions 3/3
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 4010x   10x   10x 50x 50x                   50x 50x       36x 36x   36x       42x 36x     6x 2x     4x      
import { Constants } from '../Constants';
import { Namespace } from '../Namespace';
import { Metric } from './Metric';
 
export class Histogram extends Metric {
  protected defaultBuckets: number[] = Constants.DefaultHistogramBuckets;
  private buckets: number[] = [];
 
  constructor(
    namespace: Namespace,
    name: string,
    description?: string,
    unit?: string,
    buckets: number[] = [],
    attributes: Record<string, string> = {},
  ) {
    super(namespace, name, description, unit, attributes);
    this.buckets = buckets;
  }
 
  record(value: number, buckets: number[] = [], attributes: Record<string, string> = {}): void {
    const resolvedBuckets = this.resolveBuckets(buckets);
    const combinedAttributes = this.combineAttributes(attributes);
 
    this.getNamespace().getMetricClient().recordTimer(this.getFullName(), value, resolvedBuckets, combinedAttributes);
  }
 
  protected resolveBuckets(buckets: number[]): number[] {
    if (buckets && buckets.length) {
      return buckets;
    }
 
    if (this.buckets && this.buckets.length) {
      return this.buckets;
    }
 
    return this.defaultBuckets;
  }
}