/** * Create SlowQueryLogDecider for provided configuration. * @param configuration SlowQueryConfiguration which is active. * @return slow query decider for this configuration. */ public static SlowQueryLogDecider create(SlowQueryConfiguration configuration) { return new SlowQueryLogDecider(configuration); }
/** * Slow query count reporting rate in milliseconds. * * @return Slow query count reporting rate in milliseconds */ public long slowQueryCountReportingRateInMillis() { return slowQueryCountReportTimeunit().toMillis(slowQueryCountReportPeriod()); }
private boolean executionTimeForLogging(long executionTimeInMilliseconds) { if (slowQueryConfiguration.slowQueryThreshold() == 0) { logger.trace("Slow query threshold turned off, logging all queries."); return true; } logger.trace("Checking if execution time:{} is above threshold: {}", executionTimeInMilliseconds, slowQueryConfiguration.slowQueryThreshold()); if (executionTimeInMilliseconds > slowQueryConfiguration.slowQueryThreshold()) { return true; } return false; }
/** * Constructor. * * @param configuration Module configuration * @param reporters Reporter list * @param globalConfiguration Global diagnostics configuration * @throws ConfigurationException in case the provided module configuration is not valid */ public SlowQueryModule(ModuleConfiguration configuration, List<Reporter> reporters, final GlobalConfiguration globalConfiguration) throws ConfigurationException { super(configuration, reporters, globalConfiguration); config = SlowQueryConfiguration.create(configuration.options); service = configuration.getMeasurementOrDefault(DEFAULT_MEASUREMENT_NAME); slowQueryCountMeasurementName = service + SLOW_QUERY_COUNT_SUFFIX; slowQueryLogDecider = SlowQueryLogDecider.create(config); slowQueryCounts = new HashMap<>(); for (StatementType statementType : StatementType.values()) { slowQueryCounts.put(statementType, new AtomicCounter()); } if (config.slowQueryCountReportEnabled()) { timer = new Timer(SLOW_QUERY_COUNT_THREAD_NAME); timer.schedule(new SlowQueryReportTask(), 0, config.slowQueryCountReportingRateInMillis()); } else { timer = null; } }
if (!slowQueryLogDecider.isForReporting(query)) { return; if (config.slowQueryCountReportEnabled()) { slowQueryCounts.get(query.statementType()).increment(); if (config.slowQueryReportEnabled()) { final Map<String, String> tags = new HashMap<>(4); tags.put("host", globalConfiguration.hostname); report(measurement);
/** * Based on defined criteria decide if this query is eligible for reporting. * @param query Query candidate for report. * @return if this query is eligible for report. */ public boolean isForReporting(Query query) { if (executionTimeForLogging(query.executionTimeInMilliseconds()) && tableForLogging(query) && typeForLogging(query)) { return true; } return false; }
private boolean typeForLogging(Query query) { logger.trace("Checking if query type is for logging."); boolean logAll = slowQueryConfiguration.queryTypesToLog().contains("ALL"); boolean queryTypeMatches = slowQueryConfiguration.queryTypesToLog().contains(query.statementType().toString()); return logAll || queryTypeMatches; }
/** * Creates typed configuration for slow query module out of generic module configuration. * * @param options Module configuration options. * @return typed slow query module configuration from a generic one. * @throws ConfigurationException in case the provided module configuration is not valid */ public static SlowQueryConfiguration create(Map<String, Object> options) throws ConfigurationException { SlowQueryConfiguration conf = new SlowQueryConfiguration(); Yaml yaml = new Yaml(); String str = yaml.dumpAsMap(options); try { conf.values = yaml.loadAs(str, Values.class); } catch (Exception e) { throw new ConfigurationException("Unable to load configuration.", e); } return conf; }
private boolean tableForLogging(Query query) { logger.trace("Checking if table is in tables for logging."); if (slowQueryConfiguration.tablesForLogging().isEmpty()) { return true; } if (StringUtils.isBlank(query.fullTableName())) { logger.debug("Query does not have table name."); return false; } for (String tableForLogging : slowQueryConfiguration.tablesForLogging()) { if (tableForLogging.equals(query.fullTableName())) { logger.debug("Taable {} is eligible for logging.", query.fullTableName()); return true; } } return false; }