Refactor DbConnectionPoolMetricsAssertions (#6101)
* refactor the DbConnectionPoolMetricsAssertions for improved readability and code reuse * Middle ground option * factor out duplicate verification * address PR comments. Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
This commit is contained in:
parent
09457bd431
commit
30710ba294
|
@ -7,12 +7,20 @@ package io.opentelemetry.instrumentation.testing.junit.db;
|
||||||
|
|
||||||
import static io.opentelemetry.api.common.AttributeKey.stringKey;
|
import static io.opentelemetry.api.common.AttributeKey.stringKey;
|
||||||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
|
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
|
||||||
|
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
|
||||||
|
|
||||||
|
import io.opentelemetry.api.common.AttributeKey;
|
||||||
import io.opentelemetry.api.common.Attributes;
|
import io.opentelemetry.api.common.Attributes;
|
||||||
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
|
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
|
||||||
|
import io.opentelemetry.sdk.metrics.data.MetricData;
|
||||||
|
import io.opentelemetry.sdk.testing.assertj.LongSumAssert;
|
||||||
|
import io.opentelemetry.sdk.testing.assertj.MetricAssert;
|
||||||
|
|
||||||
public final class DbConnectionPoolMetricsAssertions {
|
public final class DbConnectionPoolMetricsAssertions {
|
||||||
|
|
||||||
|
private static final AttributeKey<String> POOL_NAME_KEY = stringKey("pool.name");
|
||||||
|
private static final AttributeKey<String> STATE_KEY = stringKey("state");
|
||||||
|
|
||||||
public static DbConnectionPoolMetricsAssertions create(
|
public static DbConnectionPoolMetricsAssertions create(
|
||||||
InstrumentationExtension testing, String instrumentationName, String poolName) {
|
InstrumentationExtension testing, String instrumentationName, String poolName) {
|
||||||
return new DbConnectionPoolMetricsAssertions(testing, instrumentationName, poolName);
|
return new DbConnectionPoolMetricsAssertions(testing, instrumentationName, poolName);
|
||||||
|
@ -73,181 +81,184 @@ public final class DbConnectionPoolMetricsAssertions {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void assertConnectionPoolEmitsMetrics() {
|
public void assertConnectionPoolEmitsMetrics() {
|
||||||
|
verifyConnectionUsage();
|
||||||
|
if (testMinIdleConnections) {
|
||||||
|
verifyMinIdleConnections();
|
||||||
|
}
|
||||||
|
if (testMaxIdleConnections) {
|
||||||
|
verifyMaxIdleConnections();
|
||||||
|
}
|
||||||
|
verifyMaxConnections();
|
||||||
|
if (testPendingRequests) {
|
||||||
|
verifyPendingRequests();
|
||||||
|
}
|
||||||
|
if (testConnectionTimeouts) {
|
||||||
|
verifyTimeouts();
|
||||||
|
}
|
||||||
|
if (testCreateTime) {
|
||||||
|
verifyCreateTime();
|
||||||
|
}
|
||||||
|
if (testWaitTime) {
|
||||||
|
verifyWaitTime();
|
||||||
|
}
|
||||||
|
if (testUseTime) {
|
||||||
|
verifyUseTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void verifyConnectionUsage() {
|
||||||
testing.waitAndAssertMetrics(
|
testing.waitAndAssertMetrics(
|
||||||
instrumentationName,
|
instrumentationName,
|
||||||
"db.client.connections.usage",
|
"db.client.connections.usage",
|
||||||
metrics ->
|
metrics -> metrics.anySatisfy(this::verifyUsageMetric));
|
||||||
metrics.anySatisfy(
|
}
|
||||||
metric ->
|
|
||||||
assertThat(metric)
|
private void verifyUsageMetric(MetricData metric) {
|
||||||
.hasUnit("connections")
|
assertThat(metric)
|
||||||
.hasDescription(
|
.hasUnit("connections")
|
||||||
"The number of connections that are currently in state described by the state attribute.")
|
.hasDescription(
|
||||||
.hasLongSumSatisfying(
|
"The number of connections that are currently in state described by the state attribute.")
|
||||||
sum ->
|
.hasLongSumSatisfying(
|
||||||
sum.isNotMonotonic()
|
sum ->
|
||||||
.hasPointsSatisfying(
|
sum.isNotMonotonic()
|
||||||
point ->
|
.hasPointsSatisfying(
|
||||||
point.hasAttributes(
|
point ->
|
||||||
Attributes.of(
|
point.hasAttributesSatisfying(
|
||||||
stringKey("pool.name"),
|
equalTo(POOL_NAME_KEY, poolName), equalTo(STATE_KEY, "idle")),
|
||||||
poolName,
|
point ->
|
||||||
stringKey("state"),
|
point.hasAttributesSatisfying(
|
||||||
"idle")),
|
equalTo(POOL_NAME_KEY, poolName), equalTo(STATE_KEY, "used"))));
|
||||||
point ->
|
}
|
||||||
point.hasAttributes(
|
|
||||||
Attributes.of(
|
private void verifyMaxConnections() {
|
||||||
stringKey("pool.name"),
|
|
||||||
poolName,
|
|
||||||
stringKey("state"),
|
|
||||||
"used"))))));
|
|
||||||
if (testMinIdleConnections) {
|
|
||||||
testing.waitAndAssertMetrics(
|
|
||||||
instrumentationName,
|
|
||||||
"db.client.connections.idle.min",
|
|
||||||
metrics ->
|
|
||||||
metrics.anySatisfy(
|
|
||||||
metric ->
|
|
||||||
assertThat(metric)
|
|
||||||
.hasUnit("connections")
|
|
||||||
.hasDescription("The minimum number of idle open connections allowed.")
|
|
||||||
.hasLongSumSatisfying(
|
|
||||||
sum ->
|
|
||||||
sum.isNotMonotonic()
|
|
||||||
.hasPointsSatisfying(
|
|
||||||
point ->
|
|
||||||
point.hasAttributes(
|
|
||||||
Attributes.of(
|
|
||||||
stringKey("pool.name"), poolName))))));
|
|
||||||
}
|
|
||||||
if (testMaxIdleConnections) {
|
|
||||||
testing.waitAndAssertMetrics(
|
|
||||||
instrumentationName,
|
|
||||||
"db.client.connections.idle.max",
|
|
||||||
metrics ->
|
|
||||||
metrics.anySatisfy(
|
|
||||||
metric ->
|
|
||||||
assertThat(metric)
|
|
||||||
.hasUnit("connections")
|
|
||||||
.hasDescription("The maximum number of idle open connections allowed.")
|
|
||||||
.hasLongSumSatisfying(
|
|
||||||
sum ->
|
|
||||||
sum.isNotMonotonic()
|
|
||||||
.hasPointsSatisfying(
|
|
||||||
point ->
|
|
||||||
point.hasAttributes(
|
|
||||||
Attributes.of(
|
|
||||||
stringKey("pool.name"), poolName))))));
|
|
||||||
}
|
|
||||||
testing.waitAndAssertMetrics(
|
testing.waitAndAssertMetrics(
|
||||||
instrumentationName,
|
instrumentationName,
|
||||||
"db.client.connections.max",
|
"db.client.connections.max",
|
||||||
metrics ->
|
metrics -> metrics.anySatisfy(this::verifyMaxConnectionsMetric));
|
||||||
metrics.anySatisfy(
|
}
|
||||||
metric ->
|
|
||||||
assertThat(metric)
|
private void verifyMaxConnectionsMetric(MetricData metric) {
|
||||||
.hasUnit("connections")
|
assertThat(metric)
|
||||||
.hasDescription("The maximum number of open connections allowed.")
|
.hasUnit("connections")
|
||||||
.hasLongSumSatisfying(
|
.hasDescription("The maximum number of open connections allowed.")
|
||||||
sum ->
|
.hasLongSumSatisfying(this::verifyPoolName);
|
||||||
sum.isNotMonotonic()
|
}
|
||||||
.hasPointsSatisfying(
|
|
||||||
point ->
|
private void verifyMinIdleConnections() {
|
||||||
point.hasAttributes(
|
testing.waitAndAssertMetrics(
|
||||||
Attributes.of(
|
instrumentationName,
|
||||||
stringKey("pool.name"), poolName))))));
|
"db.client.connections.idle.min",
|
||||||
if (testPendingRequests) {
|
metrics -> metrics.anySatisfy(this::verifyMinIdleConnectionsMetric));
|
||||||
testing.waitAndAssertMetrics(
|
}
|
||||||
instrumentationName,
|
|
||||||
"db.client.connections.pending_requests",
|
private void verifyMinIdleConnectionsMetric(MetricData metric) {
|
||||||
metrics ->
|
assertThat(metric)
|
||||||
metrics.anySatisfy(
|
.hasUnit("connections")
|
||||||
metric ->
|
.hasDescription("The minimum number of idle open connections allowed.")
|
||||||
assertThat(metric)
|
.hasLongSumSatisfying(this::verifyPoolName);
|
||||||
.hasUnit("requests")
|
}
|
||||||
.hasDescription(
|
|
||||||
"The number of pending requests for an open connection, cumulative for the entire pool.")
|
private void verifyMaxIdleConnections() {
|
||||||
.hasLongSumSatisfying(
|
testing.waitAndAssertMetrics(
|
||||||
sum ->
|
instrumentationName,
|
||||||
sum.isNotMonotonic()
|
"db.client.connections.idle.max",
|
||||||
.hasPointsSatisfying(
|
metrics -> metrics.anySatisfy(this::verifyMaxIdleConnectionsMetric));
|
||||||
point ->
|
}
|
||||||
point.hasAttributes(
|
|
||||||
Attributes.of(
|
private void verifyMaxIdleConnectionsMetric(MetricData metric) {
|
||||||
stringKey("pool.name"), poolName))))));
|
assertThat(metric)
|
||||||
}
|
.hasUnit("connections")
|
||||||
if (testConnectionTimeouts) {
|
.hasDescription("The maximum number of idle open connections allowed.")
|
||||||
testing.waitAndAssertMetrics(
|
.hasLongSumSatisfying(this::verifyPoolName);
|
||||||
instrumentationName,
|
}
|
||||||
"db.client.connections.timeouts",
|
|
||||||
metrics ->
|
private void verifyPoolName(LongSumAssert sum) {
|
||||||
metrics.anySatisfy(
|
sum.isNotMonotonic()
|
||||||
metric ->
|
.hasPointsSatisfying(point -> point.hasAttributes(Attributes.of(POOL_NAME_KEY, poolName)));
|
||||||
assertThat(metric)
|
}
|
||||||
.hasUnit("timeouts")
|
|
||||||
.hasDescription(
|
private void verifyPendingRequests() {
|
||||||
"The number of connection timeouts that have occurred trying to obtain a connection from the pool.")
|
testing.waitAndAssertMetrics(
|
||||||
.hasLongSumSatisfying(
|
instrumentationName,
|
||||||
sum ->
|
"db.client.connections.pending_requests",
|
||||||
sum.isMonotonic()
|
metrics -> metrics.anySatisfy(this::verifyPendingRequestsMetric));
|
||||||
.hasPointsSatisfying(
|
}
|
||||||
point ->
|
|
||||||
point.hasAttributes(
|
private void verifyPendingRequestsMetric(MetricData metric) {
|
||||||
Attributes.of(
|
assertThat(metric)
|
||||||
stringKey("pool.name"), poolName))))));
|
.hasUnit("requests")
|
||||||
}
|
.hasDescription(
|
||||||
if (testCreateTime) {
|
"The number of pending requests for an open connection, cumulative for the entire pool.")
|
||||||
testing.waitAndAssertMetrics(
|
.hasLongSumSatisfying(this::verifyPoolName);
|
||||||
instrumentationName,
|
}
|
||||||
"db.client.connections.create_time",
|
|
||||||
metrics ->
|
private void verifyTimeouts() {
|
||||||
metrics.anySatisfy(
|
testing.waitAndAssertMetrics(
|
||||||
metric ->
|
instrumentationName,
|
||||||
assertThat(metric)
|
"db.client.connections.timeouts",
|
||||||
.hasUnit("ms")
|
metrics -> metrics.anySatisfy(this::verifyTimeoutsMetric));
|
||||||
.hasDescription("The time it took to create a new connection.")
|
}
|
||||||
.hasHistogramSatisfying(
|
|
||||||
histogram ->
|
private void verifyTimeoutsMetric(MetricData metric) {
|
||||||
histogram.hasPointsSatisfying(
|
assertThat(metric)
|
||||||
point ->
|
.hasUnit("timeouts")
|
||||||
point.hasAttributes(
|
.hasDescription(
|
||||||
Attributes.of(stringKey("pool.name"), poolName))))));
|
"The number of connection timeouts that have occurred trying to obtain a connection from the pool.")
|
||||||
}
|
.hasLongSumSatisfying(
|
||||||
if (testWaitTime) {
|
sum ->
|
||||||
testing.waitAndAssertMetrics(
|
sum.isMonotonic()
|
||||||
instrumentationName,
|
.hasPointsSatisfying(
|
||||||
"db.client.connections.wait_time",
|
point -> point.hasAttributes(Attributes.of(POOL_NAME_KEY, poolName))));
|
||||||
metrics ->
|
}
|
||||||
metrics.anySatisfy(
|
|
||||||
metric ->
|
private void verifyCreateTime() {
|
||||||
assertThat(metric)
|
testing.waitAndAssertMetrics(
|
||||||
.hasUnit("ms")
|
instrumentationName,
|
||||||
.hasDescription(
|
"db.client.connections.create_time",
|
||||||
"The time it took to obtain an open connection from the pool.")
|
metrics -> metrics.anySatisfy(this::verifyCreateTimeMetric));
|
||||||
.hasHistogramSatisfying(
|
}
|
||||||
histogram ->
|
|
||||||
histogram.hasPointsSatisfying(
|
private void verifyCreateTimeMetric(MetricData metric) {
|
||||||
point ->
|
assertThat(metric)
|
||||||
point.hasAttributes(
|
.hasUnit("ms")
|
||||||
Attributes.of(stringKey("pool.name"), poolName))))));
|
.hasDescription("The time it took to create a new connection.")
|
||||||
}
|
.hasHistogramSatisfying(
|
||||||
if (testUseTime) {
|
histogram ->
|
||||||
testing.waitAndAssertMetrics(
|
histogram.hasPointsSatisfying(
|
||||||
instrumentationName,
|
point -> point.hasAttributes(Attributes.of(POOL_NAME_KEY, poolName))));
|
||||||
"db.client.connections.use_time",
|
}
|
||||||
metrics ->
|
|
||||||
metrics.anySatisfy(
|
private void verifyWaitTime() {
|
||||||
metric ->
|
testing.waitAndAssertMetrics(
|
||||||
assertThat(metric)
|
instrumentationName,
|
||||||
.hasUnit("ms")
|
"db.client.connections.wait_time",
|
||||||
.hasDescription(
|
metrics -> metrics.anySatisfy(this::verifyWaitTimeMetric));
|
||||||
"The time between borrowing a connection and returning it to the pool.")
|
}
|
||||||
.hasHistogramSatisfying(
|
|
||||||
histogram ->
|
private void verifyWaitTimeMetric(MetricData metric) {
|
||||||
histogram.hasPointsSatisfying(
|
assertThat(metric)
|
||||||
point ->
|
.hasUnit("ms")
|
||||||
point.hasAttributes(
|
.hasDescription("The time it took to obtain an open connection from the pool.")
|
||||||
Attributes.of(stringKey("pool.name"), poolName))))));
|
.hasHistogramSatisfying(
|
||||||
}
|
histogram ->
|
||||||
|
histogram.hasPointsSatisfying(
|
||||||
|
point -> point.hasAttributes(Attributes.of(POOL_NAME_KEY, poolName))));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void verifyUseTime() {
|
||||||
|
testing.waitAndAssertMetrics(
|
||||||
|
instrumentationName,
|
||||||
|
"db.client.connections.use_time",
|
||||||
|
metrics -> metrics.anySatisfy(this::verifyUseTimeMetric));
|
||||||
|
}
|
||||||
|
|
||||||
|
private MetricAssert verifyUseTimeMetric(MetricData metric) {
|
||||||
|
return assertThat(metric)
|
||||||
|
.hasUnit("ms")
|
||||||
|
.hasDescription("The time between borrowing a connection and returning it to the pool.")
|
||||||
|
.hasHistogramSatisfying(
|
||||||
|
histogram ->
|
||||||
|
histogram.hasPointsSatisfying(
|
||||||
|
point -> point.hasAttributes(Attributes.of(POOL_NAME_KEY, poolName))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue