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 7c5b6da
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 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 @@ -203,9 +197,7 @@ def _translate_data(

def export(
self,
metrics: Dict[
Resource, Dict[InstrumentationScope, Iterable[MetricReaderMetric]]
],
metrics: MappingMetricT,
*args,
**kwargs
) -> MetricExportResult:
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 @@ -136,9 +140,7 @@ def get_metrics(self) -> List[Metric]:

def _receive_metrics(
self,
metrics: Dict[
Resource, Dict[InstrumentationScope, Iterable[MetricReaderMetric]]
],
metrics: MappingMetricT,
*args,
**kwargs
) -> None:
Expand Down Expand Up @@ -213,9 +215,7 @@ def _ticker(self) -> None:

def _receive_metrics(
self,
metrics: Dict[
Resource, Dict[InstrumentationScope, Iterable[MetricReaderMetric]]
],
metrics: MappingMetricT,
*args,
**kwargs
) -> None:
Expand Down

0 comments on commit 7c5b6da

Please sign in to comment.