|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.c3p0; |
| 7 | + |
| 8 | +import com.mchange.v2.c3p0.PooledDataSource; |
| 9 | +import io.opentelemetry.api.OpenTelemetry; |
| 10 | +import io.opentelemetry.api.metrics.ObservableLongUpDownCounter; |
| 11 | +import io.opentelemetry.instrumentation.api.metrics.db.DbConnectionPoolMetrics; |
| 12 | +import java.sql.SQLException; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.concurrent.ConcurrentHashMap; |
| 17 | +import java.util.function.LongSupplier; |
| 18 | +import javax.annotation.Nullable; |
| 19 | + |
| 20 | +final class ConnectionPoolMetrics { |
| 21 | + private static final String INSTRUMENTATION_NAME = "io.opentelemetry.c3p0-0.9"; |
| 22 | + |
| 23 | + // a weak map does not make sense here because each Meter holds a reference to the dataSource |
| 24 | + // PooledDataSource implements equals() & hashCode() in IdentityTokenResolvable, |
| 25 | + // that's why we wrap it with IdentityDataSourceKey that uses identity comparison instead |
| 26 | + private static final Map<IdentityDataSourceKey, List<ObservableLongUpDownCounter>> |
| 27 | + dataSourceMetrics = new ConcurrentHashMap<>(); |
| 28 | + |
| 29 | + public static void registerMetrics(OpenTelemetry openTelemetry, PooledDataSource dataSource) { |
| 30 | + dataSourceMetrics.compute( |
| 31 | + new IdentityDataSourceKey(dataSource), |
| 32 | + (key, existingCounters) -> |
| 33 | + ConnectionPoolMetrics.createMeters(openTelemetry, key, existingCounters)); |
| 34 | + } |
| 35 | + |
| 36 | + private static List<ObservableLongUpDownCounter> createMeters( |
| 37 | + OpenTelemetry openTelemetry, |
| 38 | + IdentityDataSourceKey key, |
| 39 | + List<ObservableLongUpDownCounter> existingCounters) { |
| 40 | + // remove old counters from the registry in case they were already there |
| 41 | + removeMetersFromRegistry(existingCounters); |
| 42 | + |
| 43 | + PooledDataSource dataSource = key.dataSource; |
| 44 | + |
| 45 | + DbConnectionPoolMetrics metrics = |
| 46 | + DbConnectionPoolMetrics.create( |
| 47 | + openTelemetry, INSTRUMENTATION_NAME, dataSource.getDataSourceName()); |
| 48 | + |
| 49 | + return Arrays.asList( |
| 50 | + metrics.usedConnections(wrapThrowingSupplier(dataSource::getNumBusyConnectionsDefaultUser)), |
| 51 | + metrics.idleConnections(wrapThrowingSupplier(dataSource::getNumIdleConnectionsDefaultUser)), |
| 52 | + metrics.pendingRequestsForConnection( |
| 53 | + wrapThrowingSupplier(dataSource::getNumThreadsAwaitingCheckoutDefaultUser))); |
| 54 | + } |
| 55 | + |
| 56 | + public static void unregisterMetrics(PooledDataSource dataSource) { |
| 57 | + List<ObservableLongUpDownCounter> meters = |
| 58 | + dataSourceMetrics.remove(new IdentityDataSourceKey(dataSource)); |
| 59 | + removeMetersFromRegistry(meters); |
| 60 | + } |
| 61 | + |
| 62 | + private static void removeMetersFromRegistry( |
| 63 | + @Nullable List<ObservableLongUpDownCounter> observableInstruments) { |
| 64 | + if (observableInstruments != null) { |
| 65 | + for (ObservableLongUpDownCounter observable : observableInstruments) { |
| 66 | + observable.close(); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * A wrapper over {@link PooledDataSource} that implements identity comparison in its {@link |
| 73 | + * #equals(Object)} and {@link #hashCode()} methods. |
| 74 | + */ |
| 75 | + static final class IdentityDataSourceKey { |
| 76 | + final PooledDataSource dataSource; |
| 77 | + |
| 78 | + IdentityDataSourceKey(PooledDataSource dataSource) { |
| 79 | + this.dataSource = dataSource; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + @SuppressWarnings("ReferenceEquality") |
| 84 | + public boolean equals(@Nullable Object o) { |
| 85 | + if (this == o) { |
| 86 | + return true; |
| 87 | + } |
| 88 | + if (o == null || getClass() != o.getClass()) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + IdentityDataSourceKey that = (IdentityDataSourceKey) o; |
| 92 | + return dataSource == that.dataSource; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public int hashCode() { |
| 97 | + return System.identityHashCode(dataSource); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public String toString() { |
| 102 | + return dataSource.toString(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + static LongSupplier wrapThrowingSupplier(DataSourceIntSupplier supplier) { |
| 107 | + return () -> { |
| 108 | + try { |
| 109 | + return supplier.getAsInt(); |
| 110 | + } catch (SQLException e) { |
| 111 | + throw new IllegalStateException("Failed to get C3P0 datasource metric", e); |
| 112 | + } |
| 113 | + }; |
| 114 | + } |
| 115 | + |
| 116 | + @FunctionalInterface |
| 117 | + interface DataSourceIntSupplier { |
| 118 | + int getAsInt() throws SQLException; |
| 119 | + } |
| 120 | + |
| 121 | + private ConnectionPoolMetrics() {} |
| 122 | +} |
0 commit comments