Skip to content

Commit

Permalink
Merge branch '1.1.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Schneider committed Oct 31, 2018
2 parents be0e638 + 7f79339 commit d98a876
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.lang.NonNullApi;
import io.micrometer.core.lang.NonNullFields;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.sql.Connection;
Expand All @@ -37,6 +35,7 @@
* @author Kristof Depypere
* @author Jon Schneider
* @author Johnny Lim
* @since 1.1.0
*/
@NonNullApi
@NonNullFields
Expand All @@ -51,8 +50,6 @@ public class PostgreSQLDatabaseMetrics implements MeterBinder {
private static final String QUERY_BUFFERS_BACKEND = getBgWriterQuery("buffers_backend");
private static final String QUERY_BUFFERS_CHECKPOINT = getBgWriterQuery("buffers_checkpoint");

private final Logger logger = LoggerFactory.getLogger(PostgreSQLDatabaseMetrics.class);

private final String database;
private final DataSource postgresDataSource;
private final Iterable<Tag> tags;
Expand Down Expand Up @@ -281,13 +278,14 @@ Double resettableFunctionalCounter(String functionalCounterKey, DoubleSupplier f

private Long runQuery(String query) {
try (Connection connection = postgresDataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query)) {
return resultSet.getObject(1, Long.class);
} catch (SQLException e) {
logger.error("Error getting statistic from postgreSQL database");
return 0L;
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query)) {
if (resultSet.next()) {
return resultSet.getObject(1, Long.class);
}
} catch (SQLException ignored) {
}
return 0L;
}

private static String getDBStatQuery(String database, String statName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import io.micrometer.core.lang.NonNullApi;
import io.micrometer.core.lang.NonNullFields;
import io.micrometer.core.lang.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.management.NotificationEmitter;
import javax.management.openmbean.CompositeData;
Expand Down Expand Up @@ -73,9 +71,6 @@ static GcGenerationAge fromName(String name) {
@NonNullApi
@NonNullFields
public class JvmGcMetrics implements MeterBinder {

private static final Logger logger = LoggerFactory.getLogger(JvmGcMetrics.class);

private boolean managementExtensionsPresent = isManagementExtensionsPresent();

private Iterable<Tag> tags;
Expand Down Expand Up @@ -104,28 +99,28 @@ public JvmGcMetrics(Iterable<Tag> tags) {
public void bindTo(MeterRegistry registry) {
AtomicLong maxDataSize = new AtomicLong(0L);
Gauge.builder("jvm.gc.max.data.size", maxDataSize, AtomicLong::get)
.tags(tags)
.description("Max size of old generation memory pool")
.baseUnit("bytes")
.register(registry);
.tags(tags)
.description("Max size of old generation memory pool")
.baseUnit("bytes")
.register(registry);

AtomicLong liveDataSize = new AtomicLong(0L);

Gauge.builder("jvm.gc.live.data.size", liveDataSize, AtomicLong::get)
.tags(tags)
.description("Size of old generation memory pool after a full GC")
.baseUnit("bytes")
.register(registry);
.tags(tags)
.description("Size of old generation memory pool after a full GC")
.baseUnit("bytes")
.register(registry);

Counter promotedBytes = Counter.builder("jvm.gc.memory.promoted").tags(tags)
.baseUnit("bytes")
.description("Count of positive increases in the size of the old generation memory pool before GC to after GC")
.register(registry);
.baseUnit("bytes")
.description("Count of positive increases in the size of the old generation memory pool before GC to after GC")
.register(registry);

Counter allocatedBytes = Counter.builder("jvm.gc.memory.allocated").tags(tags)
.baseUnit("bytes")
.description("Incremented for an increase in the size of the young generation memory pool after one GC to before the next")
.register(registry);
.baseUnit("bytes")
.description("Incremented for an increase in the size of the young generation memory pool after one GC to before the next")
.register(registry);

if (this.managementExtensionsPresent) {
// start watching for GC notifications
Expand Down Expand Up @@ -199,12 +194,10 @@ public void bindTo(MeterRegistry registry) {
private static boolean isManagementExtensionsPresent() {
try {
Class.forName("com.sun.management.GarbageCollectionNotificationInfo", false,
JvmGcMetrics.class.getClassLoader());
JvmGcMetrics.class.getClassLoader());
return true;
} catch (Throwable e) {
// We are operating in a JVM without access to this level of detail
logger.warn("GC notifications will not be available because " +
"com.sun.management.GarbageCollectionNotificationInfo is not present");
return false;
}
}
Expand Down

0 comments on commit d98a876

Please sign in to comment.