I’m using the InfluxDB Java Client to manage metrics in a Quarkus service.
For that I have created an extension that provides a dev container of InfluxDB 2.
When I start my tests everything works, but when I trigger the hot reload by saving any file the test, that should send the metric to the database fails.
Here is the test. The model is at the bottom:
package mypackage.models.measurements;
import static mypackage.config.InfluxDBConstants.DefaultConstants.BUCKET;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import com.influxdb.annotations.Column;
import com.influxdb.annotations.Measurement;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.domain.WritePrecision;
import myextension.quarkus.devservices.influxdb.runtime.InfluxDBConfig;
import mypackage.config.InfluxDBConstants.JobMetricConstants.Fields;
import mypackage.config.InfluxDBConstants.JobMetricConstants.Tags;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
/**
* JobMetricTest
*/
@QuarkusTest
@TestInstance(Lifecycle.PER_CLASS)
public class JobMetricTest {
@Inject
InfluxDBConfig config;
public InfluxDBClient getClient() {
InfluxDBClient client = InfluxDBClientFactory.create(
config.url(),
config.token().toCharArray(),
config.organization().get(),
config.bucket().get());
assertTrue(client.ping());
return client;
}
@Test
public void testInitialization() throws InterruptedException {
Instant testTime = Instant.now();
JobMetric metric = new JobMetric("testJob");
assertTrue(metric.duration == -1);
TimeUnit.MILLISECONDS.sleep(1);
metric.finish();
assertTrue(metric.duration > 0);
assertTrue(metric.job.equals("testJob"));
assertTrue(metric.start.getEpochSecond() >= testTime.getEpochSecond());
}
@Test
public void testInfluxReflection() throws InterruptedException {
InfluxDBClient client = getClient();
Metric metric = new Metric();
metric.job = "job";
metric.hasError = false;
metric.duration = Double.valueOf(-1);
metric.start = Instant.now();
Instant stop = Instant.now();
Duration d = Duration.between(metric.start, stop);
metric.duration = (double) d.getSeconds();
metric.duration += ((double) d.toMillisPart()) / 1000;
client.getWriteApiBlocking()
.writeMeasurement( // Line 72: Fails here!
BUCKET,
config.organization().get(),
WritePrecision.NS,
metric
);
}
}
@Measurement(name = "metric")
class Metric {
@Column(timestamp = true)
public Instant start;
@Column(tag = true, name = Tags.JOB)
public String job;
@Column(name = Fields.HAS_ERROR, tag = false)
public Boolean hasError;
@Column(name = Fields.DURATION, tag = false)
public Double duration;
}
Here is the exception:
2024-11-17 21:42:17,616 ERROR [io.qua.test] (Test runner thread) ==================== TEST REPORT #2 ==================== [Error Occurred After Shutdown]
2024-11-17 21:42:17,616 ERROR [io.qua.test] (Test runner thread) Test JobMetricTest#testInfluxReflextion() failed
[Error Occurred After Shutdown]: java.lang.IllegalArgumentException: Can not get java.lang.Double field mypackage.models.measurements.Metric.duration on mypackage.models.measurements.Metric
at java.base/jdk.internal.reflect.MethodHandleFieldAccessorImpl.newGetIllegalArgumentException(MethodHandleFieldAccessorImpl.java:86)
at java.base/jdk.internal.reflect.MethodHandleObjectFieldAccessorImpl.get(MethodHandleObjectFieldAccessorImpl.java:61)
at java.base/java.lang.reflect.Field.get(Field.java:444)
at com.influxdb.client.internal.MeasurementMapper.getObject(MeasurementMapper.java:130)
at com.influxdb.client.internal.MeasurementMapper.toPoint(MeasurementMapper.java:68)
at com.influxdb.client.internal.AbstractWriteClient$BatchWriteDataMeasurement.toLineProtocol(AbstractWriteClient.java:386)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:212)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:212)
at java.base/java.util.Collections$2.tryAdvance(Collections.java:5074)
at java.base/java.util.Collections$2.forEachRemaining(Collections.java:5082)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:556)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:546)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:265)
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:702)
at com.influxdb.client.internal.AbstractWriteBlockingClient.write(AbstractWriteBlockingClient.java:69)
at com.influxdb.client.internal.WriteApiBlockingImpl.writeMeasurements(WriteApiBlockingImpl.java:253)
at com.influxdb.client.internal.WriteApiBlockingImpl.writeMeasurement(WriteApiBlockingImpl.java:220)
at com.influxdb.client.internal.WriteApiBlockingImpl.writeMeasurement(WriteApiBlockingImpl.java:207)
at mypackage.models.measurements.JobMetricTest.testInfluxReflextion(JobMetricTest.java:72)
As I said a test rerun causes the exception, but stopping the tests and restarting them results in one positive test run until there is a rerun.
When I remove duration from Metric the same error occurs but with the job field.
I’m using Java 21, Quarkus 3.15 and InfluxDBClient 7.2.0