All files / src/types/metrics Metric.ts

100% Statements 43/43
86.84% Branches 33/38
100% Functions 11/11
100% Lines 43/43

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  10x 10x   10x   10x                           93x 93x 93x 93x 93x       77x       18x       14x       14x       78x       1x       13x       1x       75x             75x   75x 75x 420x 420x 420x   420x 1x 1x     419x 1x 1x     418x 1x 1x     418x 1x 1x     418x 390x     418x       75x 2x     75x       105x      
/* eslint-disable @typescript-eslint/no-for-in-array */
import { Constants } from '../Constants';
import { Logger } from '../logging/Logger';
import { Namespace } from '../Namespace';
import { Warnings } from '../Warnings';
 
export abstract class Metric {
  private namespace: Namespace;
  private name: string;
  private description: string;
  private unit: string;
  private attributes: Record<string, string>;
 
  constructor(
    namespace: Namespace,
    name: string,
    description?: string,
    unit?: string,
    attributes: Record<string, string> = {},
  ) {
    this.namespace = namespace;
    this.name = name;
    this.description = description ?? '';
    this.unit = unit ?? '';
    this.attributes = attributes;
  }
 
  getNamespace(): Namespace {
    return this.namespace;
  }
 
  getName(): string {
    return this.name;
  }
 
  getDescription(): string {
    return this.description;
  }
 
  getUnit(): string {
    return this.unit;
  }
 
  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];
  }
 
  combineAttributes(attributes: Record<string, string>): Record<string, string> {
    const combinedAttributes = {
      ...(this.namespace.getMetricClient().getAttributes() ?? {}),
      ...(this.namespace.getAttributes() ?? {}),
      ...(this.getAttributes() ?? {}),
      ...(attributes ?? {}),
    };
 
    const cleanAttributes: Record<string, string> = {};
 
    let attributeCount = 0;
    for (const index in Object.keys(combinedAttributes)) {
      if (Object.prototype.hasOwnProperty.call(Object.keys(combinedAttributes), index)) {
        let attributeName: string = Object.keys(combinedAttributes)[index];
        let attributeValue: string = Object.values(combinedAttributes)[index];
 
        if (attributeName === null || attributeName === undefined || attributeName === '') {
          Logger.getLogger().warn(`${Warnings.InvalidAttributeKey}`);
          continue;
        }
 
        if (attributeValue === null || attributeValue === undefined || attributeValue === '') {
          Logger.getLogger().warn(`${Warnings.InvalidAttributeValue} - ${this.name}: ${attributeName}`);
          continue;
        }
 
        if (attributeName.length > Constants.MaxAttributeNameLength) {
          Logger.getLogger().warn(`${Warnings.AttributeKeyTooLong} - ${this.name}: ${attributeName}`);
          attributeName = attributeName.substring(0, Constants.MaxAttributeNameLength - 1);
        }
 
        if (attributeValue.length > Constants.MaxAttributeValueLength) {
          Logger.getLogger().warn(`${Warnings.AttributeValueTooLong} - ${this.name}: ${attributeValue}`);
          attributeValue = attributeValue.substring(0, Constants.MaxAttributeValueLength - 1);
        }
 
        if (attributeCount < Constants.MaxAttributes) {
          cleanAttributes[attributeName] = attributeValue.toString();
        }
 
        attributeCount++;
      }
    }
 
    if (attributeCount >= Constants.MaxAttributes) {
      Logger.getLogger().warn(`${Warnings.TooManyAttributes} - ${this.name}: ${attributeCount}`);
    }
 
    return cleanAttributes;
  }
 
  getFullName(): string {
    return `${this.namespace.getNamespacePrefix()}${this.name}`;
  }
}