Skip to content

Commit 3092969

Browse files
committed
Tidy up refactor to use record types for MetricData etc
1 parent 009a910 commit 3092969

27 files changed

Lines changed: 455 additions & 449 deletions

server/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@
5757
<version>${avaje-jsonb.version}</version>
5858
</dependency>
5959

60+
<dependency>
61+
<groupId>io.avaje</groupId>
62+
<artifactId>avaje-record-builder</artifactId>
63+
<version>${avaje-record-builder.version}</version>
64+
<scope>provided</scope>
65+
</dependency>
66+
6067
<dependency>
6168
<groupId>io.avaje</groupId>
6269
<artifactId>avaje-http-api</artifactId>
@@ -197,6 +204,11 @@
197204
<artifactId>avaje-jsonb-generator</artifactId>
198205
<version>${avaje-jsonb.version}</version>
199206
</path>
207+
<path>
208+
<groupId>io.avaje</groupId>
209+
<artifactId>avaje-record-builder</artifactId>
210+
<version>${avaje-record-builder.version}</version>
211+
</path>
200212
<path>
201213
<groupId>io.avaje</groupId>
202214
<artifactId>avaje-http-jex-generator</artifactId>
Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,47 @@
11
package org.ebean.monitor.api;
22

33
import io.avaje.jsonb.Json;
4+
import io.avaje.recordbuilder.RecordBuilder;
45

