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 | 10x 10x 10x 10x 10x 78x 78x 78x 78x 78x 35x 1x 1x 34x 11x 19x 19x 19x 11x 11x 19x 80x 1x 101x 2x 29x 3x 26x 16x 3x 13x 41x 3x 38x 48x 3x 45x 20x 78x 78x | 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 {
if (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);
if (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;
}
|