Support int typed aggregations#696
Conversation
5c78be8 to
7e2474f
Compare
7e2474f to
1681a35
Compare
|
@lzchen please help to review. |
songy23
left a comment
There was a problem hiding this comment.
Looks good overall, please fix lint.
opencensus/stats/aggregation.py
Outdated
| def __init__(self, buckets=None): | ||
| self._buckets = buckets or [] | ||
|
|
||
| @property |
There was a problem hiding this comment.
You still have an aggregation_type property. As well remove the param comment.
There was a problem hiding this comment.
I ended up removing BaseAggregation altogether, per @c24t's comments.
opencensus/stats/aggregation.py
Outdated
| @@ -78,16 +61,24 @@ class SumAggregation(BaseAggregation): | |||
| :param aggregation_type: represents the type of this aggregation | |||
There was a problem hiding this comment.
IMO, I would include a get_metric_type(measure) method in BaseAggregation with no implementation to let developers know that aggregations should implement this (there might be new kinds of aggregations)
opencensus/stats/aggregation.py
Outdated
| @@ -101,16 +92,16 @@ class CountAggregation(BaseAggregation): | |||
| :param aggregation_type: represents the type of this aggregation | |||
opencensus/stats/aggregation_data.py
Outdated
| :class:`opencensus.metrics.export.value.ValueDouble` or | ||
| :class:`opencensus.metrics.export.value.ValueLong` | ||
| :param value_type: the type of value to be used when creating a point | ||
| :type sum_data: meh |
opencensus/stats/aggregation.py
Outdated
| @@ -78,16 +61,24 @@ class SumAggregation(BaseAggregation): | |||
| :param aggregation_type: represents the type of this aggregation | |||
There was a problem hiding this comment.
Nit: Fix "Sum Aggregation DESCRIBES" in comments
|
This is great! The factory design is very slick. Just a couple of comments and should be good to go! |
c24t
left a comment
There was a problem hiding this comment.
This is a huge improvement, great that you were able to remove aggregation.aggregation_data and aggregation.aggregation_type and get_metric_type.
Besides @lzchen's comments I'd ask that you also remove BaseAggregation, or at least move buckets down to the distribution class.
opencensus/stats/aggregation.py
Outdated
| """ | ||
| def __init__(self, buckets=None, aggregation_type=Type.NONE): | ||
| self._aggregation_type = aggregation_type | ||
| def __init__(self, buckets=None): |
There was a problem hiding this comment.
While you're in here: I don't see any reason for buckets to be on BaseAggregation, it looks like an odd choice from the first implementation (#149) that never got cleaned up. Since aggregation_type is gone now I recommend scrapping this whole class.
There was a problem hiding this comment.
Done, just removed the class.
opencensus/stats/aggregation_data.py
Outdated
|
|
||
|
|
||
| class SumAggregationDataFloat(BaseAggregationData): | ||
| class SumAggregationData(BaseAggregationData): |
There was a problem hiding this comment.
Why the decision to make value_type an instance attr instead of keeping separate aggregation classes for each value type here?
There was a problem hiding this comment.
Initially, I had this private class with value_type and then bound classes like:
SumAggregationDataFloat = functools.partial(SumAggregationData, value_type=value.ValueDouble)
SumAggregationDataInt = functools.partial(SumAggregationData, value_type=value.ValueInt)
But then in SumAggregation.new_aggregation_data, we needed an additional set of conditionals to check the type of the measure or metric and decide which to use. Instead doing it this way, we just need the one set of conditionals in .get_metric_type which we need anyway, and can reuse MetricDescriptor.to_type_class. It seemed a little cleaner, but really it isn't that different.
We could also duplicate the classes entirely, instead of the functools.partial, but that seemed unnecessary.
I guess the other option is to force the user to decide and provide Int or Float, but since we can determine it that seems easier and less error prone.
|
Also fixes [#679] |
opencensus/stats/aggregation_data.py
Outdated
| @@ -52,16 +52,21 @@ def to_point(self, timestamp): | |||
| raise NotImplementedError # pragma: NO COVER | |||
|
|
|||
|
|
|||
There was a problem hiding this comment.
The BaseAggregationData class seems a little odd to me as well, each implementation supplies aggregation_data upon construction but then doesn't update it, and stores there data in a separate instance variable that they update and return.
Is there a reason for this class to exist?
There was a problem hiding this comment.
Not that I can tell, besides to show that aggregations should implement to_point, but in that case there should be an unimplemented add_sample in the class too.
In any case it looks like you can lose the aggregation_data attr and property.
There was a problem hiding this comment.
I just removed BaseAggregationData entirely
| """Get the MetricDescriptorType for the metric produced by this | ||
| aggregation and measure. | ||
| """ | ||
| if isinstance(measure, measure_module.MeasureInt): |
There was a problem hiding this comment.
I don't have strong opinion here, it seems a more "Pythonic" style is:
if isinstance(measure, measure_module.MeasureInt):
return MetricDescriptorType.CUMULATIVE_INT64
if isinstance(measure, measure_module.MeasureFloat):
return MetricDescriptorType.CUMULATIVE_DOUBLE
raise ValueErrorUp to you.
There was a problem hiding this comment.
Good catch, surprised the linter didn't yell about this (or maybe it was a warning I didn't see 😁)
c24t
left a comment
There was a problem hiding this comment.
LGTM after removing the attr from BaseAggregationData (or the whole class). Please also add a note to the changelog.
reyang
left a comment
There was a problem hiding this comment.
This is a big improvement, thank you @colincadams!
|
Thanks all! And thanks for updating the changelog @c24t, totally forgot! |
This makes Aggregation a factory of AggregationData, using the type of the underlying measure to determine which value type to use.
fixes #565
Currently just a first pass, looking for feedback on the approach that we started discussing in #565.