Skip to content

Commit

Permalink
OpenCensus agent exporter fixes
Browse files Browse the repository at this point in the history
- Pass start_timestamp to the timeseries in OC exporter for stateful
metrics
- Convert label values to strings, just in case
  • Loading branch information
aabmass committed Jul 28, 2020
1 parent 7f74a89 commit e044ea8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
3 changes: 3 additions & 0 deletions ext/opentelemetry-ext-opencensusexporter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand All @@ -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
)
Expand All @@ -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:
Expand All @@ -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]),
)
)

Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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(),
Expand All @@ -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")
Expand All @@ -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
)
Expand Down

0 comments on commit e044ea8

Please sign in to comment.