|
13 | 13 | */ |
14 | 14 | package io.trino.dblistener; |
15 | 15 |
|
| 16 | +import com.google.inject.Inject; |
| 17 | +import io.airlift.json.JsonCodec; |
| 18 | +import io.airlift.json.JsonCodecFactory; |
16 | 19 | import io.airlift.log.Logger; |
17 | 20 | import io.trino.spi.eventlistener.EventListener; |
18 | 21 | import io.trino.spi.eventlistener.QueryCompletedEvent; |
| 22 | +import io.trino.spi.eventlistener.QueryStatistics; |
| 23 | +import io.trino.spi.eventlistener.StageGcStatistics; |
| 24 | +import org.jdbi.v3.core.Handle; |
| 25 | +import org.jdbi.v3.core.Jdbi; |
| 26 | + |
| 27 | +import java.util.List; |
19 | 28 |
|
20 | 29 | public class DbListener |
21 | 30 | implements EventListener |
22 | 31 | { |
23 | 32 | private static final Logger LOG = Logger.get(DbListener.class); |
| 33 | + private static final JsonCodec<List<StageGcStatistics>> STAGE_GC_STATS_CODEC = new JsonCodecFactory().listJsonCodec(StageGcStatistics.class); |
| 34 | + private final Jdbi jdbi; |
| 35 | + |
| 36 | + @Inject |
| 37 | + public DbListener(Jdbi jdbi) |
| 38 | + { |
| 39 | + this.jdbi = jdbi; |
| 40 | + } |
24 | 41 |
|
25 | 42 | @Override |
26 | 43 | public void queryCompleted(QueryCompletedEvent queryCompletedEvent) |
27 | 44 | { |
28 | | - LOG.info("query ID is: %s", queryCompletedEvent.getMetadata().getQueryId()); |
| 45 | + jdbi.useTransaction(handle -> { |
| 46 | + saveQuery(queryCompletedEvent, handle); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + private void saveQuery(QueryCompletedEvent queryCompletedEvent, Handle handle) |
| 51 | + { |
| 52 | + String sql = "" + |
| 53 | + "INSERT INTO queries (" + |
| 54 | + " query_id," + |
| 55 | + " catalog," + |
| 56 | + " `schema`," + |
| 57 | + " environment," + |
| 58 | + " query_text," + |
| 59 | + " query_plan," + |
| 60 | + " created," + |
| 61 | + " finished," + |
| 62 | + " query_state," + |
| 63 | + " error_info," + |
| 64 | + " cpu_time," + |
| 65 | + " failed_cpu_time," + |
| 66 | + " wall_time," + |
| 67 | + " queued_time," + |
| 68 | + " scheduled_time," + |
| 69 | + " failed_scheduled_time," + |
| 70 | + " waiting_time," + |
| 71 | + " analysis_time," + |
| 72 | + " planning_time," + |
| 73 | + " execution_time," + |
| 74 | + " input_blocked_time," + |
| 75 | + " failed_input_blocked_time," + |
| 76 | + " output_blocked_time," + |
| 77 | + " failed_output_blocked_time," + |
| 78 | + " peak_user_memory_bytes," + |
| 79 | + " peak_task_total_memory," + |
| 80 | + " physical_input_bytes," + |
| 81 | + " physical_input_rows," + |
| 82 | + " processed_input_bytes," + |
| 83 | + " processed_input_rows," + |
| 84 | + " internal_network_bytes," + |
| 85 | + " internal_network_rows," + |
| 86 | + " total_bytes," + |
| 87 | + " total_rows," + |
| 88 | + " output_bytes," + |
| 89 | + " output_rows," + |
| 90 | + " written_bytes," + |
| 91 | + " written_rows," + |
| 92 | + " cumulative_memory," + |
| 93 | + " failed_cumulative_memory," + |
| 94 | + " completed_splits," + |
| 95 | + " stage_gc_statistics" + |
| 96 | + ")" + |
| 97 | + "VALUES (" + |
| 98 | + " :query_id," + |
| 99 | + " :catalog," + |
| 100 | + " :schema," + |
| 101 | + " :environment," + |
| 102 | + " :query_text," + |
| 103 | + " :query_plan," + |
| 104 | + " :created," + |
| 105 | + " :finished," + |
| 106 | + " :query_state," + |
| 107 | + " :error_info," + |
| 108 | + " :cpu_time," + |
| 109 | + " :failed_cpu_time," + |
| 110 | + " :wall_time," + |
| 111 | + " :queued_time," + |
| 112 | + " :scheduled_time," + |
| 113 | + " :failed_scheduled_time," + |
| 114 | + " :waiting_time," + |
| 115 | + " :analysis_time," + |
| 116 | + " :planning_time," + |
| 117 | + " :execution_time," + |
| 118 | + " :input_blocked_time," + |
| 119 | + " :failed_input_blocked_time," + |
| 120 | + " :output_blocked_time," + |
| 121 | + " :failed_output_blocked_time," + |
| 122 | + " :peak_user_memory_bytes," + |
| 123 | + " :peak_task_total_memory," + |
| 124 | + " :physical_input_bytes," + |
| 125 | + " :physical_input_rows," + |
| 126 | + " :processed_input_bytes," + |
| 127 | + " :processed_input_rows," + |
| 128 | + " :internal_network_bytes," + |
| 129 | + " :internal_network_rows," + |
| 130 | + " :total_bytes," + |
| 131 | + " :total_rows," + |
| 132 | + " :output_bytes," + |
| 133 | + " :output_rows," + |
| 134 | + " :written_bytes," + |
| 135 | + " :written_rows," + |
| 136 | + " :cumulative_memory," + |
| 137 | + " :failed_cumulative_memory," + |
| 138 | + " :completed_splits," + |
| 139 | + " :stage_gc_statistics" + |
| 140 | + ")"; |
| 141 | + |
| 142 | + QueryStatistics queryStatistics = queryCompletedEvent.getStatistics(); |
| 143 | + String failureInfo = queryCompletedEvent.getFailureInfo().isPresent() ? queryCompletedEvent.getFailureInfo().get().getFailureMessage().get() : ""; |
| 144 | + long scheduledTime = queryStatistics.getScheduledTime().isPresent() ? queryStatistics.getScheduledTime().get().toMillis() : 0; |
| 145 | + long failedScheduledTime = queryStatistics.getFailedScheduledTime().isPresent() ? queryStatistics.getFailedScheduledTime().get().toMillis() : 0; |
| 146 | + long waitingTime = queryStatistics.getResourceWaitingTime().isPresent() ? queryStatistics.getResourceWaitingTime().get().toMillis() : 0; |
| 147 | + long analysisTime = queryStatistics.getAnalysisTime().isPresent() ? queryStatistics.getAnalysisTime().get().toMillis() : 0; |
| 148 | + long planningTime = queryStatistics.getPlanningTime().isPresent() ? queryStatistics.getPlanningTime().get().toMillis() : 0; |
| 149 | + long executionTime = queryStatistics.getExecutionTime().isPresent() ? queryStatistics.getExecutionTime().get().toMillis() : 0; |
| 150 | + long inputBlockedTime = queryStatistics.getInputBlockedTime().isPresent() ? queryStatistics.getInputBlockedTime().get().toMillis() : 0; |
| 151 | + long failedInputBlockedTime = queryStatistics.getFailedInputBlockedTime().isPresent() ? queryStatistics.getFailedInputBlockedTime().get().toMillis() : 0; |
| 152 | + long outputBlockedTime = queryStatistics.getOutputBlockedTime().isPresent() ? queryStatistics.getOutputBlockedTime().get().toMillis() : 0; |
| 153 | + long failedOutputBlockedTime = queryStatistics.getFailedOutputBlockedTime().isPresent() ? queryStatistics.getFailedOutputBlockedTime().get().toMillis() : 0; |
| 154 | + |
| 155 | + handle.createUpdate(sql) |
| 156 | + .bind("query_id", queryCompletedEvent.getMetadata().getQueryId()) |
| 157 | + .bind("catalog", queryCompletedEvent.getContext().getCatalog().orElse("")) |
| 158 | + .bind("schema", queryCompletedEvent.getContext().getSchema().orElse("")) |
| 159 | + .bind("environment", queryCompletedEvent.getContext().getEnvironment()) |
| 160 | + .bind("query_text", queryCompletedEvent.getMetadata().getQuery()) |
| 161 | + .bind("query_plan", queryCompletedEvent.getMetadata().getPlan().orElse("")) |
| 162 | + .bind("created", queryCompletedEvent.getCreateTime()) |
| 163 | + .bind("finished", queryCompletedEvent.getEndTime()) |
| 164 | + .bind("query_state", queryCompletedEvent.getMetadata().getQueryState()) |
| 165 | + .bind("error_info", failureInfo) |
| 166 | + .bind("cpu_time", queryStatistics.getCpuTime().toMillis()) |
| 167 | + .bind("failed_cpu_time", queryStatistics.getFailedCpuTime().toMillis()) |
| 168 | + .bind("wall_time", queryStatistics.getWallTime().toMillis()) |
| 169 | + .bind("queued_time", queryStatistics.getQueuedTime().toMillis()) |
| 170 | + .bind("scheduled_time", scheduledTime) |
| 171 | + .bind("failed_scheduled_time", failedScheduledTime) |
| 172 | + .bind("waiting_time", waitingTime) |
| 173 | + .bind("analysis_time", analysisTime) |
| 174 | + .bind("planning_time", planningTime) |
| 175 | + .bind("execution_time", executionTime) |
| 176 | + .bind("input_blocked_time", inputBlockedTime) |
| 177 | + .bind("failed_input_blocked_time", failedInputBlockedTime) |
| 178 | + .bind("output_blocked_time", outputBlockedTime) |
| 179 | + .bind("failed_output_blocked_time", failedOutputBlockedTime) |
| 180 | + .bind("peak_user_memory_bytes", queryStatistics.getPeakUserMemoryBytes()) |
| 181 | + .bind("peak_task_total_memory", queryStatistics.getPeakTaskTotalMemory()) |
| 182 | + .bind("physical_input_bytes", queryStatistics.getPhysicalInputBytes()) |
| 183 | + .bind("physical_input_rows", queryStatistics.getPhysicalInputRows()) |
| 184 | + .bind("processed_input_bytes", queryStatistics.getProcessedInputBytes()) |
| 185 | + .bind("processed_input_rows", queryStatistics.getProcessedInputRows()) |
| 186 | + .bind("internal_network_bytes", queryStatistics.getInternalNetworkBytes()) |
| 187 | + .bind("internal_network_rows", queryStatistics.getInternalNetworkRows()) |
| 188 | + .bind("total_bytes", queryStatistics.getTotalBytes()) |
| 189 | + .bind("total_rows", queryStatistics.getTotalRows()) |
| 190 | + .bind("output_bytes", queryStatistics.getOutputBytes()) |
| 191 | + .bind("output_rows", queryStatistics.getOutputRows()) |
| 192 | + .bind("written_bytes", queryStatistics.getWrittenBytes()) |
| 193 | + .bind("written_rows", queryStatistics.getWrittenRows()) |
| 194 | + .bind("cumulative_memory", queryStatistics.getCumulativeMemory()) |
| 195 | + .bind("failed_cumulative_memory", queryStatistics.getFailedCumulativeMemory()) |
| 196 | + .bind("completed_splits", queryStatistics.getCompletedSplits()) |
| 197 | + .bind("stage_gc_statistics", STAGE_GC_STATS_CODEC.toJson(queryStatistics.getStageGcStatistics())) |
| 198 | + .execute(); |
29 | 199 | } |
30 | 200 | } |
0 commit comments