56
/**
67
* A timed metric with combined count, mean, max and total.
78
* <p>
89
* Times are generally expected to be in microseconds.
910
* </p>
11+
*
12+
* @param name The metric name.
13+
* @param hash Optional hash used to identify a query metric.
14+
* @param loc Optional location attribute. Code location of query or {@code @Transactional}.
15+
* @param sql SQL only supplied for SQL query metrics.
16+
* @param tags Optional canonical tags for v2 payloads, as a sorted delimited
17+
* {@code "key:value,key2:value2"} string (e.g.
18+
* {@code "kind:orm,label:Customer.findList,type:Customer"}). Null/absent for
19+
* legacy v1 payloads.
20+
* @param count Timed metric count.
21+
* @param mean Timed metric mean.
22+
* @param max Timed metric max.
23+
* @param total Timed metric total.
24+
* @param value Non timed metrics just use the value attribute.
1025
*/
26+
@RecordBuilder
1127
@Json
12-
public class MetricData {
28+
public record MetricData(
29+
String name,
30+
String hash,
31+
String loc,
32+
String sql,
33+
String tags,
34+
Long count,
35+
Long mean,
36+
Long max,
37+
Long total,
38+
Double value
39+
) {
1340

1441
/**
15-
* The metric name.
42+
* Create a new builder.
1643
*/
17-
public String name;
18-
19-
/**
20-
* Optional hash used to identify a query metric.
21-
*/
22-
public String hash;
23-
24-
/**
25-
* Optional location attribute. Code location of query or @Transactional.
26-
*/
27-
public String loc;
28-
29-
/**
30-
* SQL only supplied for SQL query metrics.
31-
*/
32-
public String sql;
33-
34-
/**
35-
* Optional canonical tags for v2 payloads, as a sorted delimited
36-
* {@code "key:value,key2:value2"} string (e.g. {@code "kind:orm,label:Customer.findList,type:Customer"}).
37-
* <p>
38-
* Null/absent for legacy v1 payloads. Parsing only at this stage — not yet
39-
* stored or used for metric identity.
40-
*/
41-
public String tags;
42-
43-
/**
44-
* Timed metrics have attributes of count, mean, max and total.
45-
*/
46-
public Long count;
47-
public Long mean;
48-
public Long max;
49-
public Long total;
50-
51-
/**
52-
* Non timed metrics just use the value attribute.
53-
*/
54-
public Double value;
44+
public static MetricDataBuilder builder() {
45+
return MetricDataBuilder.builder();
46+
}
5547
}
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
package org.ebean.monitor.api;
22

33
import io.avaje.jsonb.Json;
4+
import io.avaje.recordbuilder.RecordBuilder;
45

56
import java.util.ArrayList;
67
import java.util.List;
78

89
/**
910
* Database metrics.
11+
*
12+
* @param db The Db name.
13+
* @param metrics The metrics for the database.
1014
*/
15+
@RecordBuilder
1116
@Json
12-
public class MetricDbData {
17+
public record MetricDbData(
18+
String db,
19+
List<MetricData> metrics
20+
) {
1321

14-
/**
15-
* The Db name.
16-
*/
17-
public String db;
22+
public MetricDbData {
23+
metrics = (metrics == null) ? new ArrayList<>() : metrics;
24+
}
1825

1926
/**
20-
* The metrics for the database.
27+
* Create a new builder.
2128
*/
22-
public List<MetricData> metrics = new ArrayList<>();
29+
public static MetricDbDataBuilder builder() {
30+
return MetricDbDataBuilder.builder();
31+
}
2332
}
Lines changed: 48 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.ebean.monitor.api;
22

33
import io.avaje.jsonb.Json;
4+
import io.avaje.recordbuilder.RecordBuilder;
45

56
import java.util.ArrayList;
67
import java.util.LinkedHashMap;
@@ -9,69 +10,57 @@
910

1011
/**
1112
* Metrics to ingest.
13+
*
14+
* @param v Metric payload format version. {@code 0}/absent means legacy v1 (flat
15+
* names, no tags); {@code 2} means v2 (canonical family {@code name} +
16+
* delimited {@code tags} string on each {@link MetricData}).
17+
* @param startEventTime Optional start of the metric window provided by the client (epoch
18+
* millis, typically the {@code eventTime} of the prior successful send,
19+
* or the reporter start time on the first send). When non-zero, the OTLP
20+
* forwarder uses this as {@code startTimeUnixNano} so consecutive deltas
21+
* are truly disjoint and survive
22+
* {@code otelcol.processor.deltatocumulative}.
23+
* @param eventTime The metric collection time.
24+
* @param appName The name of the application the metrics are collected for.
25+
* @param version The application version. Used to determine when new versions are released.
26+
* @param environment The name of the environment the metrics are collected for.
27+
* @param instanceId An Id for the server instance (eg. Kubernetes pod name).
28+
* @param metrics General metrics for the application (Rest endpoints, JVM metrics,
29+
* CGroup metrics etc).
30+
* @param dbs The database metrics.
31+
* @param resAttrs Optional extra resource attributes — typically parsed from the standard
32+
* {@code OTEL_RESOURCE_ATTRIBUTES} env var by the client. Forwarded
33+
* verbatim to the OTLP {@code resource.attributes} list. The reserved
34+
* per-app fields ({@code service.name}, {@code service.version},
35+
* {@code service.instance.id}, {@code deployment.environment.name})
36+
* sourced from the dedicated header fields above take precedence over any
37+
* matching keys here.
1238
*/
39+
@RecordBuilder
1340
@Json
14-
public class MetricRequest {
41+
public record MetricRequest(
42+
int v,
43+
long startEventTime,
44+
long eventTime,
45+
String appName,
46+
String version,
47+
String environment,
48+
String instanceId,
49+
List<MetricData> metrics,
50+
List<MetricDbData> dbs,
51+
Map<String, String> resAttrs
52+
) {
1553

16-
/**
17-
* Metric payload format version. {@code 0}/absent means legacy v1 (flat names,
18-
* no tags); {@code 2} means v2 (canonical family {@code name} + delimited
19-
* {@code tags} string on each {@link MetricData}). Parsing only at this stage.
20-
*/
21-
public int v;
22-
23-
/**
24-
* Optional start of the metric window provided by the client (epoch millis,
25-
* typically the {@code eventTime} of the prior successful send, or the
26-
* reporter start time on the first send). When non-zero, the OTLP forwarder
27-
* uses this as {@code startTimeUnixNano} so consecutive deltas are truly
28-
* disjoint and survive {@code otelcol.processor.deltatocumulative}.
29-
*/
30-
public long startEventTime;
31-
32-
/**
33-
* The metric collection time.
34-
*/
35-
public long eventTime;
36-
37-
/**
38-
* The name of the application the metrics are collected for.
39-
*/
40-
public String appName;
41-
42-
/**
43-
* The application version. Used to determine when new versions are released.
44-
*/
45-
public String version;
54+
public MetricRequest {
55+
metrics = (metrics == null) ? new ArrayList<>() : metrics;
56+
dbs = (dbs == null) ? new ArrayList<>() : dbs;
57+
resAttrs = (resAttrs == null) ? new LinkedHashMap<>() : resAttrs;
58+
}
4659

4760
/**
48-
* The name of the environment the metrics are collected for.
61+
* Create a new builder.
4962
*/
50-
public String environment;
51-
52-
/**
53-
* An Id for the server instance (eg. Kubernetes pod name).
54-
*/
55-
public String instanceId;
56-
57-
/**
58-
* General metrics for the application (Rest endpoints, JVM metrics, CGroup metrics etc).
59-
*/
60-
public List<MetricData> metrics = new ArrayList<>();
61-
62-
/**
63-
* The database metrics.
64-
*/
65-
public List<MetricDbData> dbs = new ArrayList<>();
66-
67-
/**
68-
* Optional extra resource attributes — typically parsed from the standard
69-
* {@code OTEL_RESOURCE_ATTRIBUTES} env var by the client. Forwarded verbatim
70-
* to the OTLP {@code resource.attributes} list. The reserved per-app fields
71-
* ({@code service.name}, {@code service.version}, {@code service.instance.id},
72-
* {@code deployment.environment.name}) sourced from the dedicated header
73-
* fields above take precedence over any matching keys here.
74-
*/
75-
public Map<String, String> resAttrs = new LinkedHashMap<>();
76-
63+
public static MetricRequestBuilder builder() {
64+
return MetricRequestBuilder.builder();
65+
}
7766
}

0 commit comments

Comments
 (0)