All files / src index.ts

93.33% Statements 98/105
79.41% Branches 27/34
81.48% Functions 22/27
91.95% Lines 80/87

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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206  1x 1x 1x 1x 1x   1x 1x 1x   1x   1x   1x   1x 9x     1x 6x   1x 5x   1x 1x   1x           2x 2x     2x     1x 1x 1x 1x       1x           3x 3x     3x     1x 2x 2x 2x       1x             2x 2x     2x     1x           1x 1x 1x       1x             6x 6x     6x     1x           1x 1x 1x       1x             4x 4x       4x     1x           4x 4x       4x     1x             6x 6x       6x     1x           2x   2x 2x 2x     2x       1x           2x   2x 2x 2x     2x       1x 1x 1x 1x 1x  
import { IMetricClient } from './types/client/IMetricClient';
import { Metrics } from './types/Metrics';
import { Counter } from './types/metrics/Counter';
import { Gauge } from './types/metrics/Gauge';
import { Histogram } from './types/metrics/Histogram';
import { Timer } from './types/metrics/Timer';
import { Namespace } from './types/Namespace';
import { Constants } from './types/Constants';
import { Configuration, TelemetryConfig } from './types/Configuration';
import { MetricClientFactory } from './types/client/MetricsClientFactory';
 
const config: TelemetryConfig = Configuration.initializeConfiguration(process.env.TELEMETRY_CONFIG);
 
Metrics.setDefaultClient(MetricClientFactory.createClient(config.metrics));
 
export const setDefaultClient = (client: IMetricClient): void => Metrics.setDefaultClient(client);
 
export const setDefaultNamespace = (domain: string, module: string): void => {
  Metrics.getMetricClient().setDefaultNamespace(domain, module);
};
 
export const getNamespace = (domain: string, module: string): Namespace =>
  Metrics.getMetricClient().getNamespace(domain, module);
 
export const setAttribute = (attributeName: string, attributeValue: string): void =>
  Metrics.getMetricClient().setAttribute(attributeName, attributeValue);
 
export const removeAttribute = (attributeName: string): void =>
  Metrics.getMetricClient().removeAttribute(attributeName);
 
export const createGauge = (
  metricName: string,
  description?: string,
  unit?: string,
  attributes: Record<string, string> = {},
): Gauge | undefined => {
  const namespace = Metrics.getMetricClient().getDefaultNamespace();
  Iif (namespace === undefined) {
    return undefined;
  }
  return namespace.createGauge(metricName, description, unit, attributes);
};
 
export const recordGauge = (metricName: string, value: number, attributes: Record<string, string> = {}): void => {
  const namespace = Metrics.getMetricClient().getDefaultNamespace();
  if (namespace !== undefined) {
    namespace.recordGauge(metricName, value, attributes);
  }
};
 
export const createCounter = (
  metricName: string,
  description?: string,
  unit?: string,
  attributes: Record<string, string> = {},
): Counter | undefined => {
  const namespace = Metrics.getMetricClient().getDefaultNamespace();
  Iif (namespace === undefined) {
    return undefined;
  }
  return namespace.createCounter(metricName, description, unit, attributes);
};
 
export const incrementCounter = (metricName: string, value = 1, attributes: Record<string, string> = {}): void => {
  const namespace = Metrics.getMetricClient().getDefaultNamespace();
  if (namespace !== undefined) {
    namespace.incrementCounter(metricName, value, attributes);
  }
};
 
export const createHistogram = (
  metricName: string,
  description?: string,
  unit?: string,
  buckets: number[] = [],
  attributes: Record<string, string> = {},
): Histogram | undefined => {
  const namespace = Metrics.getMetricClient().getDefaultNamespace();
  Iif (namespace === undefined) {
    return undefined;
  }
  return namespace.createHistogram(metricName, description, unit, buckets, attributes);
};
 
export const recordHistogram = (
  metricName: string,
  value: number,
  buckets: number[] = [],
  attributes: Record<string, string> = {},
): void => {
  const namespace = Metrics.getMetricClient().getDefaultNamespace();
  if (namespace !== undefined) {
    namespace.recordHistogram(metricName, value, buckets, attributes);
  }
};
 
export const createTimer = (
  metricName: string,
  description?: string,
  unit?: string,
  buckets: number[] = [],
  attributes: Record<string, string> = {},
): Timer | undefined => {
  const namespace = Metrics.getMetricClient().getDefaultNamespace();
  Iif (namespace === undefined) {
    return undefined;
  }
  return namespace.createTimer(metricName, description, unit, buckets, attributes);
};
 
export const recordTimer = (
  metricName: string,
  value: number,
  buckets: number[] = [],
  attributes: Record<string, string> = {},
): void => {
  const namespace = Metrics.getMetricClient().getDefaultNamespace();
  if (namespace !== undefined) {
    namespace.recordTimer(metricName, value, buckets, attributes);
  }
};
 
export function time<A extends any[], R>(
  metricName: string,
  buckets: number[] = Constants.DefaultTimerBuckets,
  attributes: Record<string, string> = {},
  fn: (...args: A) => R | Promise<R>,
  ...args1: A
): R | Promise<R> | undefined {
  const namespace = Metrics.getMetricClient().getDefaultNamespace();
  Iif (namespace === undefined) {
    return undefined;
  }
 
  return namespace.time(metricName, buckets, attributes, fn, ...args1);
}
 
export function timeAndCount<R>(
  metricNamePrefix: string,
  fn: () => R | Promise<R>,
  buckets: number[] = Constants.DefaultTimerBuckets,
  attributes: Record<string, string> = {},
): R | Promise<R> | undefined {
  const namespace = Metrics.getMetricClient().getDefaultNamespace();
  Iif (namespace === undefined) {
    return undefined;
  }
 
  return namespace.timeAndCount(metricNamePrefix, fn, buckets, attributes);
}
 
export function metricWrapper<A extends any[], R>(
  metricNamePrefix: string,
  buckets: number[] = Constants.DefaultTimerBuckets,
  attributes: Record<string, string> = {},
  fn: (...args: A) => R | Promise<R>,
  ...args1: A
): R | Promise<R> | undefined {
  const namespace = Metrics.getMetricClient().getDefaultNamespace();
  Iif (namespace === undefined) {
    return undefined;
  }
 
  return namespace.metricWrapper(metricNamePrefix, buckets, attributes, fn, ...args1);
}
 
export function timeDecorator<A extends any[]>(
  metricName: string,
  buckets: number[] = Constants.DefaultTimerBuckets,
  attributes: Record<string, string> = {},
) {
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  return function (_target: any, _propertyKey: string, descriptor: PropertyDescriptor) {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
    const value = descriptor.value;
    descriptor.value = function (...args: A) {
      return time(metricName, buckets, attributes, value, ...args);
    };
 
    return descriptor;
  };
}
 
export function timeAndCountDecorator<A extends any[]>(
  metricNamePrefix: string,
  buckets: number[] = Constants.DefaultTimerBuckets,
  attributes: Record<string, string> = {},
) {
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  return function (_target: any, _propertyKey: string, descriptor: PropertyDescriptor) {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
    const value = descriptor.value;
    descriptor.value = function (...args: A) {
      return metricWrapper(metricNamePrefix, buckets, attributes, value, ...args);
    };
 
    return descriptor;
  };
}
 
export * from './types/client/StatsDClient';
export * from './types/client/OTLPClient';
export * from './types/client/InMemoryClient';
export * from './util/index';
export { Metrics, Counter, Gauge, Histogram, Timer };