Skip to content

Commit

Permalink
Refactored type for metrics data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed May 6, 2022
1 parent b7e7c90 commit fe55d5c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from logging import getLogger
from os import environ
from typing import Optional, Sequence, Dict, Iterable
from typing import Optional, Sequence, Dict
from grpc import ChannelCredentials, Compression
from opentelemetry.exporter.otlp.proto.grpc.exporter import (
OTLPExporterMixin,
Expand All @@ -40,14 +40,12 @@
from opentelemetry.sdk._metrics.export import (
MetricExporter,
MetricExportResult,
MappingMetricT,
)
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.util.instrumentation import (
InstrumentationScope as SDKInstrumentationScope,
)
from opentelemetry.sdk._metrics.metric_reader import (
Metric as MetricReaderMetric,
)

_logger = getLogger(__name__)

Expand Down Expand Up @@ -86,11 +84,7 @@ def __init__(
)

def _translate_data(
self,
data: Dict[
Resource,
Dict[SDKInstrumentationScope, Iterable[MetricReaderMetric]],
],
self, data: MappingMetricT
) -> ExportMetricsServiceRequest:

sdk_resource_scope_metrics: Dict[
Expand Down Expand Up @@ -202,12 +196,7 @@ def _translate_data(
)

def export(
self,
metrics: Dict[
Resource, Dict[InstrumentationScope, Iterable[MetricReaderMetric]]
],
*args,
**kwargs
self, metrics: MappingMetricT, *args, **kwargs
) -> MetricExportResult:
return self._export(metrics)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def _receive_metrics(
Resource, Dict[InstrumentationScope, Iterable[MetricReaderMetric]]
],
*args,
**kwargs
**kwargs,
) -> None:
if metrics is None:
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from os import environ, linesep
from sys import stdout
from threading import Event, RLock, Thread
from typing import IO, Callable, Dict, Iterable, List, Optional, Sequence
from typing import IO, Callable, Dict, Iterable, List, Mapping, Optional

from opentelemetry.context import (
_SUPPRESS_INSTRUMENTATION_KEY,
Expand All @@ -39,6 +39,10 @@

_logger = logging.getLogger(__name__)

MappingMetricT = Mapping[
Resource, Mapping[InstrumentationScope, Iterable[MetricReaderMetric]]
]


class MetricExportResult(Enum):
"""Result of exporting a metric
Expand All @@ -58,7 +62,7 @@ class MetricExporter(ABC):

@abstractmethod
def export(
self, metrics: Sequence[Metric], *args, **kwargs
self, metrics: MappingMetricT, *args, **kwargs
) -> "MetricExportResult":
"""Exports a batch of telemetry data.
Expand Down Expand Up @@ -95,7 +99,7 @@ def __init__(
self.formatter = formatter

def export(
self, metrics: Sequence[Metric], *args, **kwargs
self, metrics: MappingMetricT, *args, **kwargs
) -> MetricExportResult:
for metric in metrics:
self.out.write(self.formatter(metric))
Expand Down Expand Up @@ -135,12 +139,7 @@ def get_metrics(self) -> List[Metric]:
return metrics

def _receive_metrics(
self,
metrics: Dict[
Resource, Dict[InstrumentationScope, Iterable[MetricReaderMetric]]
],
*args,
**kwargs
self, metrics: MappingMetricT, *args, **kwargs
) -> None:
with self._lock:
self._metrics = metrics
Expand Down Expand Up @@ -212,12 +211,7 @@ def _ticker(self) -> None:
self.collect()

def _receive_metrics(
self,
metrics: Dict[
Resource, Dict[InstrumentationScope, Iterable[MetricReaderMetric]]
],
*args,
**kwargs
self, metrics: MappingMetricT, *args, **kwargs
) -> None:
if metrics is None:
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def _receive_metrics(
self,
metrics: Dict[Resource, Dict[InstrumentationScope, Iterable[Metric]]],
*args,
**kwargs
**kwargs,
) -> None:
"""Called by `MetricReader.collect` when it receives a batch of metrics"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from opentelemetry.sdk._metrics._internal.export import ( # noqa: F401
ConsoleMetricExporter,
InMemoryMetricReader,
MappingMetricT,
MetricExporter,
MetricExportResult,
PeriodicExportingMetricReader,
Expand Down

0 comments on commit fe55d5c

Please sign in to comment.