|
| 1 | +/*! |
| 2 | + * Copyright 2019, OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { MetricProducer, MetricProducerManager } from './types'; |
| 18 | + |
| 19 | +/** |
| 20 | + * Keeps a set of MetricProducer that is used by exporters to determine the |
| 21 | + * metrics that need to be exported. |
| 22 | + */ |
| 23 | +class BaseMetricProducerManager implements MetricProducerManager { |
| 24 | + /** Singleton instance */ |
| 25 | + private static _singletonInstance: MetricProducerManager; |
| 26 | + |
| 27 | + private _metricProducers: Set<MetricProducer> = new Set<MetricProducer>(); |
| 28 | + |
| 29 | + /** Gets the instance. */ |
| 30 | + static get instance(): MetricProducerManager { |
| 31 | + return this._singletonInstance || (this._singletonInstance = new this()); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Adds the MetricProducer to the manager if it is not already present. |
| 36 | + * |
| 37 | + * @param metricProducer The MetricProducer to be added to the manager. |
| 38 | + */ |
| 39 | + add(metricProducer: MetricProducer): void { |
| 40 | + // TODO: How to we handle this validation - throws in OpenCensus? |
| 41 | + // validateNotNull(metricProducer, 'metricProducer'); |
| 42 | + if (!this._metricProducers.has(metricProducer)) { |
| 43 | + this._metricProducers.add(metricProducer); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Removes the MetricProducer to the manager if it is present. |
| 49 | + * |
| 50 | + * @param metricProducer The MetricProducer to be removed from the manager. |
| 51 | + */ |
| 52 | + remove(metricProducer: MetricProducer): void { |
| 53 | + // TODO: How to we handle this validation - throws in OpenCensus? |
| 54 | + // validateNotNull(metricProducer, 'metricProducer'); |
| 55 | + this._metricProducers.delete(metricProducer); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Clears all MetricProducers. |
| 60 | + */ |
| 61 | + removeAll(): void { |
| 62 | + this._metricProducers.clear(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Returns all registered MetricProducers that should be exported. |
| 67 | + * |
| 68 | + * This method should be used by any metrics exporter that automatically |
| 69 | + * exports data for MetricProducer registered with the MetricProducerManager. |
| 70 | + * |
| 71 | + * @return {Set<MetricProducer>} The Set of MetricProducers. |
| 72 | + */ |
| 73 | + getAllMetricProducer(): Set<MetricProducer> { |
| 74 | + return this._metricProducers; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +export const metricProducerManagerInstance = BaseMetricProducerManager.instance; |
0 commit comments