From c4950a3b65e41d89e04c6f8673db933cfe5932a3 Mon Sep 17 00:00:00 2001 From: Shovnik Bhattacharya Date: Mon, 16 Nov 2020 19:09:57 -0500 Subject: [PATCH] Rename MetricRecord to ExportRecord (#1367) --- .../opencensus/metrics_exporter/__init__.py | 46 +++++------ .../test_otcollector_metrics_exporter.py | 12 +-- .../otlp/metrics_exporter/__init__.py | 76 +++++++++---------- .../tests/test_otlp_metric_exporter.py | 12 +-- .../exporter/prometheus/__init__.py | 34 ++++----- .../tests/test_prometheus_exporter.py | 10 +-- opentelemetry-sdk/CHANGELOG.md | 5 +- .../sdk/metrics/export/__init__.py | 18 ++--- .../export/in_memory_metrics_exporter.py | 6 +- .../sdk/metrics/export/processor.py | 16 ++-- .../tests/metrics/export/test_export.py | 4 +- 11 files changed, 119 insertions(+), 120 deletions(-) diff --git a/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/metrics_exporter/__init__.py b/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/metrics_exporter/__init__.py index de3c8a8b453..979600173a2 100644 --- a/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/metrics_exporter/__init__.py +++ b/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/metrics_exporter/__init__.py @@ -29,7 +29,7 @@ import opentelemetry.exporter.opencensus.util as utils from opentelemetry.sdk.metrics import Counter, Metric from opentelemetry.sdk.metrics.export import ( - MetricRecord, + ExportRecord, MetricsExporter, MetricsExportResult, ) @@ -79,11 +79,11 @@ def __init__( self.exporter_start_timestamp.GetCurrentTime() def export( - self, metric_records: Sequence[MetricRecord] + self, export_records: Sequence[ExportRecord] ) -> MetricsExportResult: try: responses = self.client.Export( - self.generate_metrics_requests(metric_records) + self.generate_metrics_requests(export_records) ) # Read response @@ -99,7 +99,7 @@ def shutdown(self) -> None: pass def generate_metrics_requests( - self, metrics: Sequence[MetricRecord] + self, metrics: Sequence[ExportRecord] ) -> metrics_service_pb2.ExportMetricsServiceRequest: collector_metrics = translate_to_collector( metrics, self.exporter_start_timestamp @@ -112,15 +112,15 @@ def generate_metrics_requests( # pylint: disable=too-many-branches def translate_to_collector( - metric_records: Sequence[MetricRecord], + export_records: Sequence[ExportRecord], exporter_start_timestamp: Timestamp, ) -> Sequence[metrics_pb2.Metric]: collector_metrics = [] - for metric_record in metric_records: + for export_record in export_records: label_values = [] label_keys = [] - for label_tuple in metric_record.labels: + for label_tuple in export_record.labels: label_keys.append(metrics_pb2.LabelKey(key=label_tuple[0])) label_values.append( metrics_pb2.LabelValue( @@ -130,30 +130,30 @@ def translate_to_collector( ) metric_descriptor = metrics_pb2.MetricDescriptor( - name=metric_record.instrument.name, - description=metric_record.instrument.description, - unit=metric_record.instrument.unit, - type=get_collector_metric_type(metric_record.instrument), + name=export_record.instrument.name, + description=export_record.instrument.description, + unit=export_record.instrument.unit, + type=get_collector_metric_type(export_record.instrument), label_keys=label_keys, ) # If cumulative and stateful, explicitly set the start_timestamp to # exporter start time. - if metric_record.instrument.meter.processor.stateful: + if export_record.instrument.meter.processor.stateful: start_timestamp = exporter_start_timestamp else: start_timestamp = None timeseries = metrics_pb2.TimeSeries( label_values=label_values, - points=[get_collector_point(metric_record)], + points=[get_collector_point(export_record)], start_timestamp=start_timestamp, ) collector_metrics.append( metrics_pb2.Metric( metric_descriptor=metric_descriptor, timeseries=[timeseries], - resource=get_resource(metric_record), + resource=get_resource(export_record), ) ) return collector_metrics @@ -169,29 +169,29 @@ def get_collector_metric_type(metric: Metric) -> metrics_pb2.MetricDescriptor: return metrics_pb2.MetricDescriptor.UNSPECIFIED -def get_collector_point(metric_record: MetricRecord) -> metrics_pb2.Point: +def get_collector_point(export_record: ExportRecord) -> metrics_pb2.Point: # TODO: horrible hack to get original list of keys to then get the bound # instrument point = metrics_pb2.Point( timestamp=utils.proto_timestamp_from_time_ns( - metric_record.aggregator.last_update_timestamp + export_record.aggregator.last_update_timestamp ) ) - if metric_record.instrument.value_type == int: - point.int64_value = metric_record.aggregator.checkpoint - elif metric_record.instrument.value_type == float: - point.double_value = metric_record.aggregator.checkpoint + if export_record.instrument.value_type == int: + point.int64_value = export_record.aggregator.checkpoint + elif export_record.instrument.value_type == float: + point.double_value = export_record.aggregator.checkpoint else: raise TypeError( "Unsupported metric type: {}".format( - metric_record.instrument.value_type + export_record.instrument.value_type ) ) return point -def get_resource(metric_record: MetricRecord) -> resource_pb2.Resource: - resource_attributes = metric_record.resource.attributes +def get_resource(export_record: ExportRecord) -> resource_pb2.Resource: + resource_attributes = export_record.resource.attributes return resource_pb2.Resource( type=infer_oc_resource_type(resource_attributes), labels={k: str(v) for k, v in resource_attributes.items()}, diff --git a/exporter/opentelemetry-exporter-opencensus/tests/test_otcollector_metrics_exporter.py b/exporter/opentelemetry-exporter-opencensus/tests/test_otcollector_metrics_exporter.py index 441c75b6822..b48a4a5a33d 100644 --- a/exporter/opentelemetry-exporter-opencensus/tests/test_otcollector_metrics_exporter.py +++ b/exporter/opentelemetry-exporter-opencensus/tests/test_otcollector_metrics_exporter.py @@ -28,7 +28,7 @@ get_dict_as_key, ) from opentelemetry.sdk.metrics.export import ( - MetricRecord, + ExportRecord, MetricsExportResult, aggregate, ) @@ -100,7 +100,7 @@ def test_get_collector_point(self): "testName", "testDescription", "unit", float, ) result = metrics_exporter.get_collector_point( - MetricRecord( + ExportRecord( int_counter, self._key_labels, aggregator, @@ -113,7 +113,7 @@ def test_get_collector_point(self): aggregator.update(123.5) aggregator.take_checkpoint() result = metrics_exporter.get_collector_point( - MetricRecord( + ExportRecord( float_counter, self._key_labels, aggregator, @@ -124,7 +124,7 @@ def test_get_collector_point(self): self.assertRaises( TypeError, metrics_exporter.get_collector_point( - MetricRecord( + ExportRecord( valuerecorder, self._key_labels, aggregator, @@ -144,7 +144,7 @@ def test_export(self): test_metric = self._meter.create_counter( "testname", "testdesc", "unit", int, self._labels.keys(), ) - record = MetricRecord( + record = ExportRecord( test_metric, self._key_labels, aggregate.SumAggregator(), @@ -173,7 +173,7 @@ def test_translate_to_collector(self): aggregator = aggregate.SumAggregator() aggregator.update(123) aggregator.take_checkpoint() - record = MetricRecord( + record = ExportRecord( test_metric, self._key_labels, aggregator, diff --git a/exporter/opentelemetry-exporter-otlp/src/opentelemetry/exporter/otlp/metrics_exporter/__init__.py b/exporter/opentelemetry-exporter-otlp/src/opentelemetry/exporter/otlp/metrics_exporter/__init__.py index 2078664b439..c65b2a89df2 100644 --- a/exporter/opentelemetry-exporter-otlp/src/opentelemetry/exporter/otlp/metrics_exporter/__init__.py +++ b/exporter/opentelemetry-exporter-otlp/src/opentelemetry/exporter/otlp/metrics_exporter/__init__.py @@ -54,7 +54,7 @@ ValueRecorder, ) from opentelemetry.sdk.metrics.export import ( - MetricRecord, + ExportRecord, MetricsExporter, MetricsExportResult, ) @@ -71,39 +71,37 @@ def _get_data_points( - sdk_metric_record: MetricRecord, data_point_class: Type[DataPointT] + export_record: ExportRecord, data_point_class: Type[DataPointT] ) -> List[DataPointT]: - if isinstance(sdk_metric_record.aggregator, SumAggregator): - value = sdk_metric_record.aggregator.checkpoint + if isinstance(export_record.aggregator, SumAggregator): + value = export_record.aggregator.checkpoint - elif isinstance(sdk_metric_record.aggregator, MinMaxSumCountAggregator): + elif isinstance(export_record.aggregator, MinMaxSumCountAggregator): # FIXME: How are values to be interpreted from this aggregator? raise Exception("MinMaxSumCount aggregator data not supported") - elif isinstance(sdk_metric_record.aggregator, HistogramAggregator): + elif isinstance(export_record.aggregator, HistogramAggregator): # FIXME: How are values to be interpreted from this aggregator? raise Exception("Histogram aggregator data not supported") - elif isinstance(sdk_metric_record.aggregator, LastValueAggregator): - value = sdk_metric_record.aggregator.checkpoint + elif isinstance(export_record.aggregator, LastValueAggregator): + value = export_record.aggregator.checkpoint - elif isinstance(sdk_metric_record.aggregator, ValueObserverAggregator): - value = sdk_metric_record.aggregator.checkpoint.last + elif isinstance(export_record.aggregator, ValueObserverAggregator): + value = export_record.aggregator.checkpoint.last return [ data_point_class( labels=[ StringKeyValue(key=str(label_key), value=str(label_value)) - for label_key, label_value in sdk_metric_record.labels + for label_key, label_value in export_record.labels ], value=value, start_time_unix_nano=( - sdk_metric_record.aggregator.initial_checkpoint_timestamp - ), - time_unix_nano=( - sdk_metric_record.aggregator.last_update_timestamp + export_record.aggregator.initial_checkpoint_timestamp ), + time_unix_nano=(export_record.aggregator.last_update_timestamp), ) ] @@ -111,7 +109,7 @@ def _get_data_points( class OTLPMetricsExporter( MetricsExporter, OTLPExporterMixin[ - MetricRecord, ExportMetricsServiceRequest, MetricsExportResult + ExportRecord, ExportMetricsServiceRequest, MetricsExportResult ], ): # pylint: disable=unsubscriptable-object @@ -162,14 +160,14 @@ def __init__( # pylint: disable=no-self-use def _translate_data( - self, data: Sequence[MetricRecord] + self, export_records: Sequence[ExportRecord] ) -> ExportMetricsServiceRequest: # pylint: disable=too-many-locals,no-member # pylint: disable=attribute-defined-outside-init sdk_resource_instrumentation_library_metrics = {} - # The criteria to decide how to translate data is based on this table + # The criteria to decide how to translate export_records is based on this table # taken directly from OpenTelemetry Proto v0.5.0: # TODO: Update table after the decision on: @@ -185,13 +183,13 @@ def _translate_data( # SumObserver Sum(aggregation_temporality=cumulative;is_monotonic=true) # UpDownSumObserver Sum(aggregation_temporality=cumulative;is_monotonic=false) # ValueObserver Gauge() - for sdk_metric_record in data: + for export_record in export_records: - if sdk_metric_record.resource not in ( + if export_record.resource not in ( sdk_resource_instrumentation_library_metrics.keys() ): sdk_resource_instrumentation_library_metrics[ - sdk_metric_record.resource + export_record.resource ] = InstrumentationLibraryMetrics() type_class = { @@ -210,16 +208,16 @@ def _translate_data( }, } - value_type = sdk_metric_record.instrument.value_type + value_type = export_record.instrument.value_type sum_class = type_class[value_type]["sum"]["class"] gauge_class = type_class[value_type]["gauge"]["class"] data_point_class = type_class[value_type]["data_point_class"] - if isinstance(sdk_metric_record.instrument, Counter): + if isinstance(export_record.instrument, Counter): otlp_metric_data = sum_class( data_points=_get_data_points( - sdk_metric_record, data_point_class + export_record, data_point_class ), aggregation_temporality=( AggregationTemporality.AGGREGATION_TEMPORALITY_DELTA @@ -228,10 +226,10 @@ def _translate_data( ) argument = type_class[value_type]["sum"]["argument"] - elif isinstance(sdk_metric_record.instrument, UpDownCounter): + elif isinstance(export_record.instrument, UpDownCounter): otlp_metric_data = sum_class( data_points=_get_data_points( - sdk_metric_record, data_point_class + export_record, data_point_class ), aggregation_temporality=( AggregationTemporality.AGGREGATION_TEMPORALITY_DELTA @@ -240,14 +238,14 @@ def _translate_data( ) argument = type_class[value_type]["sum"]["argument"] - elif isinstance(sdk_metric_record.instrument, (ValueRecorder)): + elif isinstance(export_record.instrument, (ValueRecorder)): logger.warning("Skipping exporting of ValueRecorder metric") continue - elif isinstance(sdk_metric_record.instrument, SumObserver): + elif isinstance(export_record.instrument, SumObserver): otlp_metric_data = sum_class( data_points=_get_data_points( - sdk_metric_record, data_point_class + export_record, data_point_class ), aggregation_temporality=( AggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE @@ -256,10 +254,10 @@ def _translate_data( ) argument = type_class[value_type]["sum"]["argument"] - elif isinstance(sdk_metric_record.instrument, UpDownSumObserver): + elif isinstance(export_record.instrument, UpDownSumObserver): otlp_metric_data = sum_class( data_points=_get_data_points( - sdk_metric_record, data_point_class + export_record, data_point_class ), aggregation_temporality=( AggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE @@ -268,24 +266,22 @@ def _translate_data( ) argument = type_class[value_type]["sum"]["argument"] - elif isinstance(sdk_metric_record.instrument, (ValueObserver)): + elif isinstance(export_record.instrument, (ValueObserver)): otlp_metric_data = gauge_class( data_points=_get_data_points( - sdk_metric_record, data_point_class + export_record, data_point_class ) ) argument = type_class[value_type]["gauge"]["argument"] sdk_resource_instrumentation_library_metrics[ - sdk_metric_record.resource + export_record.resource ].metrics.append( OTLPMetric( **{ - "name": sdk_metric_record.instrument.name, - "description": ( - sdk_metric_record.instrument.description - ), - "unit": sdk_metric_record.instrument.unit, + "name": export_record.instrument.name, + "description": (export_record.instrument.description), + "unit": export_record.instrument.unit, argument: otlp_metric_data, } ) @@ -299,6 +295,6 @@ def _translate_data( ) ) - def export(self, metrics: Sequence[MetricRecord]) -> MetricsExportResult: + def export(self, metrics: Sequence[ExportRecord]) -> MetricsExportResult: # pylint: disable=arguments-differ return self._export(metrics) diff --git a/exporter/opentelemetry-exporter-otlp/tests/test_otlp_metric_exporter.py b/exporter/opentelemetry-exporter-otlp/tests/test_otlp_metric_exporter.py index 3034fcdf651..dd03c27d842 100644 --- a/exporter/opentelemetry-exporter-otlp/tests/test_otlp_metric_exporter.py +++ b/exporter/opentelemetry-exporter-otlp/tests/test_otlp_metric_exporter.py @@ -40,7 +40,7 @@ Resource as OTLPResource, ) from opentelemetry.sdk.metrics import Counter, MeterProvider -from opentelemetry.sdk.metrics.export import MetricRecord +from opentelemetry.sdk.metrics.export import ExportRecord from opentelemetry.sdk.metrics.export.aggregate import SumAggregator from opentelemetry.sdk.resources import Resource as SDKResource @@ -50,7 +50,7 @@ def setUp(self): self.exporter = OTLPMetricsExporter(insecure=True) resource = SDKResource(OrderedDict([("a", 1), ("b", False)])) - self.counter_metric_record = MetricRecord( + self.counter_export_record = ExportRecord( Counter( "c", "d", @@ -97,9 +97,9 @@ def test_translate_metrics(self, mock_time_ns): mock_time_ns.configure_mock(**{"return_value": 1}) - self.counter_metric_record.aggregator.checkpoint = 1 - self.counter_metric_record.aggregator.initial_checkpoint_timestamp = 1 - self.counter_metric_record.aggregator.last_update_timestamp = 1 + self.counter_export_record.aggregator.checkpoint = 1 + self.counter_export_record.aggregator.initial_checkpoint_timestamp = 1 + self.counter_export_record.aggregator.last_update_timestamp = 1 expected = ExportMetricsServiceRequest( resource_metrics=[ @@ -146,6 +146,6 @@ def test_translate_metrics(self, mock_time_ns): ) # pylint: disable=protected-access - actual = self.exporter._translate_data([self.counter_metric_record]) + actual = self.exporter._translate_data([self.counter_export_record]) self.assertEqual(expected, actual) diff --git a/exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py b/exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py index 8ca89a94d29..1c13903564e 100644 --- a/exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py +++ b/exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py @@ -77,7 +77,7 @@ from opentelemetry.metrics import Counter, ValueRecorder from opentelemetry.sdk.metrics.export import ( - MetricRecord, + ExportRecord, MetricsExporter, MetricsExportResult, ) @@ -99,9 +99,9 @@ def __init__(self, prefix: str = ""): REGISTRY.register(self._collector) def export( - self, metric_records: Sequence[MetricRecord] + self, export_records: Sequence[ExportRecord] ) -> MetricsExportResult: - self._collector.add_metrics_data(metric_records) + self._collector.add_metrics_data(export_records) return MetricsExportResult.SUCCESS def shutdown(self) -> None: @@ -120,8 +120,8 @@ def __init__(self, prefix: str = ""): r"[^\w]", re.UNICODE | re.IGNORECASE ) - def add_metrics_data(self, metric_records: Sequence[MetricRecord]) -> None: - self._metrics_to_export.append(metric_records) + def add_metrics_data(self, export_records: Sequence[ExportRecord]) -> None: + self._metrics_to_export.append(export_records) def collect(self): """Collect fetches the metrics from OpenTelemetry @@ -131,38 +131,38 @@ def collect(self): """ while self._metrics_to_export: - for metric_record in self._metrics_to_export.popleft(): + for export_record in self._metrics_to_export.popleft(): prometheus_metric = self._translate_to_prometheus( - metric_record + export_record ) if prometheus_metric is not None: yield prometheus_metric - def _translate_to_prometheus(self, metric_record: MetricRecord): + def _translate_to_prometheus(self, export_record: ExportRecord): prometheus_metric = None label_values = [] label_keys = [] - for label_tuple in metric_record.labels: + for label_tuple in export_record.labels: label_keys.append(self._sanitize(label_tuple[0])) label_values.append(label_tuple[1]) metric_name = "" if self._prefix != "": metric_name = self._prefix + "_" - metric_name += self._sanitize(metric_record.instrument.name) + metric_name += self._sanitize(export_record.instrument.name) - description = getattr(metric_record.instrument, "description", "") - if isinstance(metric_record.instrument, Counter): + description = getattr(export_record.instrument, "description", "") + if isinstance(export_record.instrument, Counter): prometheus_metric = CounterMetricFamily( name=metric_name, documentation=description, labels=label_keys ) prometheus_metric.add_metric( - labels=label_values, value=metric_record.aggregator.checkpoint + labels=label_values, value=export_record.aggregator.checkpoint ) # TODO: Add support for histograms when supported in OT - elif isinstance(metric_record.instrument, ValueRecorder): - value = metric_record.aggregator.checkpoint - if isinstance(metric_record.aggregator, MinMaxSumCountAggregator): + elif isinstance(export_record.instrument, ValueRecorder): + value = export_record.aggregator.checkpoint + if isinstance(export_record.aggregator, MinMaxSumCountAggregator): prometheus_metric = SummaryMetricFamily( name=metric_name, documentation=description, @@ -183,7 +183,7 @@ def _translate_to_prometheus(self, metric_record: MetricRecord): else: logger.warning( - "Unsupported metric type. %s", type(metric_record.instrument) + "Unsupported metric type. %s", type(export_record.instrument) ) return prometheus_metric diff --git a/exporter/opentelemetry-exporter-prometheus/tests/test_prometheus_exporter.py b/exporter/opentelemetry-exporter-prometheus/tests/test_prometheus_exporter.py index c7c0c5b071a..5813fba33ca 100644 --- a/exporter/opentelemetry-exporter-prometheus/tests/test_prometheus_exporter.py +++ b/exporter/opentelemetry-exporter-prometheus/tests/test_prometheus_exporter.py @@ -24,7 +24,7 @@ ) from opentelemetry.metrics import get_meter_provider, set_meter_provider from opentelemetry.sdk import metrics -from opentelemetry.sdk.metrics.export import MetricRecord, MetricsExportResult +from opentelemetry.sdk.metrics.export import ExportRecord, MetricsExportResult from opentelemetry.sdk.metrics.export.aggregate import ( MinMaxSumCountAggregator, SumAggregator, @@ -66,7 +66,7 @@ def test_shutdown(self): def test_export(self): with self._registry_register_patch: - record = MetricRecord( + record = ExportRecord( self._test_metric, self._labels_key, SumAggregator(), @@ -89,7 +89,7 @@ def test_min_max_sum_aggregator_to_prometheus(self): aggregator.update(123) aggregator.update(456) aggregator.take_checkpoint() - record = MetricRecord( + record = ExportRecord( metric, key_labels, aggregator, get_meter_provider().resource ) collector = CustomCollector("testprefix") @@ -107,7 +107,7 @@ def test_counter_to_prometheus(self): aggregator = SumAggregator() aggregator.update(123) aggregator.take_checkpoint() - record = MetricRecord( + record = ExportRecord( metric, key_labels, aggregator, get_meter_provider().resource ) collector = CustomCollector("testprefix") @@ -135,7 +135,7 @@ def test_invalid_metric(self): metric = StubMetric("tesname", "testdesc", "unit", int, meter) labels = {"environment": "staging"} key_labels = get_dict_as_key(labels) - record = MetricRecord( + record = ExportRecord( metric, key_labels, None, get_meter_provider().resource ) collector = CustomCollector("testprefix") diff --git a/opentelemetry-sdk/CHANGELOG.md b/opentelemetry-sdk/CHANGELOG.md index 2a3ca9b5867..47ae86852a0 100644 --- a/opentelemetry-sdk/CHANGELOG.md +++ b/opentelemetry-sdk/CHANGELOG.md @@ -2,7 +2,10 @@ ## Unreleased -- Add optional parameter to `record_exception` method ([#1314](https://github.com/open-telemetry/opentelemetry-python/pull/1314)) +- Rename `MetricRecord` class to `ExportRecord` + ([#1367](https://github.com/open-telemetry/opentelemetry-python/pull/1367)) +- Add optional parameter to `record_exception` method + ([#1314](https://github.com/open-telemetry/opentelemetry-python/pull/1314)) - Update exception handling optional parameters, add escaped attribute to record_exception ([#1365](https://github.com/open-telemetry/opentelemetry-python/pull/1365)) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/export/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/export/__init__.py index e7882217ec4..60ae4b9466b 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/export/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/export/__init__.py @@ -25,7 +25,7 @@ class MetricsExportResult(Enum): FAILURE = 1 -class MetricRecord: +class ExportRecord: def __init__( self, instrument: metrics_api.InstrumentT, @@ -47,12 +47,12 @@ class MetricsExporter: """ def export( - self, metric_records: Sequence[MetricRecord] + self, export_records: Sequence[ExportRecord] ) -> "MetricsExportResult": """Exports a batch of telemetry data. Args: - metric_records: A sequence of `MetricRecord` s. A `MetricRecord` + export_records: A sequence of `ExportRecord` s. A `ExportRecord` contains the metric to be exported, the labels associated with that metric, as well as the aggregator used to export the current checkpointed value. @@ -76,16 +76,16 @@ class ConsoleMetricsExporter(MetricsExporter): """ def export( - self, metric_records: Sequence[MetricRecord] + self, export_records: Sequence[ExportRecord] ) -> "MetricsExportResult": - for record in metric_records: + for export_record in export_records: print( '{}(data="{}", labels="{}", value={}, resource={})'.format( type(self).__name__, - record.instrument, - record.labels, - record.aggregator.checkpoint, - record.resource.attributes, + export_record.instrument, + export_record.labels, + export_record.aggregator.checkpoint, + export_record.resource.attributes, ) ) return MetricsExportResult.SUCCESS diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/export/in_memory_metrics_exporter.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/export/in_memory_metrics_exporter.py index e75fd350f71..7261a8f2286 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/export/in_memory_metrics_exporter.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/export/in_memory_metrics_exporter.py @@ -16,7 +16,7 @@ from typing import Sequence from opentelemetry.sdk.metrics.export import ( - MetricRecord, + ExportRecord, MetricsExporter, MetricsExportResult, ) @@ -41,13 +41,13 @@ def clear(self): self._exported_metrics.clear() def export( - self, metric_records: Sequence[MetricRecord] + self, export_records: Sequence[ExportRecord] ) -> MetricsExportResult: if self._stopped: return MetricsExportResult.FAILURE with self._lock: - self._exported_metrics.extend(metric_records) + self._exported_metrics.extend(export_records) return MetricsExportResult.SUCCESS def get_exported_metrics(self): diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/export/processor.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/export/processor.py index fa16f5f4ea7..765c94e2c5e 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/export/processor.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/export/processor.py @@ -14,7 +14,7 @@ from typing import Sequence -from opentelemetry.sdk.metrics.export import MetricRecord +from opentelemetry.sdk.metrics.export import ExportRecord from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.util import get_dict_as_key @@ -36,22 +36,22 @@ def __init__(self, stateful: bool, resource: Resource): self.stateful = stateful self._resource = resource - def checkpoint_set(self) -> Sequence[MetricRecord]: - """Returns a list of MetricRecords used for exporting. + def checkpoint_set(self) -> Sequence[ExportRecord]: + """Returns a list of ExportRecords used for exporting. - The list of MetricRecords is a snapshot created from the current + The list of ExportRecords is a snapshot created from the current data in all of the aggregators in this processor. """ - metric_records = [] + export_records = [] # pylint: disable=W0612 for ( (instrument, aggregator_type, _, labels), aggregator, ) in self._batch_map.items(): - metric_records.append( - MetricRecord(instrument, labels, aggregator, self._resource) + export_records.append( + ExportRecord(instrument, labels, aggregator, self._resource) ) - return metric_records + return export_records def finished_collection(self): """Performs certain post-export logic. diff --git a/opentelemetry-sdk/tests/metrics/export/test_export.py b/opentelemetry-sdk/tests/metrics/export/test_export.py index a96c8af455a..635409770d1 100644 --- a/opentelemetry-sdk/tests/metrics/export/test_export.py +++ b/opentelemetry-sdk/tests/metrics/export/test_export.py @@ -22,7 +22,7 @@ from opentelemetry.sdk import metrics from opentelemetry.sdk.metrics.export import ( ConsoleMetricsExporter, - MetricRecord, + ExportRecord, ) from opentelemetry.sdk.metrics.export.aggregate import ( LastValueAggregator, @@ -52,7 +52,7 @@ def test_export(self): ) labels = {"environment": "staging"} aggregator = SumAggregator() - record = MetricRecord( + record = ExportRecord( metric, labels, aggregator, meter_provider.resource ) result = '{}(data="{}", labels="{}", value={}, resource={})'.format(