diff --git a/ext/opentelemetry-ext-opencensusexporter/CHANGELOG.md b/ext/opentelemetry-ext-opencensusexporter/CHANGELOG.md index 4d1c9a97b82..52596a45596 100644 --- a/ext/opentelemetry-ext-opencensusexporter/CHANGELOG.md +++ b/ext/opentelemetry-ext-opencensusexporter/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +- Send start_timestamp and convert labels to strings + ([#937](https://github.com/open-telemetry/opentelemetry-python/pull/937)) + ## 0.8b0 Released 2020-05-27 diff --git a/ext/opentelemetry-ext-opencensusexporter/src/opentelemetry/ext/opencensusexporter/metrics_exporter/__init__.py b/ext/opentelemetry-ext-opencensusexporter/src/opentelemetry/ext/opencensusexporter/metrics_exporter/__init__.py index bb1a1ee888c..d6547637a7d 100644 --- a/ext/opentelemetry-ext-opencensusexporter/src/opentelemetry/ext/opencensusexporter/metrics_exporter/__init__.py +++ b/ext/opentelemetry-ext-opencensusexporter/src/opentelemetry/ext/opencensusexporter/metrics_exporter/__init__.py @@ -18,6 +18,7 @@ from typing import Sequence import grpc +from google.protobuf.timestamp_pb2 import Timestamp from opencensus.proto.agent.metrics.v1 import ( metrics_service_pb2, metrics_service_pb2_grpc, @@ -65,6 +66,8 @@ def __init__( self.client = client self.node = utils.get_node(service_name, host_name) + self.exporter_start_timestamp = Timestamp() + self.exporter_start_timestamp.GetCurrentTime() def export( self, metric_records: Sequence[MetricRecord] @@ -89,7 +92,9 @@ def shutdown(self) -> None: def generate_metrics_requests( self, metrics: Sequence[MetricRecord] ) -> metrics_service_pb2.ExportMetricsServiceRequest: - collector_metrics = translate_to_collector(metrics) + collector_metrics = translate_to_collector( + metrics, self.exporter_start_timestamp + ) service_request = metrics_service_pb2.ExportMetricsServiceRequest( node=self.node, metrics=collector_metrics ) @@ -99,6 +104,7 @@ def generate_metrics_requests( # pylint: disable=too-many-branches def translate_to_collector( metric_records: Sequence[MetricRecord], + exporter_start_timestamp: Timestamp, ) -> Sequence[metrics_pb2.Metric]: collector_metrics = [] for metric_record in metric_records: @@ -109,7 +115,8 @@ def translate_to_collector( label_keys.append(metrics_pb2.LabelKey(key=label_tuple[0])) label_values.append( metrics_pb2.LabelValue( - has_value=label_tuple[1] is not None, value=label_tuple[1] + has_value=label_tuple[1] is not None, + value=str(label_tuple[1]), ) ) @@ -121,9 +128,17 @@ def translate_to_collector( label_keys=label_keys, ) + # If cumulative and stateful, explicitly set the start_timestamp to + # exporter start time. + if metric_record.instrument.meter.batcher.stateful: + start_timestamp = exporter_start_timestamp + else: + start_timestamp = None + timeseries = metrics_pb2.TimeSeries( label_values=label_values, points=[get_collector_point(metric_record)], + start_timestamp=start_timestamp, ) collector_metrics.append( metrics_pb2.Metric( diff --git a/ext/opentelemetry-ext-opencensusexporter/tests/test_otcollector_metrics_exporter.py b/ext/opentelemetry-ext-opencensusexporter/tests/test_otcollector_metrics_exporter.py index f538e5acecd..51da1153d82 100644 --- a/ext/opentelemetry-ext-opencensusexporter/tests/test_otcollector_metrics_exporter.py +++ b/ext/opentelemetry-ext-opencensusexporter/tests/test_otcollector_metrics_exporter.py @@ -41,7 +41,7 @@ def setUpClass(cls): # pylint: disable=protected-access metrics.set_meter_provider(MeterProvider()) cls._meter = metrics.get_meter(__name__) - cls._labels = {"environment": "staging"} + cls._labels = {"environment": "staging", "number": 321} cls._key_labels = get_labels_as_key(cls._labels) def test_constructor(self): @@ -119,7 +119,7 @@ def test_export(self): client=mock_client, host_name=host_name ) test_metric = self._meter.create_metric( - "testname", "testdesc", "unit", int, Counter, ["environment"] + "testname", "testdesc", "unit", int, Counter, self._labels.keys(), ) record = MetricRecord( test_metric, self._key_labels, aggregate.SumAggregator(), @@ -142,13 +142,16 @@ def test_export(self): def test_translate_to_collector(self): test_metric = self._meter.create_metric( - "testname", "testdesc", "unit", int, Counter, ["environment"] + "testname", "testdesc", "unit", int, Counter, self._labels.keys() ) aggregator = aggregate.SumAggregator() aggregator.update(123) aggregator.take_checkpoint() record = MetricRecord(test_metric, self._key_labels, aggregator,) - output_metrics = metrics_exporter.translate_to_collector([record]) + start_timestamp = Timestamp() + output_metrics = metrics_exporter.translate_to_collector( + [record], start_timestamp, + ) self.assertEqual(len(output_metrics), 1) self.assertIsInstance(output_metrics[0], metrics_pb2.Metric) self.assertEqual(output_metrics[0].metric_descriptor.name, "testname") @@ -161,14 +164,20 @@ def test_translate_to_collector(self): metrics_pb2.MetricDescriptor.CUMULATIVE_INT64, ) self.assertEqual( - len(output_metrics[0].metric_descriptor.label_keys), 1 + len(output_metrics[0].metric_descriptor.label_keys), 2 ) self.assertEqual( output_metrics[0].metric_descriptor.label_keys[0].key, "environment", ) + self.assertEqual( + output_metrics[0].metric_descriptor.label_keys[1].key, "number", + ) self.assertEqual(len(output_metrics[0].timeseries), 1) - self.assertEqual(len(output_metrics[0].timeseries[0].label_values), 1) + self.assertEqual(len(output_metrics[0].timeseries[0].label_values), 2) + self.assertEqual( + output_metrics[0].timeseries[0].start_timestamp, start_timestamp + ) self.assertEqual( output_metrics[0].timeseries[0].label_values[0].has_value, True )