private Exception represent(ServerHealthState serverHealthState) { String message = new StringBuilder() .append(serverHealthState.getMessage().toUpperCase()) .append("\n") .append(serverHealthState.getDescription()) .toString(); return new Exception(message); }
public static void toJSON(OutputListWriter outputListWriter, Collection<ServerHealthState> healthStates) { healthStates.forEach(healthState -> outputListWriter.addChild(writer -> writer.add("message", healthState.getMessage()) .add("detail", healthState.getDescription()) .add("level", healthState.getLogLevel().toString()) .add("time", healthState.getTimestamp())) ); } }
public ServerHealthState warning(String message, String description, HealthStateType type) { ServerHealthState state = ServerHealthState.warning(message, description, type); serverHealthService.update(state); canContinue = false; return state; }
public HealthStateType update(ServerHealthState serverHealthState) { HealthStateType type = serverHealthState.getType(); if (serverHealthState.getLogLevel() == HealthStateLevel.OK) { if (serverHealth.containsKey(type)) { serverHealth.remove(type); } return null; } else { serverHealth.put(type, serverHealthState); return type; } }
public void onConfigChange(CruiseConfig newCruiseConfig) { ServerHealthState serverHealthState; if (isArtifactsDirChanged(newCruiseConfig)) { serverHealthState = ServerHealthState.warning(ARTIFACTS_ROOT_CHANGED_MESSAGE, ARTIFACTS_ROOT_CHANGED_DESC, ARTIFACTS_ROOT_CHANGE_HEALTH_STATE_TYPE); LOGGER.info("[Configuration Changed] Artifacts directory was changed."); } else { serverHealthState = ServerHealthState.success(ARTIFACTS_ROOT_CHANGE_HEALTH_STATE_TYPE); } serverHealthService.update(serverHealthState); }
@Test public void shouldEscapeWarningMessageAndDescriptionByDefault() { ServerHealthState warningStateWithoutTimeout = ServerHealthState.warning("\"<message1 & message2>\"", "\"<message1 & message2>\"", HealthStateType.general(HealthStateScope.forPipeline("foo"))); ServerHealthState warningStateWithTimeout = ServerHealthState.warning("\"<message1 & message2>\"", "\"<message1 & message2>\"", HealthStateType.general(HealthStateScope.forPipeline("foo")), Timeout.TEN_SECONDS); ServerHealthState warningState = ServerHealthState.warning("\"<message1 & message2>\"", "\"<message1 & message2>\"", HealthStateType.general(HealthStateScope.forPipeline("foo")), 15L); assertThat(warningStateWithoutTimeout.getMessage(), is(""<message1 & message2>"")); assertThat(warningStateWithoutTimeout.getDescription(), is(""<message1 & message2>"")); assertThat(warningStateWithTimeout.getMessage(), is("\"<message1 & message2>\"")); assertThat(warningStateWithTimeout.getDescription(), is("\"<message1 & message2>\"")); assertThat(warningState.getMessage(), is(""<message1 & message2>"")); assertThat(warningState.getDescription(), is(""<message1 & message2>"")); }
@Test public void shouldAddWarningWhenConfigRepoLooseObjectCountGoesBeyondTheConfiguredThreshold() throws Exception { when(systemEnvironment.get(SystemEnvironment.GO_CONFIG_REPO_GC_LOOSE_OBJECT_WARNING_THRESHOLD)).thenReturn(10L); when(configRepository.getLooseObjectCount()).thenReturn(20L); service.checkRepoAndAddWarningIfRequired(); List<ServerHealthState> healthStates = serverHealthService.filterByScope(HealthStateScope.forConfigRepo("GC")); String message = "Action required: Run 'git gc' on config.git repo"; String description = "Number of loose objects in your Configuration repository(config.git) has grown beyond " + "the configured threshold. As the size of config repo increases, the config save operations tend to slow down " + "drastically. It is recommended that you run 'git gc' from " + "'<go server installation directory>/db/config.git/' to address this problem. Go can do this " + "automatically on a periodic basis if you enable automatic GC. <a target='_blank' href='" + docsUrl("/advanced_usage/config_repo.html") + "'>read more...</a>"; assertThat(healthStates.get(0).getDescription(), is(description)); assertThat(healthStates.get(0).getLogLevel(), is(HealthStateLevel.WARNING)); assertThat(healthStates.get(0).getMessage(), is(message)); }
public ServerHealthState error(String message, String description, HealthStateType type) { ServerHealthState state = ServerHealthState.error(message, description, type); serverHealthService.update(state); canContinue = false; return state; }
@Test public void shouldPreserverHtmlInWarningMessageAndDescription() { ServerHealthState warningState = ServerHealthState.warningWithHtml("\"<message1 & message2>\"", "\"<message1 & message2>\"", HealthStateType.general(HealthStateScope.forPipeline("foo"))); ServerHealthState warningStateWithTime = ServerHealthState.warningWithHtml("\"<message1 & message2>\"", "\"<message1 & message2>\"", HealthStateType.general(HealthStateScope.forPipeline("foo")), 15L); assertThat(warningState.getMessage(), is("\"<message1 & message2>\"")); assertThat(warningState.getDescription(), is("\"<message1 & message2>\"")); assertThat(warningStateWithTime.getMessage(), is("\"<message1 & message2>\"")); assertThat(warningStateWithTime.getDescription(), is("\"<message1 & message2>\"")); } }
@Test public void shouldEscapeErrorMessageAndDescriptionByDefault() { ServerHealthState errorState = ServerHealthState.error("\"<message1 & message2>\"", "\"<message1 & message2>\"", HealthStateType.general(HealthStateScope.forPipeline("foo"))); assertThat(errorState.getMessage(), is(""<message1 & message2>"")); assertThat(errorState.getDescription(), is(""<message1 & message2>"")); }
public boolean matchesSafely(ServerHealthService item) { allLogs = item.logs(); for (ServerHealthState serverHealthState : allLogs) { if (serverHealthState.getType().equals(healthStateType)) { entry = serverHealthState; } } if (!(entry != null)) { return false; } else { levelMatches = healthStateLevel == null || healthStateLevel.equals(entry.getLogLevel()); messageMatches = message == null || message.equals(entry.getMessage()); return levelMatches && messageMatches; } }
public static JsonAction from(ServerHealthState serverHealthState) { if (serverHealthState.isSuccess()) { return jsonCreated(new LinkedHashMap()); } Map<String, Object> jsonLog = new LinkedHashMap<>(); jsonLog.put(ERROR_FOR_JSON, serverHealthState.getDescription()); return new JsonAction(serverHealthState.getType().getHttpCode(), jsonLog); }
@Test public void stateRelatedPipelineNames() { HgMaterial hgMaterial = MaterialsMother.hgMaterial(); CruiseConfig config = new BasicCruiseConfig(); config.addPipeline("group", PipelineConfigMother.pipelineConfig(PIPELINE_NAME, new MaterialConfigs(hgMaterial.config()))); config.addPipeline("group", PipelineConfigMother.pipelineConfig("pipeline2", new MaterialConfigs(hgMaterial.config()))); config.addPipeline("group", PipelineConfigMother.pipelineConfig("pipeline3")); serverHealthService = new ServerHealthService(); serverHealthService.update(ServerHealthState.error("message", "description", HealthStateType.general(forPipeline(PIPELINE_NAME)))); assertThat((serverHealthService.logs().get(0)).getPipelineNames(config), equalTo(Collections.singleton(PIPELINE_NAME))); serverHealthService = new ServerHealthService(); serverHealthService.update(ServerHealthState.error("message", "description", HealthStateType.general(forStage(PIPELINE_NAME, "stage1")))); assertThat((serverHealthService.logs().get(0)).getPipelineNames(config), equalTo(Collections.singleton(PIPELINE_NAME))); serverHealthService = new ServerHealthService(); serverHealthService.update(ServerHealthState.error("message", "description", HealthStateType.general(forJob(PIPELINE_NAME, "stage1", "job1")))); assertThat((serverHealthService.logs().get(0)).getPipelineNames(config), equalTo(Collections.singleton(PIPELINE_NAME))); }
public void describeTo(Description description) { if (entry == null) { description.appendText("Can not find result with " + healthStateType + " in all logs: " + allLogs); } else { if (!levelMatches) { description.appendText("Level was " + entry.getLogLevel() + " instead of " + healthStateLevel); } if (!messageMatches) { description.appendText("Message was: \n" + entry.getMessage() + "\n instead of:\n" + message); } } } };
private void replaceServerHealthMessages(String errorMessageTitle, HealthStateScope scope, List<PluginHealthMessage> pluginHealthMessages) { serverHealthService.removeByScope(scope); for (int index = 0; index < pluginHealthMessages.size(); index++) { PluginHealthMessage pluginHealthMessage = pluginHealthMessages.get(index); ServerHealthState state; if (pluginHealthMessage.isWarning()) { state = ServerHealthState.warning(errorMessageTitle, pluginHealthMessage.message(), HealthStateType.withSubkey(scope, "message_" + index)); } else { state = ServerHealthState.error(errorMessageTitle, pluginHealthMessage.message(), HealthStateType.withSubkey(scope, "message_" + index)); } serverHealthService.update(state); } } }
public ServerHealthState success(HealthStateType healthStateType) { ServerHealthState state = ServerHealthState.success(healthStateType); serverHealthService.update(state); return state; }
public String fullMessage() { ServerHealthState serverHealthState = serverHealthStateOperationResult.getServerHealthState(); String desc = serverHealthState == null ? BLANK_STRING : serverHealthState.getDescription(); return StringUtils.isBlank(desc) ? message : String.format("%s { %s }", message, desc); }
public String getMessageWithTimestamp() { return getMessage() + " [" + TIMESTAMP_FORMAT.format(timestamp) + "]"; }
@Test public void shouldGetMessageWithTimestamp() { ServerHealthState errorState = ServerHealthState.error("my message", "my description", HealthStateType.general(HealthStateScope.forPipeline("foo"))); assertThat(errorState.getMessageWithTimestamp(), is("my message" + " [" + ServerHealthState.TIMESTAMP_FORMAT.format(errorState.getTimestamp()) + "]")); }
public int getHttpCode() { return getType().getHttpCode(); }