Skip to content

Commit

Permalink
api/sdk: Rename CounterAggregator -> SumAggregator (#816)
Browse files Browse the repository at this point in the history
Enables broader usage of the aggregator, across multiple counter types.
  • Loading branch information
aabmass authored Jun 15, 2020
1 parent 0b823a1 commit b70450e
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
MetricsExporter,
MetricsExportResult,
)
from opentelemetry.sdk.metrics.export.aggregate import CounterAggregator
from opentelemetry.sdk.metrics.export.aggregate import SumAggregator

logger = logging.getLogger(__name__)
MAX_BATCH_WRITE = 200
Expand Down Expand Up @@ -97,7 +97,7 @@ def _get_metric_descriptor(
logger.warning(
"Label value %s is not a string, bool or integer", value
)
if isinstance(record.aggregator, CounterAggregator):
if isinstance(record.aggregator, SumAggregator):
descriptor["metric_kind"] = MetricDescriptor.MetricKind.GAUGE
else:
logger.warning(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
CloudMonitoringMetricsExporter,
)
from opentelemetry.sdk.metrics.export import MetricRecord
from opentelemetry.sdk.metrics.export.aggregate import CounterAggregator
from opentelemetry.sdk.metrics.export.aggregate import SumAggregator


class UnsupportedAggregator:
Expand Down Expand Up @@ -114,7 +114,7 @@ def test_get_metric_descriptor(self):
)

record = MetricRecord(
MockMetric(), (("label1", "value1"),), CounterAggregator(),
MockMetric(), (("label1", "value1"),), SumAggregator(),
)
metric_descriptor = exporter._get_metric_descriptor(record)
client.create_metric_descriptor.assert_called_with(
Expand Down Expand Up @@ -149,7 +149,7 @@ def test_get_metric_descriptor(self):
("label3", 3),
("label4", False),
),
CounterAggregator(),
SumAggregator(),
)
)
client.create_metric_descriptor.assert_called_with(
Expand Down Expand Up @@ -204,20 +204,20 @@ def test_export(self):
}
)

counter_one = CounterAggregator()
counter_one.checkpoint = 1
counter_one.last_update_timestamp = (WRITE_INTERVAL + 1) * 1e9
sum_agg_one = SumAggregator()
sum_agg_one.checkpoint = 1
sum_agg_one.last_update_timestamp = (WRITE_INTERVAL + 1) * 1e9
exporter.export(
[
MetricRecord(
MockMetric(),
(("label1", "value1"), ("label2", 1),),
counter_one,
sum_agg_one,
),
MetricRecord(
MockMetric(),
(("label1", "value2"), ("label2", 2),),
counter_one,
sum_agg_one,
),
]
)
Expand Down Expand Up @@ -245,33 +245,33 @@ def test_export(self):
# Attempting to export too soon after another export with the exact
# same labels leads to it being dropped

counter_two = CounterAggregator()
counter_two.checkpoint = 1
counter_two.last_update_timestamp = (WRITE_INTERVAL + 2) * 1e9
sum_agg_two = SumAggregator()
sum_agg_two.checkpoint = 1
sum_agg_two.last_update_timestamp = (WRITE_INTERVAL + 2) * 1e9
exporter.export(
[
MetricRecord(
MockMetric(),
(("label1", "value1"), ("label2", 1),),
counter_two,
sum_agg_two,
),
MetricRecord(
MockMetric(),
(("label1", "value2"), ("label2", 2),),
counter_two,
sum_agg_two,
),
]
)
self.assertEqual(client.create_time_series.call_count, 1)

# But exporting with different labels is fine
counter_two.checkpoint = 2
sum_agg_two.checkpoint = 2
exporter.export(
[
MetricRecord(
MockMetric(),
(("label1", "changed_label"), ("label2", 2),),
counter_two,
sum_agg_two,
),
]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_get_collector_metric_type(self):
self.assertIs(result, metrics_pb2.MetricDescriptor.UNSPECIFIED)

def test_get_collector_point(self):
aggregator = aggregate.CounterAggregator()
aggregator = aggregate.SumAggregator()
int_counter = self._meter.create_metric(
"testName", "testDescription", "unit", int, Counter
)
Expand Down Expand Up @@ -122,7 +122,7 @@ def test_export(self):
"testname", "testdesc", "unit", int, Counter, ["environment"]
)
record = MetricRecord(
test_metric, self._key_labels, aggregate.CounterAggregator(),
test_metric, self._key_labels, aggregate.SumAggregator(),
)

result = collector_exporter.export([record])
Expand All @@ -144,7 +144,7 @@ def test_translate_to_collector(self):
test_metric = self._meter.create_metric(
"testname", "testdesc", "unit", int, Counter, ["environment"]
)
aggregator = aggregate.CounterAggregator()
aggregator = aggregate.SumAggregator()
aggregator.update(123)
aggregator.take_checkpoint()
record = MetricRecord(test_metric, self._key_labels, aggregator,)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.aggregate import CounterAggregator
from opentelemetry.sdk.metrics.export.aggregate import SumAggregator


class TestPrometheusMetricExporter(unittest.TestCase):
Expand Down Expand Up @@ -67,7 +67,7 @@ def test_shutdown(self):
def test_export(self):
with self._registry_register_patch:
record = MetricRecord(
self._test_metric, self._labels_key, CounterAggregator(),
self._test_metric, self._labels_key, SumAggregator(),
)
exporter = PrometheusMetricsExporter()
result = exporter.export([record])
Expand All @@ -87,7 +87,7 @@ def test_counter_to_prometheus(self):
)
labels = {"environment@": "staging", "os": "Windows"}
key_labels = metrics.get_labels_as_key(labels)
aggregator = CounterAggregator()
aggregator = SumAggregator()
aggregator.update(123)
aggregator.take_checkpoint()
record = MetricRecord(metric, key_labels, aggregator)
Expand Down
3 changes: 3 additions & 0 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- Rename CounterAggregator -> SumAggregator
([#816](https://github.com/open-telemetry/opentelemetry-python/pull/816))

## 0.9b0

Released 2020-06-10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def merge(self, other):
"""Combines two aggregator values."""


class CounterAggregator(Aggregator):
class SumAggregator(Aggregator):
"""Aggregator for Counter metrics."""

def __init__(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
from opentelemetry.sdk.metrics.export import MetricRecord
from opentelemetry.sdk.metrics.export.aggregate import (
Aggregator,
CounterAggregator,
LastValueAggregator,
MinMaxSumCountAggregator,
SumAggregator,
ValueObserverAggregator,
)

Expand Down Expand Up @@ -57,15 +57,15 @@ def aggregator_for(self, instrument_type: Type[InstrumentT]) -> Aggregator:
"""
# pylint:disable=R0201
if issubclass(instrument_type, (Counter, UpDownCounter)):
return CounterAggregator()
return SumAggregator()
if issubclass(instrument_type, (SumObserver, UpDownSumObserver)):
return LastValueAggregator()
if issubclass(instrument_type, ValueRecorder):
return MinMaxSumCountAggregator()
if issubclass(instrument_type, ValueObserver):
return ValueObserverAggregator()
# TODO: Add other aggregators
return CounterAggregator()
return SumAggregator()

def checkpoint_set(self) -> Sequence[MetricRecord]:
"""Returns a list of MetricRecords used for exporting.
Expand Down
Loading

0 comments on commit b70450e

Please sign in to comment.