mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-22 15:10:02 +00:00
#2760 - Add ServerMetrics.asJson() ... to support conversion of collected metrics to json
This commit is contained in:
1 parent
7474430a63
commit
01d5aa8199
6 files changed
+41
-92
No files matched your search
@@ -37,6 +37,11 @@ public class BasicMetricVisitor extends AbstractMetricVisitor implements ServerM
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServerMetricsAsJson asJson() {
|
||||||
|
return new MetricsAsJson(this);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<MetaTimedMetric> timedMetrics() {
|
public List<MetaTimedMetric> timedMetrics() {
|
||||||
return timed;
|
return timed;
|
||||||
|
|||||||
@@ -16,17 +16,6 @@ public interface MetaInfoManager {
|
|||||||
*/
|
*/
|
||||||
ServerMetrics collectMetrics();
|
ServerMetrics collectMetrics();
|
||||||
|
|
||||||
/**
|
|
||||||
* Given the already collected metrics provide them in json form.
|
|
||||||
* <p>
|
|
||||||
* Expected to be used when we wish to collect the metrics and report them
|
|
||||||
* to some service and additionally format them as json for say output into
|
|
||||||
* an application log.
|
|
||||||
*
|
|
||||||
* @param metrics The already collected metrics
|
|
||||||
*/
|
|
||||||
ServerMetricsAsJson metricsAsJson(ServerMetrics metrics);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Collect the metrics in raw JSON form.
|
* Collect the metrics in raw JSON form.
|
||||||
* <pre>{@code
|
* <pre>{@code
|
||||||
|
|||||||
+10
-33
@@ -1,24 +1,13 @@
|
|||||||
package io.ebeaninternal.server.core;
|
package io.ebean.meta;
|
||||||
|
|
||||||
import io.ebean.Database;
|
|
||||||
import io.ebean.meta.MetaCountMetric;
|
|
||||||
import io.ebean.meta.MetaMetric;
|
|
||||||
import io.ebean.meta.MetaQueryMetric;
|
|
||||||
import io.ebean.meta.MetaTimedMetric;
|
|
||||||
import io.ebean.meta.ServerMetrics;
|
|
||||||
import io.ebean.meta.ServerMetricsAsJson;
|
|
||||||
import io.ebean.meta.SortMetric;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
final class DumpMetricsJson implements ServerMetricsAsJson {
|
final class MetricsAsJson implements ServerMetricsAsJson {
|
||||||
|
|
||||||
private final Database database;
|
|
||||||
private final ServerMetrics metrics;
|
private final ServerMetrics metrics;
|
||||||
private final String name;
|
|
||||||
private Appendable writer;
|
private Appendable writer;
|
||||||
/**
|
/**
|
||||||
* By default, include sql and location attributes for the initial collection only.
|
* By default, include sql and location attributes for the initial collection only.
|
||||||
@@ -31,16 +20,8 @@ final class DumpMetricsJson implements ServerMetricsAsJson {
|
|||||||
private int listCounter;
|
private int listCounter;
|
||||||
private int objKeyCounter;
|
private int objKeyCounter;
|
||||||
|
|
||||||
DumpMetricsJson(Database database) {
|
MetricsAsJson(ServerMetrics metrics) {
|
||||||
this.database = database;
|
|
||||||
this.name = database.name();
|
|
||||||
this.metrics = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
DumpMetricsJson(ServerMetrics metrics) {
|
|
||||||
this.database = null;
|
|
||||||
this.metrics = metrics;
|
this.metrics = metrics;
|
||||||
this.name = metrics.name();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -76,28 +57,24 @@ final class DumpMetricsJson implements ServerMetricsAsJson {
|
|||||||
@Override
|
@Override
|
||||||
public String json() {
|
public String json() {
|
||||||
writer = new StringWriter();
|
writer = new StringWriter();
|
||||||
collect(obtainMetrics());
|
collect();
|
||||||
return writer.toString();
|
return writer.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(Appendable buffer) {
|
public void write(Appendable buffer) {
|
||||||
writer = buffer;
|
writer = buffer;
|
||||||
collect(obtainMetrics());
|
collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ServerMetrics obtainMetrics() {
|
private void collect() {
|
||||||
return metrics != null ? metrics : database.metaInfo().collectMetrics();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void collect(ServerMetrics serverMetrics) {
|
|
||||||
try {
|
try {
|
||||||
start();
|
start();
|
||||||
for (MetaTimedMetric metric : serverMetrics.timedMetrics()) {
|
for (MetaTimedMetric metric : metrics.timedMetrics()) {
|
||||||
logTimed(metric);
|
logTimed(metric);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<MetaCountMetric> countMetrics = serverMetrics.countMetrics();
|
List<MetaCountMetric> countMetrics = metrics.countMetrics();
|
||||||
if (!countMetrics.isEmpty()) {
|
if (!countMetrics.isEmpty()) {
|
||||||
if (sortBy != null) {
|
if (sortBy != null) {
|
||||||
countMetrics.sort(SortMetric.COUNT_NAME);
|
countMetrics.sort(SortMetric.COUNT_NAME);
|
||||||
@@ -107,7 +84,7 @@ final class DumpMetricsJson implements ServerMetricsAsJson {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List<MetaQueryMetric> queryMetrics = serverMetrics.queryMetrics();
|
List<MetaQueryMetric> queryMetrics = metrics.queryMetrics();
|
||||||
if (!queryMetrics.isEmpty()) {
|
if (!queryMetrics.isEmpty()) {
|
||||||
if (sortBy != null) {
|
if (sortBy != null) {
|
||||||
queryMetrics.sort(sortBy);
|
queryMetrics.sort(sortBy);
|
||||||
@@ -126,7 +103,7 @@ final class DumpMetricsJson implements ServerMetricsAsJson {
|
|||||||
if (withHeader) {
|
if (withHeader) {
|
||||||
objStart();
|
objStart();
|
||||||
key("db");
|
key("db");
|
||||||
val(name);
|
val(metrics.name());
|
||||||
key("metrics");
|
key("metrics");
|
||||||
listStart();
|
listStart();
|
||||||
}
|
}
|
||||||
@@ -12,6 +12,11 @@ public interface ServerMetrics {
|
|||||||
*/
|
*/
|
||||||
String name();
|
String name();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return ServerMetricsAsJson to build the metrics as JSON content.
|
||||||
|
*/
|
||||||
|
ServerMetricsAsJson asJson();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return timed metrics for Transactions, labelled SqlQuery, labelled SqlUpdate.
|
* Return timed metrics for Transactions, labelled SqlQuery, labelled SqlUpdate.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -47,14 +47,9 @@ final class DefaultMetaInfoManager implements MetaInfoManager {
|
|||||||
return visitBasic();
|
return visitBasic();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public ServerMetricsAsJson metricsAsJson(ServerMetrics metrics) {
|
|
||||||
return new DumpMetricsJson(metrics);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ServerMetricsAsJson collectMetricsAsJson() {
|
public ServerMetricsAsJson collectMetricsAsJson() {
|
||||||
return new DumpMetricsJson(server);
|
return collectMetrics().asJson();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class DtoQueryTest extends BaseTestCase {
|
class DtoQueryTest extends BaseTestCase {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(DtoQueryTest.class);
|
private static final Logger log = LoggerFactory.getLogger(DtoQueryTest.class);
|
||||||
|
|
||||||
@@ -32,8 +32,7 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
private final AtomicInteger rowCount = new AtomicInteger();
|
private final AtomicInteger rowCount = new AtomicInteger();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dto_findList_constructorMatch() {
|
void dto_findList_constructorMatch() {
|
||||||
|
|
||||||
ResetBasicData.reset();
|
ResetBasicData.reset();
|
||||||
resetAllMetrics();
|
resetAllMetrics();
|
||||||
|
|
||||||
@@ -57,8 +56,7 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dto_findEach_constructorMatch() {
|
void dto_findEach_constructorMatch() {
|
||||||
|
|
||||||
ResetBasicData.reset();
|
ResetBasicData.reset();
|
||||||
|
|
||||||
LoggedSql.start();
|
LoggedSql.start();
|
||||||
@@ -71,8 +69,7 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dto_findEachWhile_constructorMatch() {
|
void dto_findEachWhile_constructorMatch() {
|
||||||
|
|
||||||
ResetBasicData.reset();
|
ResetBasicData.reset();
|
||||||
|
|
||||||
LoggedSql.start();
|
LoggedSql.start();
|
||||||
@@ -93,7 +90,7 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dto_findEachBatch() {
|
void dto_findEachBatch() {
|
||||||
seedData(); // 15 rows inserted to fetch
|
seedData(); // 15 rows inserted to fetch
|
||||||
|
|
||||||
resetFindEachCounts();
|
resetFindEachCounts();
|
||||||
@@ -145,8 +142,7 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dto_findOneEmpty() {
|
void dto_findOneEmpty() {
|
||||||
|
|
||||||
ResetBasicData.reset();
|
ResetBasicData.reset();
|
||||||
|
|
||||||
Optional<DCust> rob = server().findDto(DCust.class, "select id, name from o_customer where name = :name")
|
Optional<DCust> rob = server().findDto(DCust.class, "select id, name from o_customer where name = :name")
|
||||||
@@ -163,8 +159,7 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dto_findOne() {
|
void dto_findOne() {
|
||||||
|
|
||||||
ResetBasicData.reset();
|
ResetBasicData.reset();
|
||||||
|
|
||||||
DCust fiona = server().findDto(DCust.class, "select id, name from o_customer where name = :name")
|
DCust fiona = server().findDto(DCust.class, "select id, name from o_customer where name = :name")
|
||||||
@@ -181,7 +176,7 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void setParameter() {
|
void setParameter() {
|
||||||
ResetBasicData.reset();
|
ResetBasicData.reset();
|
||||||
|
|
||||||
final List<DCust> list =
|
final List<DCust> list =
|
||||||
@@ -195,7 +190,7 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void setParameters() {
|
void setParameters() {
|
||||||
ResetBasicData.reset();
|
ResetBasicData.reset();
|
||||||
|
|
||||||
final List<DCust> list =
|
final List<DCust> list =
|
||||||
@@ -208,8 +203,7 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
|
|
||||||
@ForPlatform(Platform.POSTGRES)
|
@ForPlatform(Platform.POSTGRES)
|
||||||
@Test
|
@Test
|
||||||
public void dto_bindList_usingPostrgesAnyWithPositionedParameter() {
|
void dto_bindList_usingPostrgesAnyWithPositionedParameter() {
|
||||||
|
|
||||||
ResetBasicData.reset();
|
ResetBasicData.reset();
|
||||||
|
|
||||||
LoggedSql.start();
|
LoggedSql.start();
|
||||||
@@ -243,8 +237,7 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
|
|
||||||
@ForPlatform(Platform.POSTGRES)
|
@ForPlatform(Platform.POSTGRES)
|
||||||
@Test
|
@Test
|
||||||
public void sql_bindListParam_usingPostrgesAnyWithPositionedParameter() {
|
void sql_bindListParam_usingPostrgesAnyWithPositionedParameter() {
|
||||||
|
|
||||||
ResetBasicData.reset();
|
ResetBasicData.reset();
|
||||||
|
|
||||||
List<Integer> ids = Arrays.asList(1, 2);
|
List<Integer> ids = Arrays.asList(1, 2);
|
||||||
@@ -264,8 +257,7 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
|
|
||||||
@ForPlatform(Platform.POSTGRES)
|
@ForPlatform(Platform.POSTGRES)
|
||||||
@Test
|
@Test
|
||||||
public void sqlUpdate_bindListParam_usingPostrgesAnyWithPositionedParameter() {
|
void sqlUpdate_bindListParam_usingPostrgesAnyWithPositionedParameter() {
|
||||||
|
|
||||||
ResetBasicData.reset();
|
ResetBasicData.reset();
|
||||||
|
|
||||||
List<Integer> ids = Arrays.asList(999999999, 999999998);
|
List<Integer> ids = Arrays.asList(999999999, 999999998);
|
||||||
@@ -279,16 +271,12 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dto_queryPlanHits() {
|
void dto_queryPlanHits() {
|
||||||
|
|
||||||
ResetBasicData.reset();
|
ResetBasicData.reset();
|
||||||
|
|
||||||
resetAllMetrics();
|
resetAllMetrics();
|
||||||
|
|
||||||
String[] names = {"Rob", "Fiona", "Shrek"};
|
String[] names = {"Rob", "Fiona", "Shrek"};
|
||||||
|
|
||||||
for (String name : names) {
|
for (String name : names) {
|
||||||
|
|
||||||
List<DCust> custs = server().findDto(DCust.class, "select c3.id, c3.name from o_customer c3 where c3.name = :name")
|
List<DCust> custs = server().findDto(DCust.class, "select c3.id, c3.name from o_customer c3 where c3.name = :name")
|
||||||
.setLabel("basic")
|
.setLabel("basic")
|
||||||
.setParameter("name", name)
|
.setParameter("name", name)
|
||||||
@@ -309,7 +297,6 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
assertThat(queryMetric.count()).isEqualTo(3);
|
assertThat(queryMetric.count()).isEqualTo(3);
|
||||||
assertThat(queryMetric.name()).isEqualTo("dto.DCust_basic");
|
assertThat(queryMetric.name()).isEqualTo("dto.DCust_basic");
|
||||||
|
|
||||||
|
|
||||||
server().findDto(DCust.class, "select c4.id, c4.name from o_customer c4 where lower(c4.name) = :name")
|
server().findDto(DCust.class, "select c4.id, c4.name from o_customer c4 where lower(c4.name) = :name")
|
||||||
.setLabel("basic2")
|
.setLabel("basic2")
|
||||||
.setParameter("name", "rob")
|
.setParameter("name", "rob")
|
||||||
@@ -322,13 +309,12 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
|
|
||||||
log.info("stats " + stats);
|
log.info("stats " + stats);
|
||||||
|
|
||||||
String asJson = server().metaInfo().metricsAsJson(metric2).withHash(false).withNewLine(false).json();
|
String asJson = metric2.asJson().withHash(false).withNewLine(false).json();
|
||||||
assertThat(asJson).contains("dto.DCust_basic2");
|
assertThat(asJson).contains("dto.DCust_basic2");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dto_findList_relaxedMode() {
|
void dto_findList_relaxedMode() {
|
||||||
|
|
||||||
ResetBasicData.reset();
|
ResetBasicData.reset();
|
||||||
|
|
||||||
List<DCust3> list = server().findDto(DCust3.class, "select id, name, 42 as total, '42' as something_we_cannot_map from o_customer")
|
List<DCust3> list = server().findDto(DCust3.class, "select id, name, 42 as total, '42' as something_we_cannot_map from o_customer")
|
||||||
@@ -340,8 +326,7 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dto_findList_relaxedMode_defaultConstructor() {
|
void dto_findList_relaxedMode_defaultConstructor() {
|
||||||
|
|
||||||
ResetBasicData.reset();
|
ResetBasicData.reset();
|
||||||
|
|
||||||
List<DCust2> list = server().findDto(DCust2.class, "select id, '42' as something_we_cannot_map, name from o_customer")
|
List<DCust2> list = server().findDto(DCust2.class, "select id, '42' as something_we_cannot_map, name from o_customer")
|
||||||
@@ -353,8 +338,7 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dto_findList_constructorPlusMatch() {
|
void dto_findList_constructorPlusMatch() {
|
||||||
|
|
||||||
ResetBasicData.reset();
|
ResetBasicData.reset();
|
||||||
|
|
||||||
String sql = "select c.id, c.name, count(o.id) as totalOrders " +
|
String sql = "select c.id, c.name, count(o.id) as totalOrders " +
|
||||||
@@ -372,20 +356,17 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dto_findList_setters() {
|
void dto_findList_setters() {
|
||||||
|
|
||||||
ResetBasicData.reset();
|
ResetBasicData.reset();
|
||||||
|
|
||||||
DtoQuery<DCust2> dtoQuery = server().findDto(DCust2.class, "select id, name from o_customer");
|
DtoQuery<DCust2> dtoQuery = server().findDto(DCust2.class, "select id, name from o_customer");
|
||||||
|
|
||||||
List<DCust2> list = dtoQuery.findList();
|
List<DCust2> list = dtoQuery.findList();
|
||||||
|
|
||||||
assertThat(list).isNotEmpty();
|
assertThat(list).isNotEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dto3_findList_constructorMatch() {
|
void dto3_findList_constructorMatch() {
|
||||||
|
|
||||||
ResetBasicData.reset();
|
ResetBasicData.reset();
|
||||||
|
|
||||||
List<DCust3> robs = server().findDto(DCust3.class, "select id, name, 42 as totalOrders from o_customer where name like ?")
|
List<DCust3> robs = server().findDto(DCust3.class, "select id, name, 42 as totalOrders from o_customer where name like ?")
|
||||||
@@ -393,21 +374,18 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
.setMaxRows(10)
|
.setMaxRows(10)
|
||||||
.findList();
|
.findList();
|
||||||
|
|
||||||
|
|
||||||
log.info(robs.toString());
|
log.info(robs.toString());
|
||||||
assertThat(robs).isNotEmpty();
|
assertThat(robs).isNotEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dto3_findList_settersMatch() {
|
void dto3_findList_settersMatch() {
|
||||||
|
|
||||||
ResetBasicData.reset();
|
ResetBasicData.reset();
|
||||||
|
|
||||||
List<DCust3> robs = server().findDto(DCust3.class, "select id, name from o_customer where name = :name")
|
List<DCust3> robs = server().findDto(DCust3.class, "select id, name from o_customer where name = :name")
|
||||||
.setParameter("name", "Rob")
|
.setParameter("name", "Rob")
|
||||||
.findList();
|
.findList();
|
||||||
|
|
||||||
|
|
||||||
log.info(robs.toString());
|
log.info(robs.toString());
|
||||||
assertThat(robs).isNotEmpty();
|
assertThat(robs).isNotEmpty();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user