Repository: beam Updated Branches: refs/heads/master a267ca89e -> 59542c3cc
Add Nullable getters to MetricsContainerImpl Project: http://git-wip-us.apache.org/repos/asf/beam/repo Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/8a25597e Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/8a25597e Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/8a25597e Branch: refs/heads/master Commit: 8a25597eb5da41f2174dfcf4cea8d0fc230603e4 Parents: a267ca8 Author: Sunil Pedapudi <skpedap...@gmail.com> Authored: Tue Sep 19 14:55:43 2017 -0700 Committer: Thomas Groh <tg...@google.com> Committed: Wed Sep 20 13:33:20 2017 -0700 ---------------------------------------------------------------------- .../core/metrics/MetricsContainerImpl.java | 40 ++++++++++++++++++++ .../core/metrics/MetricsContainerImplTest.java | 10 +++++ 2 files changed, 50 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/beam/blob/8a25597e/runners/core-java/src/main/java/org/apache/beam/runners/core/metrics/MetricsContainerImpl.java ---------------------------------------------------------------------- diff --git a/runners/core-java/src/main/java/org/apache/beam/runners/core/metrics/MetricsContainerImpl.java b/runners/core-java/src/main/java/org/apache/beam/runners/core/metrics/MetricsContainerImpl.java index 4b331e0..1d5ad72 100644 --- a/runners/core-java/src/main/java/org/apache/beam/runners/core/metrics/MetricsContainerImpl.java +++ b/runners/core-java/src/main/java/org/apache/beam/runners/core/metrics/MetricsContainerImpl.java @@ -23,6 +23,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.collect.ImmutableList; import java.io.Serializable; import java.util.Map; +import javax.annotation.Nullable; import org.apache.beam.runners.core.construction.metrics.MetricKey; import org.apache.beam.runners.core.metrics.MetricUpdates.MetricUpdate; import org.apache.beam.sdk.annotations.Experimental; @@ -80,21 +81,60 @@ public class MetricsContainerImpl implements Serializable, MetricsContainer { this.stepName = stepName; } + /** + * Return a {@code CounterCell} named {@code metricName}. If it doesn't exist, create a + * {@code Metric} with the specified name. + */ @Override public CounterCell getCounter(MetricName metricName) { return counters.get(metricName); } + /** + * Return a {@code CounterCell} named {@code metricName}. If it doesn't exist, return + * {@code null}. + */ + @Nullable + public CounterCell tryGetCounter(MetricName metricName) { + return counters.tryGet(metricName); + } + + /** + * Return a {@code DistributionCell} named {@code metricName}. If it doesn't exist, create a + * {@code Metric} with the specified name. + */ @Override public DistributionCell getDistribution(MetricName metricName) { return distributions.get(metricName); } + /** + * Return a {@code DistributionCell} named {@code metricName}. If it doesn't exist, return + * {@code null}. + */ + @Nullable + public DistributionCell tryGetDistribution(MetricName metricName) { + return distributions.tryGet(metricName); + } + + /** + * Return a {@code GaugeCell} named {@code metricName}. If it doesn't exist, create a + * {@code Metric} with the specified name. + */ @Override public GaugeCell getGauge(MetricName metricName) { return gauges.get(metricName); } + /** + * Return a {@code GaugeCell} named {@code metricName}. If it doesn't exist, return + * {@code null}. + */ + @Nullable + public GaugeCell tryGetGauge(MetricName metricName) { + return gauges.tryGet(metricName); + } + private <UpdateT, CellT extends MetricCell<UpdateT>> ImmutableList<MetricUpdate<UpdateT>> extractUpdates(MetricsMap<MetricName, CellT> cells) { ImmutableList.Builder<MetricUpdate<UpdateT>> updates = ImmutableList.builder(); http://git-wip-us.apache.org/repos/asf/beam/blob/8a25597e/runners/core-java/src/test/java/org/apache/beam/runners/core/metrics/MetricsContainerImplTest.java ---------------------------------------------------------------------- diff --git a/runners/core-java/src/test/java/org/apache/beam/runners/core/metrics/MetricsContainerImplTest.java b/runners/core-java/src/test/java/org/apache/beam/runners/core/metrics/MetricsContainerImplTest.java index b304d3b..ab4b709 100644 --- a/runners/core-java/src/test/java/org/apache/beam/runners/core/metrics/MetricsContainerImplTest.java +++ b/runners/core-java/src/test/java/org/apache/beam/runners/core/metrics/MetricsContainerImplTest.java @@ -22,6 +22,7 @@ import static org.apache.beam.runners.core.metrics.MetricUpdateMatchers.metricUp import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.emptyIterable; import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import org.apache.beam.sdk.metrics.MetricName; @@ -67,6 +68,9 @@ public class MetricsContainerImplTest { c1.inc(8L); assertThat(container.getUpdates().counterUpdates(), contains( metricUpdate("name1", 13L))); + + CounterCell dne = container.tryGetCounter(MetricName.named("ns", "dne")); + assertEquals(dne, null); } @Test @@ -89,6 +93,9 @@ public class MetricsContainerImplTest { assertThat(container.getCumulative().counterUpdates(), containsInAnyOrder( metricUpdate("name1", 13L), metricUpdate("name2", 4L))); + + CounterCell readC1 = container.tryGetCounter(MetricName.named("ns", "name1")); + assertEquals((long) readC1.getCumulative(), 13L); } @Test @@ -126,5 +133,8 @@ public class MetricsContainerImplTest { assertThat(container.getUpdates().distributionUpdates(), contains( metricUpdate("name1", DistributionData.create(17, 3, 4, 8)))); container.commitUpdates(); + + DistributionCell dne = container.tryGetDistribution(MetricName.named("ns", "dne")); + assertEquals(dne, null); } }