#1871 - Add ability to write metrics in JSON form to Appendable

This commit is contained in:
rob bygrave
2019-11-29 17:19:01 +13:00
parent eb292e017e
commit 67c2e1d24c
3 changed files with 127 additions and 58 deletions
@@ -33,6 +33,18 @@ public interface ServerMetricsAsJson {
*/
ServerMetricsAsJson withNewLine(boolean withNewLine);
/**
* Set to include a heading of the database name.
* <p>
* When this is false the metrics are written without json array start or array end.
*/
ServerMetricsAsJson withHeader(boolean withHeader);
/**
* Collect and write metrics as JSON to the given buffer.
*/
void write(Appendable buffer);
/**
* Return the metrics in raw JSON.
*/
@@ -10,6 +10,7 @@ import io.ebean.meta.ServerMetrics;
import io.ebean.meta.ServerMetricsAsJson;
import io.ebean.meta.SortMetric;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Comparator;
import java.util.List;
@@ -18,8 +19,9 @@ class DumpMetricsJson implements ServerMetricsAsJson {
private final Database database;
private final StringWriter writer = new StringWriter();
private Appendable writer;
private boolean withHeader = true;
private boolean withHash = true;
private boolean withSql = true;
private boolean withLocation = true;
@@ -34,6 +36,12 @@ class DumpMetricsJson implements ServerMetricsAsJson {
this.database = database;
}
@Override
public ServerMetricsAsJson withHeader(boolean withHeader) {
this.withHeader = withHeader;
return this;
}
@Override
public ServerMetricsAsJson withLocation(boolean withLocation) {
this.withLocation = withLocation;
@@ -66,97 +74,111 @@ class DumpMetricsJson implements ServerMetricsAsJson {
@Override
public String json() {
writer = new StringWriter();
collect(database.getMetaInfoManager().collectMetrics());
return writer.toString();
}
@Override
public void write(Appendable buffer) {
writer = buffer;
collect(database.getMetaInfoManager().collectMetrics());
}
private void collect(ServerMetrics serverMetrics) {
try {
start();
for (MetaTimedMetric metric : serverMetrics.getTimedMetrics()) {
log(metric);
}
start();
for (MetaTimedMetric metric : serverMetrics.getTimedMetrics()) {
log(metric);
}
List<MetaCountMetric> countMetrics = serverMetrics.getCountMetrics();
if (!countMetrics.isEmpty()) {
if (sortBy != null) {
countMetrics.sort(SortMetric.COUNT_NAME);
}
for (MetaCountMetric metric : countMetrics) {
logCount(metric);
}
}
List<MetaCountMetric> countMetrics = serverMetrics.getCountMetrics();
if (!countMetrics.isEmpty()) {
if (sortBy != null) {
countMetrics.sort(SortMetric.COUNT_NAME);
List<MetaOrmQueryMetric> ormQueryMetrics = serverMetrics.getOrmQueryMetrics();
if (!ormQueryMetrics.isEmpty()) {
if (sortBy != null) {
ormQueryMetrics.sort(sortBy);
}
for (MetaOrmQueryMetric metric : ormQueryMetrics) {
logQuery(metric);
}
}
for (MetaCountMetric metric : countMetrics) {
logCount(metric);
}
}
List<MetaOrmQueryMetric> ormQueryMetrics = serverMetrics.getOrmQueryMetrics();
if (!ormQueryMetrics.isEmpty()) {
if (sortBy != null) {
ormQueryMetrics.sort(sortBy);
}
for (MetaOrmQueryMetric metric : ormQueryMetrics) {
logQuery(metric);
List<MetaQueryMetric> dtoQueryMetrics = serverMetrics.getDtoQueryMetrics();
if (!dtoQueryMetrics.isEmpty()) {
if (sortBy != null) {
dtoQueryMetrics.sort(sortBy);
}
for (MetaQueryMetric metric : dtoQueryMetrics) {
logDtoQuery(metric);
}
}
end();
} catch (IOException e) {
throw new RuntimeException("Error writing metrics as JSON", e);
}
List<MetaQueryMetric> dtoQueryMetrics = serverMetrics.getDtoQueryMetrics();
if (!dtoQueryMetrics.isEmpty()) {
if (sortBy != null) {
dtoQueryMetrics.sort(sortBy);
}
for (MetaQueryMetric metric : dtoQueryMetrics) {
logDtoQuery(metric);
}
}
end();
}
private void start() {
objStart();
key("db");
val(database.getName());
key("metrics");
listStart();
private void start() throws IOException {
if (withHeader) {
objStart();
key("db");
val(database.getName());
key("metrics");
listStart();
}
}
private void end() {
listEnd();
objEnd();
private void end() throws IOException {
if (withHeader) {
listEnd();
objEnd();
}
}
private void objStart() {
private void objStart() throws IOException {
objKeyCounter = 0;
writer.append("{");
}
private void objEnd() {
private void objEnd() throws IOException {
writer.append("}");
}
private void listStart() {
private void listStart() throws IOException {
listCounter = 0;
writer.append("[");
writer.append(newLine);
}
private void listEnd() {
private void listEnd() throws IOException {
writer.append("]");
}
private void key(String key) {
private void key(String key) throws IOException {
if (objKeyCounter++ > 0) {
writer.append(", ");
}
writer.append("\"").append(key).append("\":");
}
private void val(long count) {
private void val(long count) throws IOException {
writer.append(Long.toString(count));
}
private void val(String val) {
private void val(String val) throws IOException {
writer.append("\"").append(val).append("\"");
}
private void metricStart(MetaMetric metric) {
private void metricStart(MetaMetric metric) throws IOException {
if (listCounter++ > 0) {
writer.append(",").append(newLine);
}
@@ -167,11 +189,11 @@ class DumpMetricsJson implements ServerMetricsAsJson {
val(metric.getMetricType().name());
}
private void metricEnd() {
private void metricEnd() throws IOException {
objEnd();
}
private void log(MetaTimedMetric metric) {
private void log(MetaTimedMetric metric) throws IOException {
metricStart(metric);
appendCounters(metric);
if (withLocation) {
@@ -180,14 +202,14 @@ class DumpMetricsJson implements ServerMetricsAsJson {
metricEnd();
}
private void logCount(MetaCountMetric metric) {
private void logCount(MetaCountMetric metric) throws IOException {
metricStart(metric);
key("count");
val(metric.getCount());
metricEnd();
}
private void logQuery(MetaOrmQueryMetric metric) {
private void logQuery(MetaOrmQueryMetric metric) throws IOException {
metricStart(metric);
appendCounters(metric);
if (withHash) {
@@ -198,14 +220,14 @@ class DumpMetricsJson implements ServerMetricsAsJson {
metricEnd();
}
private void logDtoQuery(MetaQueryMetric metric) {
private void logDtoQuery(MetaQueryMetric metric) throws IOException {
metricStart(metric);
appendCounters(metric);
appendLocationAndSql(metric);
metricEnd();
}
private void appendLocationAndSql(MetaQueryMetric metric) {
private void appendLocationAndSql(MetaQueryMetric metric) throws IOException {
if (withLocation) {
appendLocation(metric.getLocation());
}
@@ -215,14 +237,14 @@ class DumpMetricsJson implements ServerMetricsAsJson {
}
}
private void appendLocation(String location) {
private void appendLocation(String location) throws IOException {
if (location != null) {
key("loc");
val(location);
}
}
private void appendCounters(MetaTimedMetric timedMetric) {
private void appendCounters(MetaTimedMetric timedMetric) throws IOException {
key("count");
val(timedMetric.getCount());
key("total");