use of io.mycat.monitor.SqlEntry in project Mycat2 by MyCATApache.
the class MySQLLogConsumer method accept.
@Override
@SneakyThrows
public void accept(SqlEntry sqlEntry) {
if (!init) {
init = true;
try {
init();
} catch (Exception e) {
LOGGER.error("", e);
}
}
boolean isInRuntime = MetaClusterCurrent.exist(IOExecutor.class) && MetaClusterCurrent.exist(JdbcConnectionManager.class) && MetaClusterCurrent.exist(IOExecutor.class);
if (isInRuntime) {
IOExecutor ioExecutor = MetaClusterCurrent.wrapper(IOExecutor.class);
JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
ioExecutor.executeBlocking((Handler<Promise<Void>>) event -> {
try {
try (DefaultConnection connection = jdbcConnectionManager.getConnection(metadataManager.getPrototype())) {
JdbcUtils.execute(connection.getRawConnection(), "INSERT INTO `mycat`.`sql_log` (" + "`instanceId`," + "user," + "connectionId," + "ip," + "port," + "traceId," + "hash," + "sqlType," + "`sql`," + "transactionId," + "sqlTime," + "responseTime," + "affectRow," + "result," + "externalMessage)" + "values(?,?,?,?,?," + "?,?,?,?,?," + "?,?,?,?,?)", Arrays.asList(sqlEntry.getInstanceId(), sqlEntry.getUser(), sqlEntry.getConnectionId(), sqlEntry.getIp(), sqlEntry.getPort(), sqlEntry.getTraceId(), sqlEntry.getHash(), Objects.toString(sqlEntry.getSqlType()), sqlEntry.getSql(), sqlEntry.getTransactionId(), sqlEntry.getSqlTime(), sqlEntry.getResponseTime(), sqlEntry.getAffectRow(), sqlEntry.isResult(), sqlEntry.getExternalMessage()));
}
} catch (Exception e) {
LOGGER.info(" warning sql:{} , info:{}", sqlEntry.getSql(), sqlEntry);
LOGGER.error("", e);
} finally {
event.tryComplete();
}
});
}
}
use of io.mycat.monitor.SqlEntry in project Mycat2 by MyCATApache.
the class SqlStatCollector method collect.
@Override
public List<MetricFamilySamples> collect() {
try {
GaugeMetricFamily gaugeMetricFamily = new GaugeMetricFamily("sql_stat", "sql_stat", ImmutableList.of("statement", "time_type"));
List<SqlEntry> records = SqlRecorderRuntime.INSTANCE.getRecords();
for (SqlEntry record : records) {
if (record.getSqlTime() > 0) {
String sql = "/*+ " + record.getTraceId() + "*/" + record.getSql().toString();
gaugeMetricFamily.addMetric(ImmutableList.of(sql, "EXECUTE_TIME"), record.getSqlTime());
gaugeMetricFamily.addMetric(ImmutableList.of(sql, "START_TIME"), record.getResponseTime().minus(Duration.ofMillis(record.getSqlTime())).toEpochSecond(ZoneOffset.UTC));
gaugeMetricFamily.addMetric(ImmutableList.of(sql, "END_TIME"), record.getResponseTime().toEpochSecond(ZoneOffset.UTC));
}
}
return ImmutableList.of(gaugeMetricFamily);
} catch (Throwable e) {
LOGGER.error("", e);
throw e;
}
}
use of io.mycat.monitor.SqlEntry in project Mycat2 by MyCATApache.
the class HintHandler method showSlowSql.
private Future<Void> showSlowSql(Response response, String body) {
ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
resultSetBuilder.addColumnInfo("trace_id", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("sql", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("sql_rows", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("start_time", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("end_time", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("execute_time", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("target_name", JDBCType.VARCHAR);
Stream<SqlEntry> sqlRecords = SqlRecorderRuntime.INSTANCE.getRecords().stream().sorted(Comparator.comparingLong(SqlEntry::getSqlTime).reversed());
Map map = JsonUtil.from(body, Map.class);
Object idText = map.get("trace_id");
if (idText != null) {
sqlRecords = sqlRecords.filter(i -> idText == i.getTraceId());
}
sqlRecords.forEach(r -> {
resultSetBuilder.addObjectRowPayload(Arrays.asList(Objects.toString(r.getTraceId()), Objects.toString(r.getSql()), Objects.toString(r.getAffectRow()), Objects.toString(r.getResponseTime().minus(Duration.ofMillis(r.getSqlTime()))), Objects.toString(r.getResponseTime()), Objects.toString(r.getSqlTime()), Objects.toString(null)));
});
return response.sendResultSet(resultSetBuilder.build());
}
Aggregations