diff options
Diffstat (limited to 'impl_core/src/main/java/io/opencensus/implcore/stats/MutableViewData.java')
-rw-r--r-- | impl_core/src/main/java/io/opencensus/implcore/stats/MutableViewData.java | 74 |
1 files changed, 60 insertions, 14 deletions
diff --git a/impl_core/src/main/java/io/opencensus/implcore/stats/MutableViewData.java b/impl_core/src/main/java/io/opencensus/implcore/stats/MutableViewData.java index 0664681c..71c92a82 100644 --- a/impl_core/src/main/java/io/opencensus/implcore/stats/MutableViewData.java +++ b/impl_core/src/main/java/io/opencensus/implcore/stats/MutableViewData.java @@ -31,7 +31,15 @@ import io.opencensus.common.Function; import io.opencensus.common.Functions; import io.opencensus.common.Timestamp; import io.opencensus.implcore.internal.CheckerFrameworkUtils; +import io.opencensus.metrics.LabelValue; +import io.opencensus.metrics.Metric; import io.opencensus.metrics.MetricDescriptor; +import io.opencensus.metrics.MetricDescriptor.Type; +import io.opencensus.metrics.Point; +import io.opencensus.metrics.TimeSeriesCumulative; +import io.opencensus.metrics.TimeSeriesGauge; +import io.opencensus.metrics.TimeSeriesList.TimeSeriesCumulativeList; +import io.opencensus.metrics.TimeSeriesList.TimeSeriesGaugeList; import io.opencensus.stats.Aggregation; import io.opencensus.stats.AggregationData; import io.opencensus.stats.StatsCollectionState; @@ -39,6 +47,7 @@ import io.opencensus.stats.View; import io.opencensus.stats.ViewData; import io.opencensus.tags.TagContext; import io.opencensus.tags.TagValue; +import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.List; @@ -66,13 +75,12 @@ abstract class MutableViewData { * * @param view the {@code View} linked with this {@code MutableViewData}. * @param start the start {@code Timestamp}. - * @param metricMap a reference to {@code MetricMap}. * @return a {@code MutableViewData}. */ - static MutableViewData create(final View view, final Timestamp start, final MetricMap metricMap) { + static MutableViewData create(final View view, final Timestamp start) { return view.getWindow() .match( - new CreateCumulative(view, start, metricMap), + new CreateCumulative(view, start), new CreateInterval(view, start), Functions.<MutableViewData>throwAssertionError()); } @@ -82,6 +90,9 @@ abstract class MutableViewData { return view; } + @javax.annotation.Nullable + abstract Metric toMetric(Timestamp now, StatsCollectionState state); + /** Record stats with the given tags. */ abstract void record( TagContext context, double value, Timestamp timestamp, Map<String, String> attachments); @@ -101,15 +112,12 @@ abstract class MutableViewData { private Timestamp start; private final Map<List</*@Nullable*/ TagValue>, MutableAggregation> tagValueAggregationMap = Maps.newHashMap(); - private final MetricMap metricMap; - // Cache a MetricDescriptor to avoid converting View to MetricDescriptor in the future. private final MetricDescriptor metricDescriptor; - private CumulativeMutableViewData(View view, Timestamp start, MetricMap metricMap) { + private CumulativeMutableViewData(View view, Timestamp start) { super(view); this.start = start; - this.metricMap = metricMap; MetricDescriptor metricDescriptor = MetricUtils.viewToMetricDescriptor(view); if (metricDescriptor == null) { throw new AssertionError( @@ -119,6 +127,42 @@ abstract class MutableViewData { } } + @javax.annotation.Nullable + @Override + Metric toMetric(Timestamp now, StatsCollectionState state) { + if (state == StatsCollectionState.DISABLED) { + return null; + } + // TODO(bdrutu): Refactor this after TimeSeriesGauge and TimeSeriesCumulative are combined. + Type type = metricDescriptor.getType(); + if (type == Type.GAUGE_INT64 || type == Type.GAUGE_DOUBLE) { + List<TimeSeriesGauge> timeSeriesGauges = new ArrayList<TimeSeriesGauge>(); + for (Entry<List</*@Nullable*/ TagValue>, MutableAggregation> entry : + tagValueAggregationMap.entrySet()) { + List<LabelValue> labelValues = MetricUtils.tagValuesToLabelValues(entry.getKey()); + Point point = MetricUtils.mutableAggregationToPoint(entry.getValue(), now, type); + timeSeriesGauges.add( + TimeSeriesGauge.create(labelValues, Collections.singletonList(point))); + } + return Metric.create(metricDescriptor, TimeSeriesGaugeList.create(timeSeriesGauges)); + } else { + List<TimeSeriesCumulative> timeSeriesCumulatives = new ArrayList<TimeSeriesCumulative>(); + for (Entry< + List< + /*@Nullable*/ + TagValue>, + MutableAggregation> + entry : tagValueAggregationMap.entrySet()) { + List<LabelValue> labelValues = MetricUtils.tagValuesToLabelValues(entry.getKey()); + Point point = MetricUtils.mutableAggregationToPoint(entry.getValue(), now, type); + timeSeriesCumulatives.add( + TimeSeriesCumulative.create(labelValues, Collections.singletonList(point), start)); + } + return Metric.create( + metricDescriptor, TimeSeriesCumulativeList.create(timeSeriesCumulatives)); + } + } + @Override void record( TagContext context, double value, Timestamp timestamp, Map<String, String> attachments) { @@ -128,9 +172,7 @@ abstract class MutableViewData { tagValueAggregationMap.put( tagValues, createMutableAggregation(super.view.getAggregation())); } - MutableAggregation mutableAggregation = tagValueAggregationMap.get(tagValues); - mutableAggregation.add(value, attachments, timestamp); - metricMap.record(metricDescriptor, tagValues, mutableAggregation, timestamp); + tagValueAggregationMap.get(tagValues).add(value, attachments, timestamp); } @Override @@ -216,6 +258,12 @@ abstract class MutableViewData { shiftBucketList(N + 1, start); } + @javax.annotation.Nullable + @Override + Metric toMetric(Timestamp now, StatsCollectionState state) { + return null; + } + @Override void record( TagContext context, double value, Timestamp timestamp, Map<String, String> attachments) { @@ -401,17 +449,15 @@ abstract class MutableViewData { implements Function<View.AggregationWindow.Cumulative, MutableViewData> { @Override public MutableViewData apply(View.AggregationWindow.Cumulative arg) { - return new CumulativeMutableViewData(view, start, metricMap); + return new CumulativeMutableViewData(view, start); } private final View view; private final Timestamp start; - private final MetricMap metricMap; - private CreateCumulative(View view, Timestamp start, MetricMap metricMap) { + private CreateCumulative(View view, Timestamp start) { this.view = view; this.start = start; - this.metricMap = metricMap; } } |