Skip to content

Commit

Permalink
add more stubs in place
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Boten committed Dec 20, 2021
1 parent 39afa69 commit 9aeb45a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
from opentelemetry.exporter.otlp.proto.grpc.exporter import (
OTLPExporterMixin,
)
from opentelemetry.proto.collector.metrics.v1.metrics_service_pb2_grpc import (
MetricsServiceStub,
)
from opentelemetry.sdk._metrics import (
MetricData,
)
Expand All @@ -30,6 +33,9 @@ class OTLPMetricExporter(
MetricExporter,
OTLPExporterMixin,
):
_result = MetricExportResult
_stub = MetricsServiceStub

def __init__(
self,
endpoint: Optional[str] = None,
Expand All @@ -54,3 +60,12 @@ def _translate_data(
self, data: Sequence[MetricData]
) -> MetricExportResult:
return super()._translate_data(data)

def export(self, batch: Sequence[MetricData]) -> MetricExportResult:
for data in batch:
# TODO: do something with the data
pass
return MetricExportResult.SUCCESS

def shutdown(self):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
# limitations under the License.

import abc
import sys
from enum import Enum
from os import linesep
from typing import IO, Callable, Sequence

from opentelemetry.sdk._metrics import MetricData
from opentelemetry.sdk._metrics.measurement import Measurement


class MetricExportResult(Enum):
Expand All @@ -28,8 +34,18 @@ class MetricExporter(abc.ABC):
in their own format.
"""

def export(self, metrics: Sequence[MetricData]) -> "MetricExportResult":
"""Exports a batch of telemetry data.
Args:
metrics: The list of `opentelemetry.trace.Span` objects to be exported
Returns:
The result of the export
"""

@abc.abstractmethod
def shutdown(self):
def shutdown(self) -> None:
"""Shuts down the exporter.
Called when the SDK is shut down.
Expand All @@ -43,3 +59,23 @@ class ConsoleMetricExporter(MetricExporter):
This class can be used for diagnostic purposes. It prints the exported
metrics to the console STDOUT.
"""

def __init__(
self,
out: IO = sys.stdout,
formatter: Callable[
[Measurement], str
] = lambda record: record.to_json()
+ linesep,
):
self.out = out
self.formatter = formatter

def export(self, batch: Sequence[MetricData]) -> MetricExportResult:
for data in batch:
self.out.write(self.formatter(data.log_record))
self.out.flush()
return MetricExportResult.SUCCESS

def shutdown(self) -> None:
pass

0 comments on commit 9aeb45a

Please sign in to comment.