|
| 1 | +# Copyright 2019, OpenCensus Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import threading |
| 16 | + |
| 17 | + |
| 18 | +class MetricProducer(object): |
| 19 | + """Produces a set of metrics for export.""" |
| 20 | + |
| 21 | + def get_metrics(self): |
| 22 | + """Get a set of metrics to be exported. |
| 23 | +
|
| 24 | + :rtype: set(:class: `opencensus.metrics.export.metric.Metric`) |
| 25 | + :return: A set of metrics to be exported. |
| 26 | + """ |
| 27 | + raise NotImplementedError # pragma: NO COVER |
| 28 | + |
| 29 | + |
| 30 | +class MetricProducerManager(object): |
| 31 | + """Container class for MetricProducers to be used by exporters. |
| 32 | +
|
| 33 | + :type metric_producers: iterable(class: 'MetricProducer') |
| 34 | + :param metric_producers: Optional initial metric producers. |
| 35 | + """ |
| 36 | + |
| 37 | + def __init__(self, metric_producers=None): |
| 38 | + if metric_producers is None: |
| 39 | + self.metric_producers = set() |
| 40 | + else: |
| 41 | + self.metric_producers = set(metric_producers) |
| 42 | + self.mp_lock = threading.Lock() |
| 43 | + |
| 44 | + def add(self, metric_producer): |
| 45 | + """Add a metric producer. |
| 46 | +
|
| 47 | + :type metric_producer: :class: 'MetricProducer' |
| 48 | + :param metric_producer: The metric producer to add. |
| 49 | + """ |
| 50 | + if metric_producer is None: |
| 51 | + raise ValueError |
| 52 | + with self.mp_lock: |
| 53 | + self.metric_producers.add(metric_producer) |
| 54 | + |
| 55 | + def remove(self, metric_producer): |
| 56 | + """Remove a metric producer. |
| 57 | +
|
| 58 | + :type metric_producer: :class: 'MetricProducer' |
| 59 | + :param metric_producer: The metric producer to remove. |
| 60 | + """ |
| 61 | + if metric_producer is None: |
| 62 | + raise ValueError |
| 63 | + try: |
| 64 | + with self.mp_lock: |
| 65 | + self.metric_producers.remove(metric_producer) |
| 66 | + except KeyError: |
| 67 | + pass |
| 68 | + |
| 69 | + def get_all(self): |
| 70 | + """Get the set of all metric producers. |
| 71 | +
|
| 72 | + Get a copy of `metric_producers`. Prefer this method to using the |
| 73 | + attribute directly to avoid other threads adding/removing producers |
| 74 | + while you're reading it. |
| 75 | +
|
| 76 | + :rtype: set(:class: `MetricProducer`) |
| 77 | + :return: A set of all metric producers at the time of the call. |
| 78 | + """ |
| 79 | + with self.mp_lock: |
| 80 | + mps_copy = set(self.metric_producers) |
| 81 | + return mps_copy |
0 commit comments