#1872 - Reporting ORM metrics defaults to suppressing sql and location after initial collection

This commit is contained in:
rob bygrave
2019-11-29 22:05:58 +13:00
parent 996e582a85
commit be12910aec
5 changed files with 91 additions and 21 deletions
@@ -25,4 +25,12 @@ public interface MetaQueryMetric extends MetaTimedMetric {
*/
String getHash();
/**
* Return true if this is the first metrics collection for this query.
* <p>
* This can be used to suppress including the SQL and location from metrics
* content.
* </p>
*/
boolean initialCollection();
}
@@ -21,10 +21,18 @@ class DumpMetricsJson implements ServerMetricsAsJson {
private Appendable writer;
/**
* By default include SQL for the initial collection only.
*/
private int includeSqlMode = 1;
/**
* By default include with the initial SQL collection only.
*/
private int includeLocation = 1;
private boolean withHeader = true;
private boolean withHash = true;
private boolean withSql = true;
private boolean withLocation = true;
private String newLine = "\n";
private Comparator<MetaTimedMetric> sortBy = SortMetric.NAME;
@@ -44,13 +52,13 @@ class DumpMetricsJson implements ServerMetricsAsJson {
@Override
public ServerMetricsAsJson withLocation(boolean withLocation) {
this.withLocation = withLocation;
this.includeLocation = withLocation ? 2 : 0;
return this;
}
@Override
public ServerMetricsAsJson withSql(boolean withSql) {
this.withSql = withSql;
this.includeSqlMode = withSql ? 2 : 0;
return this;
}
@@ -118,7 +126,7 @@ class DumpMetricsJson implements ServerMetricsAsJson {
dtoQueryMetrics.sort(sortBy);
}
for (MetaQueryMetric metric : dtoQueryMetrics) {
logDtoQuery(metric);
logQuery(metric);
}
}
end();
@@ -196,7 +204,7 @@ class DumpMetricsJson implements ServerMetricsAsJson {
private void log(MetaTimedMetric metric) throws IOException {
metricStart(metric);
appendCounters(metric);
if (withLocation) {
if (includeLocation != 0) {
appendLocation(metric.getLocation());
}
metricEnd();
@@ -209,7 +217,7 @@ class DumpMetricsJson implements ServerMetricsAsJson {
metricEnd();
}
private void logQuery(MetaOrmQueryMetric metric) throws IOException {
private void logQuery(MetaQueryMetric metric) throws IOException {
metricStart(metric);
appendCounters(metric);
if (withHash) {
@@ -220,23 +228,23 @@ class DumpMetricsJson implements ServerMetricsAsJson {
metricEnd();
}
private void logDtoQuery(MetaQueryMetric metric) throws IOException {
metricStart(metric);
appendCounters(metric);
appendLocationAndSql(metric);
metricEnd();
}
private void appendLocationAndSql(MetaQueryMetric metric) throws IOException {
if (withLocation) {
if (includeLocation == 2) {
appendLocation(metric.getLocation());
}
if (withSql) {
if (isIncludeSql(metric)) {
if (includeLocation == 1) {
appendLocation(metric.getLocation());
}
key("sql");
val(metric.getSql());
}
}
private boolean isIncludeSql(MetaQueryMetric metric) {
return includeSqlMode == 2 || includeSqlMode == 1 && metric.initialCollection();
}
private void appendLocation(String location) throws IOException {
if (location != null) {
key("loc");
@@ -11,6 +11,7 @@ class DQueryPlanMetric implements QueryPlanMetric {
private final DQueryPlanMeta meta;
private final DTimedMetric metric;
private boolean collected;
DQueryPlanMetric(DQueryPlanMeta meta, DTimedMetric metric) {
this.meta = meta;
@@ -21,7 +22,8 @@ class DQueryPlanMetric implements QueryPlanMetric {
public void visit(MetricVisitor visitor) {
TimedMetricStats stats = metric.collect(visitor.isReset());
if (stats != null) {
visitor.visitQuery(new Stats(meta, stats));
visitor.visitQuery(new Stats(meta, stats, collected));
collected = true;
}
}
@@ -34,10 +36,12 @@ class DQueryPlanMetric implements QueryPlanMetric {
private final DQueryPlanMeta meta;
private final TimedMetricStats stats;
private final boolean collected;
private Stats(DQueryPlanMeta meta, TimedMetricStats stats) {
private Stats(DQueryPlanMeta meta, TimedMetricStats stats, boolean collected) {
this.meta = meta;
this.stats = stats;
this.collected = collected;
}
@Override
@@ -55,6 +59,11 @@ class DQueryPlanMetric implements QueryPlanMetric {
return meta.getType();
}
@Override
public boolean initialCollection() {
return !collected;
}
@Override
public String getHash() {
return meta.getHash();
@@ -1,6 +1,5 @@
package io.ebeaninternal.server.query;
import io.ebean.ProfileLocation;
import io.ebean.bean.ObjectGraphNode;
import io.ebean.meta.MetaOrmQueryMetric;
import io.ebean.meta.MetaOrmQueryOrigin;
@@ -24,6 +23,8 @@ public final class CQueryPlanStats {
private final TimedMetric timedMetric;
private boolean collected;
private long lastQueryTime;
private final ConcurrentHashMap<ObjectGraphNode, LongAdder> origins;
@@ -94,7 +95,9 @@ public final class CQueryPlanStats {
TimedMetricStats collect = timedMetric.collect(reset);
List<MetaOrmQueryOrigin> origins = getOrigins(reset);
return new Snapshot(queryPlan, collect, lastQueryTime, origins);
Snapshot snapshot = new Snapshot(collected, queryPlan, collect, lastQueryTime, origins);
collected = true;
return snapshot;
}
/**
@@ -150,12 +153,14 @@ public final class CQueryPlanStats {
*/
static class Snapshot implements MetaOrmQueryMetric {
private final boolean collected;
private final CQueryPlan queryPlan;
private final TimedMetricStats metrics;
private final long lastQueryTime;
private final List<MetaOrmQueryOrigin> origins;
Snapshot(CQueryPlan queryPlan, TimedMetricStats metrics, long lastQueryTime, List<MetaOrmQueryOrigin> origins) {
Snapshot(boolean collected, CQueryPlan queryPlan, TimedMetricStats metrics, long lastQueryTime, List<MetaOrmQueryOrigin> origins) {
this.collected = collected;
this.queryPlan = queryPlan;
this.metrics = metrics;
this.lastQueryTime = lastQueryTime;
@@ -242,6 +247,11 @@ public final class CQueryPlanStats {
return queryPlan.getSql();
}
@Override
public boolean initialCollection() {
return !collected;
}
@Override
public List<MetaOrmQueryOrigin> getOrigins() {
return origins;