From 9aeb45a90e98f5cbc58aef36780931efc750459a Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Mon, 20 Dec 2021 08:54:41 -0800 Subject: [PATCH] add more stubs in place --- .../proto/grpc/_metric_exporter/__init__.py | 15 ++++++++ .../sdk/_metrics/export/metric_exporter.py | 38 ++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/_metric_exporter/__init__.py b/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/_metric_exporter/__init__.py index 92bb52c37e8..87227e73917 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/_metric_exporter/__init__.py +++ b/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/_metric_exporter/__init__.py @@ -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, ) @@ -30,6 +33,9 @@ class OTLPMetricExporter( MetricExporter, OTLPExporterMixin, ): + _result = MetricExportResult + _stub = MetricsServiceStub + def __init__( self, endpoint: Optional[str] = None, @@ -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 diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/export/metric_exporter.py b/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/export/metric_exporter.py index 5365a2899eb..5db4d836d78 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/export/metric_exporter.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/export/metric_exporter.py @@ -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): @@ -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. @@ -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