All files / types/client MetricClient.ts

58.53% Statements 24/41
57.14% Branches 4/7
50% Functions 7/14
58.53% Lines 24/41

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 1231x   1x 1x 1x   1x             13x 13x 13x 13x   13x                                                                           13x               2x 1x     1x       4x 1x     3x       1x 1x             3x 1x     2x                   13x 13x                                      
import * as os from 'os';
import { IMetricClient } from './IMetricClient';
import { Namespace } from '../Namespace';
import { Warnings } from '../Warnings';
import { Logger } from '../logging/Logger';
 
export abstract class MetricClient implements IMetricClient {
  private isEnabled: boolean;
  private defaultNamespace: Namespace | undefined;
  private namespaces: Map<string, Namespace>;
  private attributes: Record<string, string>;
 
  constructor(isEnabled = true) {
    this.defaultNamespace = undefined;
    this.namespaces = new Map();
    this.attributes = {};
    this.isEnabled = isEnabled;
 
    this.setDefaultHostnameAttribute();
  }
 
  getDefaultNamespace(): Namespace | undefined {
    Iif (this.defaultNamespace === undefined) {
      Logger.getLogger().warn(Warnings.NoDefaultNamespaceSet);
      return undefined;
    }
 
    return this.defaultNamespace;
  }
 
  setDefaultNamespace(domain: string, module: string): void {
    this.defaultNamespace = this.getNamespace(domain, module);
  }
 
  getNamespace(domain: string, module: string): Namespace {
    const namespaceKey = this.getNamespaceKey(domain, module);
 
    let namespace = this.namespaces.get(namespaceKey);
 
    Iif (namespace === undefined) {
      namespace = new Namespace(this, domain, module);
      this.namespaces.set(namespaceKey, namespace);
    }
 
    return namespace;
  }
 
  getAttributes(): Record<string, string> {
    return this.attributes;
  }
 
  getAttribute(attributeName: string): string | undefined {
    return this.attributes[attributeName];
  }
 
  setAttribute(attributeName: string, attributeValue: string): void {
    this.attributes[attributeName] = attributeValue;
  }
 
  removeAttribute(attributeName: string): void {
    delete this.attributes[attributeName];
  }
 
  incrementCounter(metricName: string, value: number, attributes: Record<string, string>): void {
    if (!this.isEnabled) {
      return;
    }
 
    this.counterImpl(metricName, value, attributes);
  }
 
  recordGauge(metricName: string, value: number, attributes: Record<string, string>): void {
    if (!this.isEnabled) {
      return;
    }
 
    this.gaugeImpl(metricName, value, attributes);
  }
 
  recordTimer(metricName: string, value: number, buckets: number[], attributes: Record<string, string>): void {
    if (!this.isEnabled) {
      return;
    }
 
    this.timeImpl(metricName, value, buckets, attributes);
  }
 
  recordHistogram(metricName: string, value: number, buckets: number[], attributes: Record<string, string>): void {
    if (!this.isEnabled) {
      return;
    }
 
    this.histogramImpl(metricName, value, buckets, attributes);
  }
 
  protected getNamespaceKey(domain: string, module: string): string {
    return `${domain}_${module}`;
  }
 
  // NOTE: This behavior differs from the `hostname` attribute implementation across other telemetry wrapper libraries
  // in that it does not attempt to resolve the system hostname to an FQDN.
  private setDefaultHostnameAttribute(): void {
    try {
      this.setAttribute('hostname', os.hostname());
    } catch {
      Logger.getLogger().warn(Warnings.FailedToSetHostname);
    }
  }
 
  abstract counterImpl(metricName: string, value: number, attributes: Record<string, string>): void;
 
  abstract gaugeImpl(metricName: string, value: number, attributes: Record<string, string>): void;
 
  abstract timeImpl(metricName: string, value: number, buckets: number[], attributes: Record<string, string>): void;
 
  abstract histogramImpl(
    metricName: string,
    value: number,
    buckets: number[],
    attributes: Record<string, string>,
  ): void;
}