private static void stopLogProfiler(Profiler profiler, CeActivityDto.Status status) { profiler.addContext("status", status.name()); profiler.stopInfo("Executed task"); }
public void execute() { List<ProjectSensorWrapper> sensors = selector.selectSensors(); LOG.debug("Sensors : {}", sensors.stream() .map(Object::toString) .collect(Collectors.joining(" -> "))); for (ProjectSensorWrapper sensor : sensors) { String sensorName = getSensorName(sensor); profiler.startInfo("Sensor " + sensorName); sensor.analyse(); profiler.stopInfo(); } }
@Override public void execute(List<RegisteredMigrationStep> steps) { Profiler globalProfiler = Profiler.create(LOGGER); globalProfiler.startInfo(GLOBAL_START_MESSAGE); boolean allStepsExecuted = false; try { steps.forEach(this::execute); allStepsExecuted = true; } finally { if (allStepsExecuted) { globalProfiler.stopInfo(GLOBAL_END_MESSAGE, "success"); } else { globalProfiler.stopError(GLOBAL_END_MESSAGE, "failure"); } } }
private void execute(Collection<ModuleSensorWrapper> sensors) { for (ModuleSensorWrapper sensor : sensors) { String sensorName = getSensorName(sensor); profiler.startInfo("Sensor " + sensorName); sensor.analyse(); profiler.stopInfo(); } }
private void executeStep(Profiler stepProfiler, ComputationStep.Context context, ComputationStep step) { String status = "FAILED"; stepProfiler.start(); try { taskInterrupter.check(Thread.currentThread()); step.execute(context); status = "SUCCESS"; } finally { stepProfiler.addContext("status", status); stepProfiler.stopInfo(step.getDescription()); } }
@Test public void fail_if_stop_without_message() { underTest.start(); try { underTest.stopInfo(); fail(); } catch (IllegalStateException e) { assertThat(e).hasMessage("Profiler#stopXXX() can't be called without any message defined in start methods"); } }
@Test public void stopInfo_adds_context_after_time_by_default() { addSomeContext(underTest); underTest.start().stopInfo("Rules registered"); assertThat(tester.logs()).hasSize(1); assertThat(tester.logs(LoggerLevel.INFO).get(0)) .startsWith("Rules registered | time=") .endsWith("ms | a_string=bar | an_int=42 | after_start=true"); }
@Test public void stopInfo_clears_context() { addSomeContext(underTest); underTest.logTimeLast(true); underTest.start().stopInfo("Foo"); underTest.start().stopInfo("Bar"); assertThat(tester.logs()).hasSize(2); List<String> logs = tester.logs(LoggerLevel.INFO); assertThat(logs.get(0)) .startsWith("Foo | a_string=bar | an_int=42 | after_start=true | time=") .endsWith("ms"); assertThat(logs.get(1)) .startsWith("Bar | time=") .endsWith("ms"); }
private void execute(RegisteredMigrationStep step, MigrationStep migrationStep) { Profiler stepProfiler = Profiler.create(LOGGER); stepProfiler.startInfo(STEP_START_PATTERN, step); boolean done = false; try { migrationStep.execute(); migrationHistory.done(step); done = true; } catch (Exception e) { throw new MigrationStepExecutionException(step, e); } finally { if (done) { stepProfiler.stopInfo(STEP_STOP_PATTERN, step, "success"); } else { stepProfiler.stopError(STEP_STOP_PATTERN, step, "failure"); } } } }
@Test public void stopInfo_adds_context_before_time_if_logTimeLast_is_true() { addSomeContext(underTest); underTest.logTimeLast(true); underTest.start().stopInfo("Rules registered"); assertThat(tester.logs()).hasSize(1); assertThat(tester.logs(LoggerLevel.INFO).get(0)) .startsWith("Rules registered | a_string=bar | an_int=42 | after_start=true | time=") .endsWith("ms"); }
@Test public void empty_message() { underTest.addContext("foo", "bar"); underTest.startInfo(""); assertThat(tester.logs()).containsOnly("foo=bar"); underTest.addContext("after_start", true); underTest.stopInfo(""); assertThat(tester.logs()).hasSize(2); assertThat(tester.logs().get(1)) .startsWith("time=") .endsWith("ms | foo=bar | after_start=true"); }
@Test @UseDataProvider("logTimeLastValues") public void different_start_and_stop_messages(boolean logTimeLast) { underTest.logTimeLast(logTimeLast); tester.setLevel(LoggerLevel.TRACE); // start TRACE and stop DEBUG underTest.startTrace("Register rules"); underTest.stopDebug("Rules registered"); assertThat(tester.logs()).hasSize(2); assertThat(tester.logs().get(0)).contains("Register rules"); assertThat(tester.logs().get(1)).startsWith("Rules registered | time="); tester.clear(); // start DEBUG and stop INFO underTest.startDebug("Register rules {}", 10); underTest.stopInfo("Rules registered"); assertThat(tester.logs()).hasSize(2); assertThat(tester.logs().get(0)).contains("Register rules 10"); assertThat(tester.logs().get(1)).startsWith("Rules registered | time="); tester.clear(); // start INFO and stop TRACE underTest.startInfo("Register rules"); underTest.stopTrace("Rules registered"); assertThat(tester.logs()).hasSize(2); assertThat(tester.logs().get(0)).contains("Register rules"); assertThat(tester.logs().get(1)).startsWith("Rules registered | time="); }
private void doDatabaseMigration() { migrationState.setStatus(Status.RUNNING); migrationState.setStartedAt(new Date()); migrationState.setError(null); Profiler profiler = Profiler.create(LOGGER); try { profiler.startInfo("Starting DB Migration and container restart"); doUpgradeDb(); doRestartContainer(); migrationState.setStatus(Status.SUCCEEDED); profiler.stopInfo("DB Migration and container restart: success"); } catch (MigrationStepExecutionException e) { profiler.stopError("DB migration failed"); LOGGER.error("DB migration ended with an exception", e); saveStatus(e); } catch (Throwable t) { profiler.stopError("Container restart failed"); LOGGER.error("Container restart failed", t); saveStatus(t); } finally { semaphore.release(); } }
@Test @UseDataProvider("logTimeLastValues") public void log_on_at_stop(boolean logTimeLast) { underTest.logTimeLast(logTimeLast); tester.setLevel(LoggerLevel.TRACE); // trace underTest.start(); underTest.stopTrace("Rules registered"); assertThat(tester.logs()).hasSize(1); assertThat(tester.logs().get(0)).startsWith("Rules registered | time="); tester.clear(); // debug underTest.start(); underTest.stopDebug("Rules registered {} on {}", 6, 10); assertThat(tester.logs()).hasSize(1); assertThat(tester.logs().get(0)).startsWith("Rules registered 6 on 10 | time="); tester.clear(); // info underTest.start(); underTest.stopInfo("Rules registered"); assertThat(tester.logs()).hasSize(1); assertThat(tester.logs().get(0)).startsWith("Rules registered | time="); }
private static void stopActivityProfiler(Profiler profiler, CeTask task, CeActivityDto.Status status) { addContext(profiler, task); if (status == CeActivityDto.Status.FAILED) { profiler.stopError("Executed task"); } else { profiler.stopInfo("Executed task"); } }
public void execute() { List<ProjectSensorWrapper> sensors = selector.selectSensors(); LOG.debug("Sensors : {}", sensors.stream() .map(Object::toString) .collect(Collectors.joining(" -> "))); for (ProjectSensorWrapper sensor : sensors) { String sensorName = getSensorName(sensor); profiler.startInfo("Sensor " + sensorName); sensor.analyse(); profiler.stopInfo(); } }
@Override public void execute(Stream<RegisteredMigrationStep> steps) { Profiler globalProfiler = Profiler.create(LOGGER); globalProfiler.startInfo(GLOBAL_START_MESSAGE); boolean allStepsExecuted = false; try { steps.forEachOrdered(this::execute); allStepsExecuted = true; } finally { if (allStepsExecuted) { globalProfiler.stopInfo(GLOBAL_END_MESSAGE, "success"); } else { globalProfiler.stopError(GLOBAL_END_MESSAGE, "failure"); } } }
private void execute(Collection<ModuleSensorWrapper> sensors) { for (ModuleSensorWrapper sensor : sensors) { String sensorName = getSensorName(sensor); profiler.startInfo("Sensor " + sensorName); sensor.analyse(); profiler.stopInfo(); } }
private void execute(RegisteredMigrationStep step, MigrationStep migrationStep) { Profiler stepProfiler = Profiler.create(LOGGER); stepProfiler.startInfo(STEP_START_PATTERN, step); boolean done = false; try { migrationStep.execute(); migrationHistory.done(step); done = true; } catch (Exception e) { throw new MigrationStepExecutionException(step, e); } finally { if (done) { stepProfiler.stopInfo(STEP_STOP_PATTERN, step, "success"); } else { stepProfiler.stopError(STEP_STOP_PATTERN, step, "failure"); } } } }
private void doDatabaseMigration() { migrationState.setStatus(Status.RUNNING); migrationState.setStartedAt(new Date()); migrationState.setError(null); Profiler profiler = Profiler.create(LOGGER); try { profiler.startInfo("Starting DB Migration and container restart"); doUpgradeDb(); doRestartContainer(); migrationState.setStatus(Status.SUCCEEDED); profiler.stopInfo("DB Migration and container restart: success"); } catch (MigrationStepExecutionException e) { profiler.stopError("DB migration failed"); LOGGER.error("DB migration ended with an exception", e); saveStatus(e); } catch (Throwable t) { profiler.stopError("Container restart failed"); LOGGER.error("Container restart failed", t); saveStatus(t); } finally { semaphore.release(); } }