Skip to content

Commit

Permalink
Revert "Remove console exporter metric output if empty"
Browse files Browse the repository at this point in the history
This reverts commit 3407110.
  • Loading branch information
ocelotl committed Aug 15, 2023
1 parent 3407110 commit 02a08b6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 42 deletions.
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Remove console exporter metric output if empty
([#3335](https://github.com/open-telemetry/opentelemetry-python/pull/3335))
- Modify Prometheus exporter to translate non-monotonic Sums into Gauges
([#3306](https://github.com/open-telemetry/opentelemetry-python/pull/3306))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def _set_collect_callback(
@abstractmethod
def _receive_metrics(
self,
metrics_data: Optional[MetricsData],
metrics_data: "opentelemetry.sdk.metrics.export.MetricsData",
timeout_millis: float = 10_000,
**kwargs,
) -> None:
Expand Down Expand Up @@ -386,9 +386,9 @@ def __init__(
preferred_aggregation=preferred_aggregation,
)
self._lock = RLock()
self._metrics_data: Optional[
self._metrics_data: (
"opentelemetry.sdk.metrics.export.MetricsData"
] = None
) = None

def get_metrics_data(
self,
Expand All @@ -402,7 +402,7 @@ def get_metrics_data(

def _receive_metrics(
self,
metrics_data: Optional[MetricsData],
metrics_data: "opentelemetry.sdk.metrics.export.MetricsData",
timeout_millis: float = 10_000,
**kwargs,
) -> None:
Expand Down Expand Up @@ -511,7 +511,7 @@ def _ticker(self) -> None:

def _receive_metrics(
self,
metrics_data: Optional[MetricsData],
metrics_data: MetricsData,
timeout_millis: float = 10_000,
**kwargs,
) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from abc import ABC, abstractmethod
from threading import Lock
from time import time_ns
from typing import List, Mapping, Optional
from typing import Iterable, List, Mapping

# This kind of import is needed to avoid Sphinx errors.
import opentelemetry.sdk.metrics
Expand All @@ -29,7 +29,7 @@
from opentelemetry.sdk.metrics._internal.metric_reader_storage import (
MetricReaderStorage,
)
from opentelemetry.sdk.metrics._internal.point import MetricsData
from opentelemetry.sdk.metrics._internal.point import Metric


class MeasurementConsumer(ABC):
Expand All @@ -51,7 +51,7 @@ def collect(
self,
metric_reader: "opentelemetry.sdk.metrics.MetricReader",
timeout_millis: float = 10_000,
) -> Optional[MetricsData]:
) -> Iterable[Metric]:
pass


Expand Down Expand Up @@ -94,7 +94,7 @@ def collect(
self,
metric_reader: "opentelemetry.sdk.metrics.MetricReader",
timeout_millis: float = 10_000,
) -> Optional[MetricsData]:
) -> Iterable[Metric]:

with self._lock:
metric_reader_storage = self._reader_storages[metric_reader]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from logging import getLogger
from threading import RLock
from time import time_ns
from typing import Dict, List, Optional
from typing import Dict, List

from opentelemetry.metrics import (
Asynchronous,
Expand Down Expand Up @@ -119,7 +119,7 @@ def consume_measurement(self, measurement: Measurement) -> None:
):
view_instrument_match.consume_measurement(measurement)

def collect(self) -> Optional[MetricsData]:
def collect(self) -> MetricsData:
# Use a list instead of yielding to prevent a slow reader from holding
# SDK locks

Expand Down Expand Up @@ -231,19 +231,17 @@ def collect(self) -> Optional[MetricsData]:
instrument.instrumentation_scope
].metrics.extend(metrics)

scope_metrics = list(instrumentation_scope_scope_metrics.values())

if scope_metrics:

return MetricsData(
resource_metrics=[
ResourceMetrics(
resource=self._sdk_config.resource,
scope_metrics=scope_metrics,
schema_url=self._sdk_config.resource.schema_url,
)
]
)
return MetricsData(
resource_metrics=[
ResourceMetrics(
resource=self._sdk_config.resource,
scope_metrics=list(
instrumentation_scope_scope_metrics.values()
),
schema_url=self._sdk_config.resource.schema_url,
)
]
)

def _handle_view_instrument_match(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,3 @@ def test_console_exporter(self):

self.assertEqual(metrics["attributes"], {"a": "b"})
self.assertEqual(metrics["value"], 1)

def test_console_exporter_no_export(self):

output = StringIO()
exporter = ConsoleMetricExporter(out=output)
reader = PeriodicExportingMetricReader(
exporter, export_interval_millis=100
)
provider = MeterProvider(metric_readers=[reader])
provider.shutdown()

output.seek(0)
actual = "".join(output.readlines())
expected = ""

self.assertEqual(actual, expected)

0 comments on commit 02a08b6

Please sign in to comment.