|  | 
|  | 1 | +# Copyright The OpenTelemetry 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 gc | 
|  | 16 | +import time | 
|  | 17 | +import weakref | 
|  | 18 | +from typing import Sequence | 
|  | 19 | +from unittest import TestCase | 
|  | 20 | + | 
|  | 21 | +from opentelemetry.proto.metrics.v1.metrics_pb2 import Metric | 
|  | 22 | +from opentelemetry.sdk.metrics import MeterProvider | 
|  | 23 | +from opentelemetry.sdk.metrics._internal.export import MetricExporter, MetricExportResult, PeriodicExportingMetricReader | 
|  | 24 | + | 
|  | 25 | + | 
|  | 26 | +class FakeMetricsExporter(MetricExporter): | 
|  | 27 | +    def __init__(self, wait=0, preferred_temporality=None, preferred_aggregation=None): | 
|  | 28 | +        self.wait = wait | 
|  | 29 | +        self.metrics = [] | 
|  | 30 | +        self._shutdown = False | 
|  | 31 | +        super().__init__( | 
|  | 32 | +            preferred_temporality=preferred_temporality, | 
|  | 33 | +            preferred_aggregation=preferred_aggregation, | 
|  | 34 | +        ) | 
|  | 35 | + | 
|  | 36 | +    def export( | 
|  | 37 | +            self, | 
|  | 38 | +            metrics_data: Sequence[Metric], | 
|  | 39 | +            timeout_millis: float = 10_000, | 
|  | 40 | +            **kwargs, | 
|  | 41 | +    ) -> MetricExportResult: | 
|  | 42 | +        time.sleep(self.wait) | 
|  | 43 | +        self.metrics.extend(metrics_data) | 
|  | 44 | +        return True | 
|  | 45 | + | 
|  | 46 | +    def shutdown(self, timeout_millis: float = 30_000, **kwargs) -> None: | 
|  | 47 | +        self._shutdown = True | 
|  | 48 | + | 
|  | 49 | +    def force_flush(self, timeout_millis: float = 10_000) -> bool: | 
|  | 50 | +        return True | 
|  | 51 | + | 
|  | 52 | + | 
|  | 53 | +class TestMeterProviderShutdown(TestCase): | 
|  | 54 | +    def test_meter_provider_shutdown_triggers_garbage_collection(self): | 
|  | 55 | +        def create_and_shutdown(): | 
|  | 56 | +            exporter = FakeMetricsExporter() | 
|  | 57 | +            exporter_wr = weakref.ref(exporter) | 
|  | 58 | + | 
|  | 59 | +            reader = PeriodicExportingMetricReader(exporter) | 
|  | 60 | +            reader_wr = weakref.ref(reader) | 
|  | 61 | + | 
|  | 62 | +            provider = MeterProvider(metric_readers=[reader]) | 
|  | 63 | +            provider_wr = weakref.ref(provider) | 
|  | 64 | + | 
|  | 65 | +            provider.shutdown() | 
|  | 66 | + | 
|  | 67 | +            return exporter_wr, reader_wr, provider_wr | 
|  | 68 | + | 
|  | 69 | +        # When: the provider is shutdown | 
|  | 70 | +        exporter_weakref, reader_weakref, provider_weakref = create_and_shutdown() | 
|  | 71 | +        gc.collect() | 
|  | 72 | + | 
|  | 73 | +        # Then: the provider, exporter and reader should be garbage collected | 
|  | 74 | +        self.assertIsNone(exporter_weakref()) | 
|  | 75 | +        self.assertIsNone(reader_weakref()) | 
|  | 76 | +        self.assertIsNone(provider_weakref()) | 
0 commit comments