我正在使用 InfluxDB Java 客户端来管理 Quarkus 服务中的指标。 为此,我创建了一个扩展,提供 InfluxDB 2 的开发容器。
当我开始测试时,一切正常,但是当我通过保存测试的任何文件来触发热重载时,应该将指标发送到数据库失败。
这是测试。模型在底部:
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;
}
这里是例外:
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)
正如我所说,测试重新运行会导致异常,但是停止测试并重新启动它们会导致一次积极的测试运行,直到重新运行为止。
当我从指标中删除持续时间时,会发生相同的错误,但在工作字段中。
我正在使用 Java 21、Quarkus 3.15 和 InfluxDBClient 7.2.0
Java 中的热重载有一些限制。
需要使用JRebel或Hotswap Agent等工具来提高Java热插拔能力。