From 1aa416649e07216d8f5034b380c9fac1cc6e0ee5 Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Tue, 11 Dec 2018 15:40:12 +0800 Subject: [PATCH] fix: fix client oom by repeat create metric object --- packages/metrics/src/MetricsClient.ts | 35 +++++++++++++++++++-------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/packages/metrics/src/MetricsClient.ts b/packages/metrics/src/MetricsClient.ts index 61928574..e3677055 100644 --- a/packages/metrics/src/MetricsClient.ts +++ b/packages/metrics/src/MetricsClient.ts @@ -154,32 +154,47 @@ export class MetricsClient extends AbstractIndicator { } getCounter(group: string, name: MetricName | string) { - const counter = new Counter(); - this.register(group, name, counter); + let counter = this.allMetricsRegistry.getMetric(this.buildName(name)); + if (!counter) { + counter = new Counter(); + this.register(group, name, counter); + } return counter; } getTimer(group: string, name: MetricName | string) { - const timer = new Timer(); - this.register(group, name, timer); + let timer = this.allMetricsRegistry.getMetric(this.buildName(name)); + if (!timer) { + timer = new Timer(); + this.register(group, name, timer); + } return timer; } getMeter(group: string, name: MetricName | string) { - const meter = new Meter(); - this.register(group, name, meter); + let meter = this.allMetricsRegistry.getMetric(this.buildName(name)); + if (!meter) { + meter = new Meter(); + this.register(group, name, meter); + } return meter; } getHistogram(group: string, name: MetricName | string) { - const histogram = new Histogram(); - this.register(group, name, histogram); + let histogram = this.allMetricsRegistry.getMetric(this.buildName(name)); + if (!histogram) { + histogram = new Histogram(); + this.register(group, name, histogram); + } return histogram; } getFastCompass(group: string, name: MetricName | string) { - const fastCompass = new FastCompass(); - this.register(group, name, fastCompass); + let fastCompass = this.allMetricsRegistry.getMetric(this.buildName(name)); + if (!fastCompass) { + fastCompass = new FastCompass(); + this.register(group, name, fastCompass); + } return fastCompass; }