InternalDistributedSystemMetricsService implements MetricsService { @Override public void start() { persistentMeterRegistries.forEach(meterRegistry::add); binder.bindTo(meterRegistry); publishingServices.addAll(publishingServiceLoader.loadServices(MetricsPublishingService.class)); publishingServices.forEach(this::startMetricsPublishingService); } @VisibleForTesting  InternalDistributedSystemMetricsService(MetricsService.Builder builder, Logger logger,
      CollectingServiceLoader<MetricsPublishingService> publishingServiceLoader,
      CompositeMeterRegistry metricsServiceMeterRegistry,
      Collection<MeterRegistry> persistentMeterRegistries, CloseableMeterBinder binder,
      InternalDistributedSystem system, boolean isClient, boolean hasLocator,
      boolean hasCacheServer); @Override void start(); @Override MeterRegistry getMeterRegistry(); @Override MetricsService.Builder getRebuilder(); @Override void stop(); @Override void addSubregistry(MeterRegistry subregistry); @Override void removeSubregistry(MeterRegistry subregistry);  }
@Test public void start_addsPersistentMeterRegistriesToMetricsServiceMeterRegistry() { Set<MeterRegistry> thePersistentMeterRegistries = setOf(3, MeterRegistry.class); metricsService = new InternalDistributedSystemMetricsService(metricsServiceBuilder, logger, publishingServiceLoader, metricsServiceMeterRegistry, thePersistentMeterRegistries, meterBinder, system, false, false, true); metricsService.start(); assertThat(metricsServiceMeterRegistry.getRegistries()) .hasSameElementsAs(thePersistentMeterRegistries); }
@Test public void start_bindsMeterBinderToMetricsServiceMeterRegistry() { CompositeMeterRegistry theMetricsServiceMeterRegistry = new CompositeMeterRegistry(); metricsService = new InternalDistributedSystemMetricsService(metricsServiceBuilder, logger, publishingServiceLoader, theMetricsServiceMeterRegistry, emptyList(), meterBinder, system, false, false, true); metricsService.start(); verify(meterBinder).bindTo(same(theMetricsServiceMeterRegistry)); }
@Test public void start_startsEachPublishingServiceLoadedByLoader() { Collection<MetricsPublishingService> publishingServices = setOf(4, MetricsPublishingService.class); @SuppressWarnings("unchecked") CollectingServiceLoader<MetricsPublishingService> serviceLoader = mock(CollectingServiceLoader.class); when(serviceLoader.loadServices(MetricsPublishingService.class)) .thenReturn(publishingServices); metricsService = new InternalDistributedSystemMetricsService(metricsServiceBuilder, logger, serviceLoader, metricsServiceMeterRegistry, emptyList(), meterBinder, system, false, false, true); metricsService.start(); publishingServices.forEach( publishingService -> verify(publishingService, times(1)).start(same(metricsService))); }
@Test public void start_logsError_ifMetricsPublishingServiceStartThrows() { MetricsPublishingService throwingService = mock(MetricsPublishingService.class); RuntimeException thrownDuringStart = new RuntimeException("thrown by service.start() during test"); doThrow(thrownDuringStart).when(throwingService).start(any()); String serviceClassName = throwingService.getClass().getName(); @SuppressWarnings("unchecked") CollectingServiceLoader<MetricsPublishingService> serviceLoader = mock(CollectingServiceLoader.class); when(serviceLoader.loadServices(MetricsPublishingService.class)) .thenReturn(singleton(throwingService)); metricsService = new InternalDistributedSystemMetricsService(metricsServiceBuilder, logger, serviceLoader, metricsServiceMeterRegistry, emptyList(), meterBinder, system, false, false, true); metricsService.start(); ArgumentCaptor<String> actualMessage = ArgumentCaptor.forClass(String.class); verify(logger).error(actualMessage.capture(), same(thrownDuringStart)); assertThat(actualMessage.getValue()) .as("Error log message") .contains(serviceClassName); }