@Override public IndicesExistsResponse get() { Profiler profiler = Profiler.createIfTrace(EsClient.LOGGER).start(); try { return super.execute().actionGet(); } catch (Exception e) { throw new IllegalStateException(String.format("Fail to execute %s", toString()), e); } finally { if (profiler.isTraceEnabled()) { profiler.stopTrace(toString()); } } }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().startsWith("execute")) { Profiler profiler = Profiler.create(ProfiledDataSource.SQL_LOGGER).start(); Object result = null; try { result = InvocationUtils.invokeQuietly(statement, method, args); } finally { String sql = (String) args[0]; profiler.addContext("sql", SqlLogFormatter.reformatSql(sql)); profiler.stopTrace(""); } return result; } else { return InvocationUtils.invokeQuietly(statement, method, args); } } }
private void stopProfiler(BulkRequest request) { if (profiler.isTraceEnabled()) { profiler.stopTrace(toString(request)); } }
/** * SONAR-4642 * <p/> * Remove active rules on repositories that still exists. * <p/> * For instance, if the javascript repository do not provide anymore some rules, active rules related to this rules will be removed. * But if the javascript repository do not exists anymore, then related active rules will not be removed. * <p/> * The side effect of this approach is that extended repositories will not be managed the same way. * If an extended repository do not exists anymore, then related active rules will be removed. */ private List<ActiveRuleChange> removeActiveRulesOnStillExistingRepositories(DbSession dbSession, RegisterRulesContext recorder, List<RulesDefinition.ExtendedRepository> context) { List<String> repositoryKeys = context.stream() .map(RulesDefinition.ExtendedRepository::key) .collect(MoreCollectors.toList(context.size())); List<ActiveRuleChange> changes = new ArrayList<>(); Profiler profiler = Profiler.create(Loggers.get(getClass())); recorder.getRemoved().forEach(rule -> { // SONAR-4642 Remove active rules only when repository still exists if (repositoryKeys.contains(rule.getRepositoryKey())) { profiler.start(); changes.addAll(qProfileRules.deleteRule(dbSession, rule)); profiler.stopDebug(format("Remove active rule for rule %s", rule.getKey())); } }); return changes; }
/** * If an exception is not thrown, the response needs to be closed by either calling close() directly, or closing the * body content's stream/reader. * * @throws IllegalStateException if the request could not be executed due to a connectivity problem or timeout. Because networks can * fail during an exchange, it is possible that the remote server accepted the request before the failure * @throws MessageException if there was a problem with authentication or if a error message was parsed from the response. * @throws HttpException if the response code is not in range [200..300). Consider using {@link #createErrorMessage(HttpException)} to create more relevant messages for the users. */ public WsResponse call(WsRequest request) { Preconditions.checkState(!globalMode.isMediumTest(), "No WS call should be made in medium test mode"); Profiler profiler = Profiler.createIfDebug(LOG).start(); WsResponse response = target.wsConnector().call(request); profiler.stopDebug(format("%s %d %s", request.getMethod(), response.code(), response.requestUrl())); failIfUnauthorized(response); return response; }
private String loadHashesFromWs(String fileKey) { Profiler profiler = Profiler.createIfDebug(Loggers.get(getClass())) .addContext("file", fileKey) .startDebug("Load line hashes"); GetRequest getRequest = new GetRequest("/api/sources/hash?key=" + ScannerUtils.encodeForUrl(fileKey)); Reader reader = wsClient.call(getRequest).contentReader(); try { return IOUtils.toString(reader); } catch (IOException e) { throw new IllegalStateException(e); } finally { profiler.stopDebug(); } } }
@Test public void add_context() { Profiler profiler = Profiler.create(Loggers.get("DefaultProfilerTest")); profiler.addContext("a_string", "bar"); profiler.addContext("null_value", null); profiler.addContext("an_int", 42); profiler.start(); // do not write context as there's no message assertThat(tester.logs()).isEmpty(); profiler.addContext("after_start", true); profiler.stopInfo("Rules registered"); assertThat(tester.logs()).hasSize(1); assertThat(tester.logs().get(0)) .startsWith("Rules registered | time=") .endsWith("ms | a_string=bar | an_int=42 | after_start=true"); }
@Test public void different_start_and_stop_messages() { 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"); underTest.stopInfo("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 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 Map<String, PluginInfo> loadPlugins(List<PluginReference> pluginReferences) { Map<String, PluginInfo> infosByKey = new HashMap<>(); Profiler profiler = Profiler.create(LOG).startDebug("Load plugins"); for (PluginReference ref : pluginReferences) { Path jarFilePath = getFromCache(ref); PluginInfo info = PluginInfo.create(jarFilePath); if (!shouldSkip(info)) { infosByKey.put(info.getKey(), info); } } profiler.stopDebug(); return infosByKey; }
@VisibleForTesting void recover() { try (DbSession dbSession = dbClient.openSession(false)) { Profiler profiler = Profiler.create(LOGGER).start(); long beforeDate = system2.now() - minAgeInMs; IndexingResult result = new IndexingResult(); Collection<EsQueueDto> items = dbClient.esQueueDao().selectForRecovery(dbSession, beforeDate, loopLimit); while (!items.isEmpty()) { IndexingResult loopResult = new IndexingResult(); groupItemsByType(items).asMap().forEach((type, typeItems) -> loopResult.add(doIndex(dbSession, type, typeItems))); result.add(loopResult); if (loopResult.getSuccessRatio() <= CIRCUIT_BREAKER_IN_PERCENT) { LOGGER.error(LOG_PREFIX + "too many failures [{}/{} documents], waiting for next run", loopResult.getFailures(), loopResult.getTotal()); break; } if (loopResult.getTotal() == 0L) { break; } items = dbClient.esQueueDao().selectForRecovery(dbSession, beforeDate, loopLimit); } if (result.getTotal() > 0L) { profiler.stopInfo(LOG_PREFIX + format("%d documents processed [%d failures]", result.getTotal(), result.getFailures())); } } catch (Throwable t) { LOGGER.error(LOG_PREFIX + "fail to recover documents", t); } }
@Test public void log_on_at_stop() { 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"); assertThat(tester.logs()).hasSize(1); assertThat(tester.logs().get(0)).startsWith("Rules registered | time="); tester.clear(); // info underTest.start(); underTest.stopInfo("Rules registered"); assertThat(tester.logs()).hasSize(1); assertThat(tester.logs().get(0)).startsWith("Rules registered | time="); }
@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"); }
public static Profiler createIfDebug(Logger logger) { if (logger.isDebugEnabled()) { return create(logger); } return NullProfiler.NULL_INSTANCE; }
@Test public void stop_reuses_start_message() { tester.setLevel(LoggerLevel.TRACE); // trace underTest.startTrace("Register rules"); assertThat(tester.logs()).containsOnly("Register rules"); underTest.stopTrace(); assertThat(tester.logs()).hasSize(2); assertThat(tester.logs().get(1)).startsWith("Register rules (done) | time="); tester.clear(); // debug underTest.startDebug("Register rules"); assertThat(tester.logs()).containsOnly("Register rules"); underTest.stopTrace(); assertThat(tester.logs()).hasSize(2); assertThat(tester.logs().get(1)).startsWith("Register rules (done) | time="); tester.clear(); // info underTest.startInfo("Register rules"); assertThat(tester.logs()).containsOnly("Register rules"); underTest.stopTrace(); assertThat(tester.logs()).hasSize(2); assertThat(tester.logs().get(1)).startsWith("Register rules (done) | time="); }
private PlatformLevel start(PlatformLevel platformLevel) { profiler.start(); platformLevel.configure(); profiler.stopTrace(String.format("%s configured", platformLevel.getName())); profiler.start(); platformLevel.start(); profiler.stopTrace(String.format("%s started", platformLevel.getName())); return platformLevel; }
@Override public void start(Object component) { Profiler profiler = Profiler.createIfTrace(Loggers.get(ComponentContainer.class)); profiler.start(); super.start(component); profiler.stopTrace(component.getClass().getCanonicalName() + " started"); } };
@Override public void beforeBulk(long executionId, BulkRequest request) { profiler.start(); }