in src/core-metadata/src/main/java/org/apache/kylin/metadata/query/QueryMetricsContext.java [86:173]
private void doCollect(final QueryContext context) {
// set sql
this.sql = context.getMetrics().getCorrectedSql();
this.sqlPattern = context.getMetrics().getSqlPattern();
this.queryTime = context.getMetrics().getQueryStartTime();
// for query stats
TimeZone timeZone = TimeZone.getTimeZone(KylinConfig.getInstanceFromEnv().getTimeZone());
LocalDate date = Instant.ofEpochMilli(this.queryTime).atZone(timeZone.toZoneId()).toLocalDate();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM", Locale.getDefault(Locale.Category.FORMAT));
this.month = date.withDayOfMonth(1).format(formatter);
this.queryFirstDayOfMonth = TimeUtil.getMonthStart(this.queryTime);
this.queryDay = TimeUtil.getDayStart(this.queryTime);
this.queryFirstDayOfWeek = TimeUtil.getWeekStart(this.queryTime);
this.submitter = context.getAclInfo().getUsername();
this.server = context.getMetrics().getServer();
if (QueryContext.current().getQueryTagInfo().isAsyncQuery()) {
QueryContext.currentTrace().endLastSpan();
this.queryDuration = System.currentTimeMillis() - queryTime;
} else if (QueryContext.current().getQueryTagInfo().isStorageCacheUsed()) {
this.queryDuration = 0;
} else {
this.queryDuration = QueryContext.currentMetrics().duration();
}
this.totalScanBytes = context.getMetrics().getTotalScanBytes();
this.totalScanCount = context.getMetrics().getTotalScanRows();
this.queryJobCount = context.getMetrics().getQueryJobCount();
this.queryStageCount = context.getMetrics().getQueryStageCount();
this.queryTaskCount = context.getMetrics().getQueryTaskCount();
this.cpuTime = context.getMetrics().getCpuTime();
this.isPushdown = context.getQueryTagInfo().isPushdown();
this.isTimeout = context.getQueryTagInfo().isTimeout();
if (context.getQueryTagInfo().isStorageCacheUsed() && context.getEngineType() != null) {
this.engineType = context.getEngineType();
} else {
if (context.getQueryTagInfo().isPushdown()) {
this.engineType = context.getPushdownEngine();
} else if (context.getQueryTagInfo().isConstantQuery()) {
this.engineType = QueryHistory.EngineType.CONSTANTS.name();
} else if (!context.getMetrics().isException()) {
this.engineType = QueryHistory.EngineType.NATIVE.name();
}
}
this.queryStatus = context.getMetrics().isException() ? QueryHistory.QUERY_HISTORY_FAILED
: QueryHistory.QUERY_HISTORY_SUCCEEDED;
if (context.getQueryTagInfo().isHitExceptionCache() || context.getQueryTagInfo().isStorageCacheUsed()) {
this.isCacheHit = true;
this.cacheType = context.getQueryTagInfo().getStorageCacheType();
}
this.resultRowCount = context.getMetrics().getResultRowCount();
this.queryMsg = context.getMetrics().getQueryMsg();
this.isIndexHit = !context.getMetrics().isException() && !context.getQueryTagInfo().isPushdown()
&& !this.engineType.equals(QueryHistory.EngineType.CONSTANTS.name());
this.projectName = context.getProject();
collectErrorType(context);
List<RealizationMetrics> realizationMetricList = collectRealizationMetrics(
QueryContext.current().getQueryRealizations());
QueryHistoryInfo queryHistoryInfo = new QueryHistoryInfo(context.getMetrics().isExactlyMatch(),
context.getMetrics().getSegCount(),
Objects.nonNull(this.errorType) && !this.errorType.equals(QueryHistory.NO_REALIZATION_FOUND_ERROR));
queryHistoryInfo.setRealizationMetrics(realizationMetricList);
queryHistoryInfo.setQueryMetrics(collectQueryMetrics());
List<List<String>> querySnapshots = new ArrayList<>();
for (NativeQueryRealization qcReal : QueryContext.current().getQueryRealizations()) {
if (CollectionUtils.isEmpty(qcReal.getLookupTables())) {
continue;
}
querySnapshots.add(qcReal.getLookupTables());
}
queryHistoryInfo.setQuerySnapshots(querySnapshots);
queryHistoryInfo.setCacheType(this.cacheType);
queryHistoryInfo.setQueryMsg(this.queryMsg);
queryHistoryInfo.setHostName(AddressUtil.getHostName());
queryHistoryInfo.setPort(KylinConfig.getInstanceFromEnv().getServerPort());
this.queryHistoryInfo = queryHistoryInfo;
this.queryHistoryInfo.setTraces(createTraces(context));
}