MeterService implements BootService, Runnable { public <T extends BaseMeter> T register(T meter) { if (meter == null) { return null; } if (meterMap.size() >= Config.Meter.MAX_METER_SIZE) { LOGGER.warn( "Already out of the meter system max size, will not report. meter name:{}", meter.getName()); return meter; } final BaseMeter data = meterMap.putIfAbsent(meter.getId(), meter); return data == null ? meter : (T) data; }  T register(T meter); @Override void prepare(); @Override void boot(); @Override void onComplete(); @Override void shutdown(); @Override void run();  }
@Test public void testRegister() { grpcServerRule.getServiceRegistry().addService(serviceImplBase); registryService.register(null); assertThat(upstreamMeters.size(), is(0)); registryService.run(); assertThat(upstreamMeters.size(), is(0)); final MeterId counterId = new MeterId("test1", MeterType.COUNTER, Arrays.asList(new MeterTag("k1", "v1"))); final Counter counter = new Counter(counterId, CounterMode.INCREMENT); counter.increment(2); registryService.register(counter); registryService.run(); assertThat(upstreamMeters.size(), is(1)); isSameWithCounter(upstreamMeters.get(0), true, counterId, 2); upstreamMeters.clear(); final MeterId percentileId = new MeterId("test2", MeterType.HISTOGRAM, Arrays.asList(new MeterTag("k1", "v1"))); final Histogram histogram = new Histogram(percentileId, Arrays.asList(2d)); registryService.register(histogram); histogram.addValue(3); registryService.run(); assertThat(upstreamMeters.size(), is(2)); for (int i = 0; i < upstreamMeters.size(); i++) { if (Objects.equals(upstreamMeters.get(i).getMetricCase(), MeterData.MetricCase.HISTOGRAM)) { isSameWithHistogram(upstreamMeters.get(i), i == 0, percentileId, 2d, 1L); } else { isSameWithCounter(upstreamMeters.get(i), i == 0, counterId, 2); } } }