WorkspaceSuccessfulStopAttemptsMeterBinder implements MeterBinder { @Override public void bindTo(MeterRegistry registry) { stoppedCounter = Counter.builder(workspaceMetric("stopped.total")) .description("The count of stopped workspaces") .register(registry); eventService.subscribe( event -> { if ((event.getError() == null) && (event.getPrevStatus() == WorkspaceStatus.STOPPING && event.getStatus() == WorkspaceStatus.STOPPED)) { stoppedCounter.increment(); } }, WorkspaceStatusEvent.class); } @Inject  WorkspaceSuccessfulStopAttemptsMeterBinder(EventService eventService); @Override void bindTo(MeterRegistry registry);  }
@Test(dataProvider = "allStatusTransitionsWithoutStopping") public void shouldNotCollectEvents(WorkspaceStatus from, WorkspaceStatus to) { WorkspaceSuccessfulStopAttemptsMeterBinder meterBinder = new WorkspaceSuccessfulStopAttemptsMeterBinder(eventService); meterBinder.bindTo(registry); eventService.publish( DtoFactory.newDto(WorkspaceStatusEvent.class) .withPrevStatus(from) .withStatus(to) .withWorkspaceId("id1")); Counter successful = registry.find("che.workspace.stopped.total").counter(); Assert.assertEquals(successful.count(), 0.0); }
@Test public void shouldCollectOnlyStoppedWithoutError() { WorkspaceSuccessfulStopAttemptsMeterBinder meterBinder = new WorkspaceSuccessfulStopAttemptsMeterBinder(eventService); meterBinder.bindTo(registry); eventService.publish( DtoFactory.newDto(WorkspaceStatusEvent.class) .withPrevStatus(WorkspaceStatus.STOPPING) .withStatus(WorkspaceStatus.STOPPED) .withWorkspaceId("id1")); Counter successful = registry.find("che.workspace.stopped.total").counter(); Assert.assertEquals(successful.count(), 1.0); }
@Test public void shouldNotCollectStoppedWithError() { WorkspaceSuccessfulStopAttemptsMeterBinder meterBinder = new WorkspaceSuccessfulStopAttemptsMeterBinder(eventService); meterBinder.bindTo(registry); eventService.publish( DtoFactory.newDto(WorkspaceStatusEvent.class) .withPrevStatus(WorkspaceStatus.STOPPING) .withStatus(WorkspaceStatus.STOPPED) .withError("Error during workspace stop") .withWorkspaceId("id1")); Counter successful = registry.find("che.workspace.stopped.total").counter(); Assert.assertEquals(successful.count(), 0.0); }