@Override public String toString() { return "Query [ " + "startTimeInMilliseconds=" + startTimeInMilliseconds + ", executionTimeInMilliseconds=" + executionTimeInMilliseconds + ", clientAddress=" + clientAddress + ", statementType=" + statementType .name() + ", statement=" + statement + ", keyspace=" + keyspace + ", tableName=" + tableName + ", " + "consistencyLevel=" + consistencyLevel.name() + " ]"; }
/** * Check if measurement is simple measurement. * * @return has value */ public boolean isSimple() { return type.equals(MeasurementType.SIMPLE); }
/** * Diagnostics' configuration reload operation. */ public void reload() { diagnostics.reload(); } }
@Override public void process(Query query) { final String statementType = query.statementType().name(); final String consistencyLevel = query.consistencyLevel().name(); for (RequestRate requestRate : requestRates) { if (statementMatches(statementType, requestRate) && consistencyLevelMatches(consistencyLevel, requestRate)) { requestRate.increment(); } } }
private Query extractQuery(final long startTime, final long execTime, final Statement statement) { final String queryString = statementQueryString(statement); final Query.StatementType queryType = queryType(queryString); return Query.create(startTime, execTime, host, queryType, statement.getKeyspace(), "", queryString, extractConsistencyLevel(statement)); }
/** * 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; }
/** * Encodes measurement into JSON. * * @return JSON-formatted string representation of measurement. */ public String toJson() { return "{\"name\":\"" + name + "\"" + ",\"type\":\"" + type + "\"" + ",\"value\":" + (value.hasValue() ? value.getValue() : "null") + ",\"time\":" + time + ",\"timeUnit\":\"" + timeUnit + "\"" + ",\"tags\":" + appendMap(tags) + ",\"fields\":" + appendMap(fields) + "}"; }
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; }
/** * Completes the initialization and activates the query processing. */ public void activate() { this.diagnosticsProcessor = new DiagnosticsProcessor(config); this.isRunning.set(true); initEndpoints(); }
@Override public void run() { connector.waitForSetupCompleted(); diagnostics.activate(); logger.info("Cassandra Diagnostics initialized."); } });
private Measurement createSlowQueryCountMeasurement(double count, StatementType statementType) { final Map<String, String> tags = new HashMap<>(2); tags.put("host", globalConfiguration.hostname); tags.put("systemName", globalConfiguration.systemName); tags.put("statementType", statementType.toString()); return Measurement.createSimple(slowQueryCountMeasurementName, count, System.currentTimeMillis(), TimeUnit.MILLISECONDS, tags, new HashMap<String, String>()); } }
/** * DiagnosticsProcessor constructor. * * @param configuration Configuration object */ public DiagnosticsProcessor(final Configuration configuration) { if (configuration.reporters == null) { throw new IllegalStateException("Configuration does not have any reporter defined."); } if (configuration.modules == null) { throw new IllegalStateException("Configuration does not have any module defined."); } if (configuration.global == null) { throw new IllegalStateException("Configuration does not have global configuration defined."); } initReporters(configuration.reporters, configuration.global); initModules(configuration.modules, configuration.global); }
/** * Returns the default configuration. * * @return default configuration */ public static GlobalConfiguration getDefault() { return new GlobalConfiguration(); }
/** * Create a complex measurement object (it does not have value and measurements are in fields as key value pairs). * * @param name Measurement name * @param time Measurement time * @param timeUnit Measurement time unit * @param tags Tag name value pairs * @param fields Field name value pairs * @return Measurement object */ public static Measurement createComplex(final String name, final long time, final TimeUnit timeUnit, final Map<String, String> tags, final Map<String, String> fields) { return new Measurement(name, MeasurementType.COMPLEX, null, time, timeUnit, tags, fields); }
/** * Returns a new instance of Query. * * @param startTimeInMilliseconds query execution's start time, given as epoch timestamp in milliseconds * @param executionTimeInMilliseconds query execution time in milliseconds * @param clientAddress query's client socket address * @param statementType type of query's statement * @param keyspace query's key space * @param tableName query's table name * @param statement query's CQL statement * @param consistencyLevel query's consistencyLevel * @return a new Query instance */ public static Query create(final long startTimeInMilliseconds, final long executionTimeInMilliseconds, final String clientAddress, final StatementType statementType, final String keyspace, final String tableName, final String statement, final ConsistencyLevel consistencyLevel) { return new Query(startTimeInMilliseconds, executionTimeInMilliseconds, clientAddress, statementType, keyspace, tableName, statement, consistencyLevel); }
@Override public void report(Query query) { if (isRunning.get()) { diagnosticsProcessor.process(query); } }
private void initModules(final List<ModuleConfiguration> modulesConfiguration, final GlobalConfiguration globalConfiguration) { for (ModuleConfiguration moduleConfig : modulesConfiguration) { try { logger.info("Creating module for class name {}", moduleConfig.module); final Module module = createModule(moduleConfig, globalConfiguration); modules.add(module); } catch (Exception e) { logger.warn("Failed to create module by class name", e); } } }
/** * Check if measurement is complex measurement. * * @return has value */ public boolean isComplex() { return type.equals(MeasurementType.COMPLEX); }