WorkspaceStartAttemptsMeterBinder implements MeterBinder { @Override public void bindTo(MeterRegistry registry) { startingCounter = Counter.builder(workspaceMetric("starting_attempts.total")) .tags(withStandardTags("debug", "false")) .description("The count of workspaces start attempts") .register(registry); startingDebugCounter = Counter.builder(workspaceMetric("starting_attempts.total")) .tags(withStandardTags("debug", "true")) .description("The count of workspaces start attempts in debug mode") .register(registry); eventService.subscribe( event -> { if (event.getPrevStatus() == WorkspaceStatus.STOPPED && event.getStatus() == WorkspaceStatus.STARTING) { if (event.getOptions() != null && Boolean.parseBoolean( event.getOptions().getOrDefault(DEBUG_WORKSPACE_START, FALSE.toString()))) { startingDebugCounter.increment(); } else { startingCounter.increment(); } } }, WorkspaceStatusEvent.class); } @Inject  WorkspaceStartAttemptsMeterBinder(EventService eventService); @Override void bindTo(MeterRegistry registry);  }
@Test(dataProvider = "allStatusTransitionsWithoutStarting") public void shouldNotCollectEvents(WorkspaceStatus from, WorkspaceStatus to) { WorkspaceStartAttemptsMeterBinder meterBinder = new WorkspaceStartAttemptsMeterBinder(eventService); meterBinder.bindTo(registry); eventService.publish( DtoFactory.newDto(WorkspaceStatusEvent.class) .withPrevStatus(from) .withStatus(to) .withWorkspaceId("id1")); Counter successful = registry.find("che.workspace.starting_attempts.total").counter(); Assert.assertEquals(successful.count(), 0.0); }
@Test public void shouldCollectOnlyStart() { WorkspaceStartAttemptsMeterBinder meterBinder = new WorkspaceStartAttemptsMeterBinder(eventService); meterBinder.bindTo(registry); eventService.publish( DtoFactory.newDto(WorkspaceStatusEvent.class) .withPrevStatus(WorkspaceStatus.STOPPED) .withStatus(WorkspaceStatus.STARTING) .withWorkspaceId("id1")); Counter successful = registry.find("che.workspace.starting_attempts.total").tag("debug", "false").counter(); Assert.assertEquals(successful.count(), 1.0); Counter debug = registry.find("che.workspace.starting_attempts.total").tag("debug", "true").counter(); Assert.assertEquals(debug.count(), 0.0); }
@Test public void shouldCollectOnlyDebugStart() { WorkspaceStartAttemptsMeterBinder meterBinder = new WorkspaceStartAttemptsMeterBinder(eventService); meterBinder.bindTo(registry); eventService.publish( DtoFactory.newDto(WorkspaceStatusEvent.class) .withPrevStatus(WorkspaceStatus.STOPPED) .withStatus(WorkspaceStatus.STARTING) .withWorkspaceId("id1") .withOptions(singletonMap(Constants.DEBUG_WORKSPACE_START, Boolean.TRUE.toString()))); Counter successful = registry.find("che.workspace.starting_attempts.total").tag("debug", "false").counter(); Assert.assertEquals(successful.count(), 0.0); Counter debug = registry.find("che.workspace.starting_attempts.total").tag("debug", "true").counter(); Assert.assertEquals(debug.count(), 1.0); }
@Test public void shouldNotCollectDebugStartWhenSetToFalse() { WorkspaceStartAttemptsMeterBinder meterBinder = new WorkspaceStartAttemptsMeterBinder(eventService); meterBinder.bindTo(registry); eventService.publish( DtoFactory.newDto(WorkspaceStatusEvent.class) .withPrevStatus(WorkspaceStatus.STOPPED) .withStatus(WorkspaceStatus.STARTING) .withWorkspaceId("id1") .withOptions(singletonMap(Constants.DEBUG_WORKSPACE_START, Boolean.FALSE.toString()))); Counter successful = registry.find("che.workspace.starting_attempts.total").tag("debug", "false").counter(); Assert.assertEquals(successful.count(), 1.0); Counter debug = registry.find("che.workspace.starting_attempts.total").tag("debug", "true").counter(); Assert.assertEquals(debug.count(), 0.0); }
@Test public void shouldNotCollectDebugStartWhenSetToStrangeString() { WorkspaceStartAttemptsMeterBinder meterBinder = new WorkspaceStartAttemptsMeterBinder(eventService); meterBinder.bindTo(registry); eventService.publish( DtoFactory.newDto(WorkspaceStatusEvent.class) .withPrevStatus(WorkspaceStatus.STOPPED) .withStatus(WorkspaceStatus.STARTING) .withWorkspaceId("id1") .withOptions(singletonMap(Constants.DEBUG_WORKSPACE_START, "strange"))); Counter successful = registry.find("che.workspace.starting_attempts.total").tag("debug", "false").counter(); Assert.assertEquals(successful.count(), 1.0); Counter debug = registry.find("che.workspace.starting_attempts.total").tag("debug", "true").counter(); Assert.assertEquals(debug.count(), 0.0); }
@Test public void shouldNotCollectDebugStartWhenNotSetInOptions() { WorkspaceStartAttemptsMeterBinder meterBinder = new WorkspaceStartAttemptsMeterBinder(eventService); meterBinder.bindTo(registry); eventService.publish( DtoFactory.newDto(WorkspaceStatusEvent.class) .withPrevStatus(WorkspaceStatus.STOPPED) .withStatus(WorkspaceStatus.STARTING) .withWorkspaceId("id1") .withOptions(singletonMap("bla", "bol"))); Counter successful = registry.find("che.workspace.starting_attempts.total").tag("debug", "false").counter(); Assert.assertEquals(successful.count(), 1.0); Counter debug = registry.find("che.workspace.starting_attempts.total").tag("debug", "true").counter(); Assert.assertEquals(debug.count(), 0.0); }