PublicDimensionsProcessor implements MetricsProcessor { @Override public void process(MetricsPacket.Builder builder) { Set<DimensionId> dimensionsToRetain = builder.getDimensionIds(); dimensionsToRetain.retainAll(publicDimensions); if (dimensionsToRetain.size() > maxDimensions) { for (var metricDim : getMetricDimensions()) { dimensionsToRetain.remove(metricDim); if (dimensionsToRetain.size() <= maxDimensions) break; } } builder.retainDimensions(dimensionsToRetain); if (builder.getDimensionIds().size() > maxDimensions) { throw new IllegalStateException(String.format( "Metrics packet is only allowed to have %d dimensions, but has: %s", maxDimensions, builder.getDimensionIds())); } } PublicDimensionsProcessor(int maxDimensions); @Override void process(MetricsPacket.Builder builder);  }
@Test public void non_public_dimensions_are_removed() { var builder = new MetricsPacket.Builder(toServiceId("foo")) .putDimension(toDimensionId("a"), ""); var processor = new PublicDimensionsProcessor(10); processor.process(builder); assertTrue(builder.getDimensionIds().isEmpty()); }
@Test public void public_dimensions_are_retained() { var builder = new MetricsPacket.Builder(toServiceId("foo")) .putDimension(toDimensionId(APPLICATION_ID), "app"); var processor = new PublicDimensionsProcessor(10); processor.process(builder); assertEquals(1, builder.getDimensionIds().size()); assertEquals(toDimensionId(APPLICATION_ID), builder.getDimensionIds().iterator().next()); }