Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor InMemoryMetricExporter and add tests #3519

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
- Refactor InMemoryMetricExporter and add tests ([#3519](https://github.com/open-telemetry/opentelemetry-python/pull/3519))

## Version 1.21.0/0.42b0 (2023-11-01)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from sys import stdout
from threading import Event, Lock, RLock, Thread
from time import time_ns
from typing import IO, Callable, Dict, Iterable, Optional
from typing import IO, Callable, Dict, Iterable, Optional, Sequence

from typing_extensions import final

Expand Down Expand Up @@ -56,7 +56,7 @@
_ObservableUpDownCounter,
_UpDownCounter,
)
from opentelemetry.sdk.metrics._internal.point import MetricsData
from opentelemetry.sdk.metrics._internal.point import Metric, MetricsData
from opentelemetry.util._once import Once

_logger = getLogger(__name__)
Expand Down Expand Up @@ -171,6 +171,39 @@ def force_flush(self, timeout_millis: float = 10_000) -> bool:
return True


class InMemoryMetricExporter(MetricExporter):
"""Implementation of :class:`.MetricExporter` that stores metrics in memory.

This class can be used for testing purposes. It stores the exported metrics
in a dictionary in memory, indexed by an auto-incrementing counter that can
be retrieved using the `metrics` attribute.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
be retrieved using the `metrics` attribute.
be retrieved using the `_counter` attribute.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you did intend to highlight the metrics attribute, you could rephrase the text to something like:

This class is designed for testing purposes. It maintains exported metrics in the metrics dictionary attribute.


The `export` method adds the metrics to the in-memory store and increments
the counter. Each set of metrics can be accessed by its unique index.
"""

def __init__(self):
super().__init__()
self.metrics = {}
self._counter = 0

def export(
self,
metrics_data: Sequence[Metric],
timeout_millis: float = 10_000,
**kwargs,
) -> MetricExportResult:
self.metrics[self._counter] = metrics_data
self._counter += 1
return MetricExportResult.SUCCESS

def shutdown(self, timeout_millis: float = 30_000, **kwargs) -> None:
pass

def force_flush(self, timeout_millis: float = 10_000) -> bool:
return True


class MetricReader(ABC):
# pylint: disable=too-many-branches
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from opentelemetry.sdk.metrics._internal.export import (
AggregationTemporality,
ConsoleMetricExporter,
InMemoryMetricExporter,
InMemoryMetricReader,
MetricExporter,
MetricExportResult,
Expand Down Expand Up @@ -44,6 +45,7 @@
__all__ = [
"AggregationTemporality",
"ConsoleMetricExporter",
"InMemoryMetricExporter",
"InMemoryMetricReader",
"MetricExporter",
"MetricExportResult",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Sequence
from unittest import TestCase

from opentelemetry.sdk.metrics.export import (
InMemoryMetricExporter,
MetricExportResult,
)


class MockMetric:
# A placeholder class for mock metrics
pass


class TestInMemoryMetricExporter(TestCase):
def setUp(self):
self.exporter = InMemoryMetricExporter()

def test_initialization(self):
self.assertIsInstance(self.exporter.metrics, dict)
self.assertEqual(len(self.exporter.metrics), 0)
self.assertEqual(self.exporter._counter, 0)

def test_export(self):
mock_metrics: Sequence[MockMetric] = [MockMetric(), MockMetric()]

# Test the first export
result = self.exporter.export(mock_metrics)
self.assertEqual(result, MetricExportResult.SUCCESS)
self.assertIn(0, self.exporter.metrics)
self.assertEqual(self.exporter.metrics[0], mock_metrics)
self.assertEqual(self.exporter._counter, 1)

# Test the second export
result = self.exporter.export(mock_metrics)
self.assertEqual(result, MetricExportResult.SUCCESS)
self.assertIn(1, self.exporter.metrics)
self.assertEqual(self.exporter.metrics[1], mock_metrics)
self.assertEqual(self.exporter._counter, 2)

def test_force_flush(self):
self.assertTrue(self.exporter.force_flush())
28 changes: 2 additions & 26 deletions opentelemetry-sdk/tests/metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from logging import WARNING
from time import sleep
from typing import Iterable, Sequence
from typing import Iterable
from unittest import TestCase
from unittest.mock import MagicMock, Mock, patch

Expand All @@ -32,9 +32,8 @@
)
from opentelemetry.sdk.metrics._internal import SynchronousMeasurementConsumer
from opentelemetry.sdk.metrics.export import (
InMemoryMetricExporter,
Metric,
MetricExporter,
MetricExportResult,
MetricReader,
PeriodicExportingMetricReader,
)
Expand Down Expand Up @@ -447,29 +446,6 @@ def test_create_observable_up_down_counter(self):
self.assertEqual(observable_up_down_counter.name, "name")


class InMemoryMetricExporter(MetricExporter):
def __init__(self):
super().__init__()
self.metrics = {}
self._counter = 0

def export(
self,
metrics: Sequence[Metric],
timeout_millis: float = 10_000,
**kwargs,
) -> MetricExportResult:
self.metrics[self._counter] = metrics
self._counter += 1
return MetricExportResult.SUCCESS

def shutdown(self, timeout_millis: float = 30_000, **kwargs) -> None:
pass

def force_flush(self, timeout_millis: float = 10_000) -> bool:
return True


class TestDuplicateInstrumentAggregateData(TestCase):
def test_duplicate_instrument_aggregate_data(self):

Expand Down
Loading