Search in sources :

Example 1 with SqlEntry

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();
            }
        });
    }
}
Also used : MetadataManager(io.mycat.MetadataManager) SqlEntry(io.mycat.monitor.SqlEntry) java.util(java.util) Logger(org.slf4j.Logger) Connection(java.sql.Connection) MetaClusterCurrent(io.mycat.MetaClusterCurrent) SneakyThrows(lombok.SneakyThrows) Promise(io.vertx.core.Promise) LoggerFactory(org.slf4j.LoggerFactory) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) IOExecutor(io.mycat.IOExecutor) Consumer(java.util.function.Consumer) SQLException(java.sql.SQLException) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) JdbcUtils(com.alibaba.druid.util.JdbcUtils) Handler(io.vertx.core.Handler) Promise(io.vertx.core.Promise) MetadataManager(io.mycat.MetadataManager) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) IOExecutor(io.mycat.IOExecutor) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) SQLException(java.sql.SQLException) SneakyThrows(lombok.SneakyThrows)

Example 2 with SqlEntry

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;
    }
}
Also used : SqlEntry(io.mycat.monitor.SqlEntry) GaugeMetricFamily(io.prometheus.client.GaugeMetricFamily)

Example 3 with SqlEntry

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());
}
Also used : MycatdbCommand(io.mycat.commands.MycatdbCommand) io.mycat(io.mycat) CSVRecord(org.apache.commons.csv.CSVRecord) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) CSVFormat(org.apache.commons.csv.CSVFormat) HeartbeatFlow(io.mycat.replica.heartbeat.HeartbeatFlow) Duration(java.time.Duration) MySQLErrorCode(io.mycat.beans.mysql.MySQLErrorCode) JsonUtil(io.mycat.util.JsonUtil) MySqlHintStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlHintStatement) io.mycat.config(io.mycat.config) Timestamp(java.sql.Timestamp) Reader(java.io.Reader) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) ArrayBindable(org.apache.calcite.runtime.ArrayBindable) MycatSQLLogMonitor(io.mycat.monitor.MycatSQLLogMonitor) io.mycat.sqlhandler(io.mycat.sqlhandler) NormalTable(io.mycat.calcite.table.NormalTable) Nullable(org.jetbrains.annotations.Nullable) ReplicaSelector(io.mycat.replica.ReplicaSelector) UpdateSQLHandler(io.mycat.sqlhandler.dml.UpdateSQLHandler) Stream(java.util.stream.Stream) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) DrdsRunnerHelper(io.mycat.calcite.DrdsRunnerHelper) PhysicsInstance(io.mycat.replica.PhysicsInstance) NotNull(org.jetbrains.annotations.NotNull) ShardingTable(io.mycat.calcite.table.ShardingTable) VertxExecuter(io.mycat.vertx.VertxExecuter) SqlEntry(io.mycat.monitor.SqlEntry) java.util(java.util) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) LocalDateTime(java.time.LocalDateTime) XaLog(cn.mycat.vertx.xa.XaLog) Supplier(java.util.function.Supplier) Iterators(com.google.common.collect.Iterators) SqlResultSetService(io.mycat.commands.SqlResultSetService) InterruptThreadHint(io.mycat.hint.InterruptThreadHint) MycatSQLExprTableSourceUtil(io.mycat.util.MycatSQLExprTableSourceUtil) KillThreadHint(io.mycat.hint.KillThreadHint) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) JDBCType(java.sql.JDBCType) PrepareExecutor(io.mycat.calcite.PrepareExecutor) SchemaHandler(io.mycat.calcite.table.SchemaHandler) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) ImmutableList(com.google.common.collect.ImmutableList) DatasourceStatus(io.mycat.replica.heartbeat.DatasourceStatus) Observable(io.reactivex.rxjava3.core.Observable) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) StreamSupport(java.util.stream.StreamSupport) GlobalTable(io.mycat.calcite.table.GlobalTable) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLUtils(com.alibaba.druid.sql.SQLUtils) Flowable(io.reactivex.rxjava3.core.Flowable) PromiseInternal(io.vertx.core.impl.future.PromiseInternal) io.mycat.hint(io.mycat.hint) VertxUpdateExecuter(io.mycat.vertx.VertxUpdateExecuter) StringWriter(java.io.StringWriter) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) ExecutorProvider(io.mycat.calcite.ExecutorProvider) IOException(java.io.IOException) QuoteMode(org.apache.commons.csv.QuoteMode) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) SqlRecorderRuntime(io.mycat.exporter.SqlRecorderRuntime) VertxUtil(io.mycat.util.VertxUtil) ReplicaSwitchType(io.mycat.replica.ReplicaSwitchType) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) MycatInsertRel(io.mycat.calcite.physical.MycatInsertRel) io.mycat.calcite.spm(io.mycat.calcite.spm) NameMap(io.mycat.util.NameMap) MysqlPayloadObject(io.mycat.api.collector.MysqlPayloadObject) JdbcUtils(com.alibaba.druid.util.JdbcUtils) CodeExecuterContext(io.mycat.calcite.CodeExecuterContext) HeartBeatStatus(io.mycat.replica.heartbeat.HeartBeatStatus) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) UnmodifiableIterator(com.google.common.collect.UnmodifiableIterator) FileReader(java.io.FileReader) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) StorageManager(io.mycat.sqlhandler.config.StorageManager) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) SqlEntry(io.mycat.monitor.SqlEntry) MysqlPayloadObject(io.mycat.api.collector.MysqlPayloadObject) NameMap(io.mycat.util.NameMap)

Aggregations

SqlEntry (io.mycat.monitor.SqlEntry)3 JdbcUtils (com.alibaba.druid.util.JdbcUtils)2 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)2 java.util (java.util)2 XaLog (cn.mycat.vertx.xa.XaLog)1 SQLUtils (com.alibaba.druid.sql.SQLUtils)1 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)1 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)1 MySqlHintStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlHintStatement)1 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)1 ImmutableList (com.google.common.collect.ImmutableList)1 Iterators (com.google.common.collect.Iterators)1 UnmodifiableIterator (com.google.common.collect.UnmodifiableIterator)1 io.mycat (io.mycat)1 IOExecutor (io.mycat.IOExecutor)1