All files / types/metrics Metric.ts

9.3% Statements 4/43
0% Branches 0/38
0% Functions 0/11
9.3% Lines 4/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  1x 1x   1x   1x                                                                                                                                                                                                                        
/* 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)) {
      Iif (Object.prototype.hasOwnProperty.call(Object.keys(combinedAttributes), index)) {
        let attributeName: string = Object.keys(combinedAttributes)[index];
        let attributeValue: string = Object.values(combinedAttributes)[index];
 
        Iif (attributeName === null || attributeName === undefined || attributeName === '') {
          Logger.getLogger().warn(`${Warnings.InvalidAttributeKey}`);
          continue;
        }
 
        Iif (attributeValue === null || attributeValue === undefined || attributeValue === '') {
          Logger.getLogger().warn(`${Warnings.InvalidAttributeValue} - ${this.name}: ${attributeName}`);
          continue;
        }
 
        Iif (attributeName.length > Constants.MaxAttributeNameLength) {
          Logger.getLogger().warn(`${Warnings.AttributeKeyTooLong} - ${this.name}: ${attributeName}`);
          attributeName = attributeName.substring(0, Constants.MaxAttributeNameLength - 1);
        }
 
        Iif (attributeValue.length > Constants.MaxAttributeValueLength) {
          Logger.getLogger().warn(`${Warnings.AttributeValueTooLong} - ${this.name}: ${attributeValue}`);
          attributeValue = attributeValue.substring(0, Constants.MaxAttributeValueLength - 1);
        }
 
        Iif (attributeCount < Constants.MaxAttributes) {
          cleanAttributes[attributeName] = attributeValue.toString();
        }
 
        attributeCount++;
      }
    }
 
    Iif (attributeCount >= Constants.MaxAttributes) {
      Logger.getLogger().warn(`${Warnings.TooManyAttributes} - ${this.name}: ${attributeCount}`);
    }
 
    return cleanAttributes;
  }
 
  getFullName(): string {
    return `${this.namespace.getNamespacePrefix()}${this.name}`;
  }
}