mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-22 15:10:02 +00:00
#2760 - Add MetaInfoManager.metricsAsJson() ... to support conversion of collected metrics to json
This commit is contained in:
1 parent
64311b2b9d
commit
bda0786924
9 files changed
+68
-17
No files matched your search
@@ -8,22 +8,33 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class BasicMetricVisitor extends AbstractMetricVisitor implements ServerMetrics {
|
public class BasicMetricVisitor extends AbstractMetricVisitor implements ServerMetrics {
|
||||||
|
|
||||||
|
private final String name;
|
||||||
private final List<MetaTimedMetric> timed = new ArrayList<>();
|
private final List<MetaTimedMetric> timed = new ArrayList<>();
|
||||||
private final List<MetaQueryMetric> query = new ArrayList<>();
|
private final List<MetaQueryMetric> query = new ArrayList<>();
|
||||||
private final List<MetaCountMetric> count = new ArrayList<>();
|
private final List<MetaCountMetric> count = new ArrayList<>();
|
||||||
|
|
||||||
|
public BasicMetricVisitor() {
|
||||||
|
this("db");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct to reset and collect everything.
|
* Construct to reset and collect everything.
|
||||||
*/
|
*/
|
||||||
public BasicMetricVisitor() {
|
public BasicMetricVisitor(String name) {
|
||||||
super(true, true, true, true);
|
this(name, true, true, true, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct specifying reset and what to collect.
|
* Construct specifying reset and what to collect.
|
||||||
*/
|
*/
|
||||||
public BasicMetricVisitor(boolean reset, boolean collectTransactionMetrics, boolean collectQueryMetrics, boolean collectL2Metrics) {
|
public BasicMetricVisitor(String name, boolean reset, boolean collectTransactionMetrics, boolean collectQueryMetrics, boolean collectL2Metrics) {
|
||||||
super(reset, collectTransactionMetrics, collectQueryMetrics, collectL2Metrics);
|
super(reset, collectTransactionMetrics, collectQueryMetrics, collectL2Metrics);
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String name() {
|
||||||
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -16,6 +16,17 @@ 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
|
||||||
|
|||||||
@@ -7,6 +7,11 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public interface ServerMetrics {
|
public interface ServerMetrics {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the name of the database these metrics were obtained for.
|
||||||
|
*/
|
||||||
|
String name();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return timed metrics for Transactions, labelled SqlQuery, labelled SqlUpdate.
|
* Return timed metrics for Transactions, labelled SqlQuery, labelled SqlUpdate.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -47,6 +47,11 @@ 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 new DumpMetricsJson(server);
|
||||||
@@ -59,7 +64,7 @@ final class DefaultMetaInfoManager implements MetaInfoManager {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BasicMetricVisitor visitBasic() {
|
public BasicMetricVisitor visitBasic() {
|
||||||
BasicMetricVisitor basic = new BasicMetricVisitor();
|
BasicMetricVisitor basic = new BasicMetricVisitor(server.name());
|
||||||
visitMetrics(basic);
|
visitMetrics(basic);
|
||||||
return basic;
|
return basic;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,17 +29,13 @@ final class DumpMetricsData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void collect(ServerMetrics serverMetrics) {
|
private void collect(ServerMetrics serverMetrics) {
|
||||||
final List<MetaTimedMetric> timedMetrics = serverMetrics.timedMetrics();
|
for (MetaTimedMetric metric : serverMetrics.timedMetrics()) {
|
||||||
final List<MetaCountMetric> countMetrics = serverMetrics.countMetrics();
|
|
||||||
final List<MetaQueryMetric> queryMetrics = serverMetrics.queryMetrics();
|
|
||||||
|
|
||||||
for (MetaTimedMetric metric : timedMetrics) {
|
|
||||||
add(metric);
|
add(metric);
|
||||||
}
|
}
|
||||||
for (MetaCountMetric metric : countMetrics) {
|
for (MetaCountMetric metric : serverMetrics.countMetrics()) {
|
||||||
addCount(metric);
|
addCount(metric);
|
||||||
}
|
}
|
||||||
for (MetaQueryMetric metric : queryMetrics) {
|
for (MetaQueryMetric metric : serverMetrics.queryMetrics()) {
|
||||||
addQuery(metric);
|
addQuery(metric);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,11 @@ import java.util.List;
|
|||||||
final class DumpMetricsJson implements ServerMetricsAsJson {
|
final class DumpMetricsJson implements ServerMetricsAsJson {
|
||||||
|
|
||||||
private final Database database;
|
private final Database database;
|
||||||
|
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.
|
||||||
*/
|
*/
|
||||||
private int includeExtraAttributes = 1;
|
private int includeExtraAttributes = 1;
|
||||||
private boolean withHeader = true;
|
private boolean withHeader = true;
|
||||||
@@ -31,6 +33,14 @@ final class DumpMetricsJson implements ServerMetricsAsJson {
|
|||||||
|
|
||||||
DumpMetricsJson(Database database) {
|
DumpMetricsJson(Database database) {
|
||||||
this.database = database;
|
this.database = database;
|
||||||
|
this.name = database.name();
|
||||||
|
this.metrics = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
DumpMetricsJson(ServerMetrics metrics) {
|
||||||
|
this.database = null;
|
||||||
|
this.metrics = metrics;
|
||||||
|
this.name = metrics.name();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -66,14 +76,18 @@ final class DumpMetricsJson implements ServerMetricsAsJson {
|
|||||||
@Override
|
@Override
|
||||||
public String json() {
|
public String json() {
|
||||||
writer = new StringWriter();
|
writer = new StringWriter();
|
||||||
collect(database.metaInfo().collectMetrics());
|
collect(obtainMetrics());
|
||||||
return writer.toString();
|
return writer.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(Appendable buffer) {
|
public void write(Appendable buffer) {
|
||||||
writer = buffer;
|
writer = buffer;
|
||||||
collect(database.metaInfo().collectMetrics());
|
collect(obtainMetrics());
|
||||||
|
}
|
||||||
|
|
||||||
|
private ServerMetrics obtainMetrics() {
|
||||||
|
return metrics != null ? metrics : database.metaInfo().collectMetrics();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void collect(ServerMetrics serverMetrics) {
|
private void collect(ServerMetrics serverMetrics) {
|
||||||
@@ -112,7 +126,7 @@ final class DumpMetricsJson implements ServerMetricsAsJson {
|
|||||||
if (withHeader) {
|
if (withHeader) {
|
||||||
objStart();
|
objStart();
|
||||||
key("db");
|
key("db");
|
||||||
val(database.name());
|
val(name);
|
||||||
key("metrics");
|
key("metrics");
|
||||||
listStart();
|
listStart();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -103,6 +103,13 @@
|
|||||||
|
|
||||||
<!-- Test dependencies -->
|
<!-- Test dependencies -->
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.avaje</groupId>
|
||||||
|
<artifactId>avaje-applog-slf4j</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.ebean</groupId>
|
<groupId>io.ebean</groupId>
|
||||||
<artifactId>ebean-platform-all</artifactId>
|
<artifactId>ebean-platform-all</artifactId>
|
||||||
|
|||||||
@@ -210,7 +210,7 @@ public class DtoQuery2Test extends BaseTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// collect without reset
|
// collect without reset
|
||||||
BasicMetricVisitor basic = new BasicMetricVisitor(false, true, true, true);
|
BasicMetricVisitor basic = new BasicMetricVisitor("db", false, true, true, true);
|
||||||
server().metaInfo().visitMetrics(basic);
|
server().metaInfo().visitMetrics(basic);
|
||||||
|
|
||||||
List<MetaQueryMetric> stats = basic.queryMetrics();
|
List<MetaQueryMetric> stats = basic.queryMetrics();
|
||||||
|
|||||||
@@ -298,7 +298,7 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// collect without reset
|
// collect without reset
|
||||||
BasicMetricVisitor basic = new BasicMetricVisitor(false, true, true, true);
|
BasicMetricVisitor basic = new BasicMetricVisitor("db", false, true, true, true);
|
||||||
server().metaInfo().visitMetrics(basic);
|
server().metaInfo().visitMetrics(basic);
|
||||||
|
|
||||||
List<MetaQueryMetric> stats = basic.queryMetrics();
|
List<MetaQueryMetric> stats = basic.queryMetrics();
|
||||||
@@ -322,6 +322,8 @@ public class DtoQueryTest extends BaseTestCase {
|
|||||||
|
|
||||||
log.info("stats " + stats);
|
log.info("stats " + stats);
|
||||||
|
|
||||||
|
String asJson = server().metaInfo().metricsAsJson(metric2).withHash(false).withNewLine(false).json();
|
||||||
|
assertThat(asJson).contains("dto.DCust_basic2");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in new issue
Block a user