-
Notifications
You must be signed in to change notification settings - Fork 807
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Fabian Stäber <[email protected]>
- Loading branch information
Showing
38 changed files
with
2,627 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>io.prometheus</groupId> | ||
<artifactId>client_java</artifactId> | ||
<version>1.0.0-alpha-4-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>prometheus-metrics-instrumentation-jvm</artifactId> | ||
<packaging>bundle</packaging> | ||
|
||
<name>Prometheus Metrics Instrumentation - JVM</name> | ||
<description> | ||
Instrumentation library for JVM metrics | ||
</description> | ||
|
||
<licenses> | ||
<license> | ||
<name>The Apache Software License, Version 2.0</name> | ||
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> | ||
<distribution>repo</distribution> | ||
</license> | ||
</licenses> | ||
|
||
<developers> | ||
<developer> | ||
<id>brian-brazil</id> | ||
<name>Brian Brazil</name> | ||
<email>[email protected]</email> | ||
</developer> | ||
<developer> | ||
<id>fstab</id> | ||
<name>Fabian Stäber</name> | ||
<email>[email protected]</email> | ||
</developer> | ||
</developers> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.prometheus</groupId> | ||
<artifactId>prometheus-metrics-core</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<!-- test dependencies --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<version>4.11.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.prometheus</groupId> | ||
<artifactId>prometheus-metrics-exporter-httpserver</artifactId> | ||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.prometheus</groupId> | ||
<artifactId>prometheus-metrics-exposition-formats</artifactId> | ||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
126 changes: 126 additions & 0 deletions
126
...ion-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmBufferPoolMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package io.prometheus.metrics.instrumentation.jvm; | ||
|
||
import io.prometheus.metrics.config.PrometheusProperties; | ||
import io.prometheus.metrics.core.metrics.GaugeWithCallback; | ||
import io.prometheus.metrics.model.registry.PrometheusRegistry; | ||
import io.prometheus.metrics.model.snapshots.Unit; | ||
|
||
import java.lang.management.BufferPoolMXBean; | ||
import java.lang.management.ManagementFactory; | ||
import java.util.List; | ||
|
||
/** | ||
* JVM Buffer Pool metrics. The {@link JvmBufferPoolMetrics} are registered as part of the {@link JvmMetrics} like this: | ||
* <pre>{@code | ||
* JvmMetrics.newBuilder().register(); | ||
* }</pre> | ||
* However, if you want only the {@link JvmBufferPoolMetrics} you can also register them directly: | ||
* <pre>{@code | ||
* JvmBufferPoolMetrics.newBuilder().register(); | ||
* }</pre> | ||
* Example metrics being exported: | ||
* <pre> | ||
* # HELP jvm_buffer_pool_capacity_bytes Bytes capacity of a given JVM buffer pool. | ||
* # TYPE jvm_buffer_pool_capacity_bytes gauge | ||
* jvm_buffer_pool_capacity_bytes{pool="direct"} 8192.0 | ||
* jvm_buffer_pool_capacity_bytes{pool="mapped"} 0.0 | ||
* # HELP jvm_buffer_pool_used_buffers Used buffers of a given JVM buffer pool. | ||
* # TYPE jvm_buffer_pool_used_buffers gauge | ||
* jvm_buffer_pool_used_buffers{pool="direct"} 1.0 | ||
* jvm_buffer_pool_used_buffers{pool="mapped"} 0.0 | ||
* # HELP jvm_buffer_pool_used_bytes Used bytes of a given JVM buffer pool. | ||
* # TYPE jvm_buffer_pool_used_bytes gauge | ||
* jvm_buffer_pool_used_bytes{pool="direct"} 8192.0 | ||
* jvm_buffer_pool_used_bytes{pool="mapped"} 0.0 | ||
* </pre> | ||
*/ | ||
public class JvmBufferPoolMetrics { | ||
|
||
private static final String JVM_BUFFER_POOL_USED_BYTES = "jvm_buffer_pool_used_bytes"; | ||
private static final String JVM_BUFFER_POOL_CAPACITY_BYTES = "jvm_buffer_pool_capacity_bytes"; | ||
private static final String JVM_BUFFER_POOL_USED_BUFFERS = "jvm_buffer_pool_used_buffers"; | ||
|
||
private final PrometheusProperties config; | ||
private final List<BufferPoolMXBean> bufferPoolBeans; | ||
|
||
private JvmBufferPoolMetrics(List<BufferPoolMXBean> bufferPoolBeans, PrometheusProperties config) { | ||
this.config = config; | ||
this.bufferPoolBeans = bufferPoolBeans; | ||
} | ||
|
||
private void register(PrometheusRegistry registry) { | ||
|
||
GaugeWithCallback.newBuilder(config) | ||
.withName(JVM_BUFFER_POOL_USED_BYTES) | ||
.withHelp("Used bytes of a given JVM buffer pool.") | ||
.withUnit(Unit.BYTES) | ||
.withLabelNames("pool") | ||
.withCallback(callback -> { | ||
for (BufferPoolMXBean pool : bufferPoolBeans) { | ||
callback.call(pool.getMemoryUsed(), pool.getName()); | ||
} | ||
}) | ||
.register(registry); | ||
|
||
GaugeWithCallback.newBuilder(config) | ||
.withName(JVM_BUFFER_POOL_CAPACITY_BYTES) | ||
.withHelp("Bytes capacity of a given JVM buffer pool.") | ||
.withUnit(Unit.BYTES) | ||
.withLabelNames("pool") | ||
.withCallback(callback -> { | ||
for (BufferPoolMXBean pool : bufferPoolBeans) { | ||
callback.call(pool.getTotalCapacity(), pool.getName()); | ||
} | ||
}) | ||
.register(registry); | ||
|
||
GaugeWithCallback.newBuilder(config) | ||
.withName(JVM_BUFFER_POOL_USED_BUFFERS) | ||
.withHelp("Used buffers of a given JVM buffer pool.") | ||
.withLabelNames("pool") | ||
.withCallback(callback -> { | ||
for (BufferPoolMXBean pool : bufferPoolBeans) { | ||
callback.call(pool.getCount(), pool.getName()); | ||
} | ||
}) | ||
.register(registry); | ||
} | ||
|
||
public static Builder newBuilder() { | ||
return new Builder(PrometheusProperties.get()); | ||
} | ||
|
||
public static Builder newBuilder(PrometheusProperties config) { | ||
return new Builder(config); | ||
} | ||
|
||
public static class Builder { | ||
|
||
private final PrometheusProperties config; | ||
private List<BufferPoolMXBean> bufferPoolBeans; | ||
|
||
private Builder(PrometheusProperties config) { | ||
this.config = config; | ||
} | ||
|
||
/** | ||
* Package private. For testing only. | ||
*/ | ||
Builder withBufferPoolBeans(List<BufferPoolMXBean> bufferPoolBeans) { | ||
this.bufferPoolBeans = bufferPoolBeans; | ||
return this; | ||
} | ||
|
||
public void register() { | ||
register(PrometheusRegistry.defaultRegistry); | ||
} | ||
|
||
public void register(PrometheusRegistry registry) { | ||
List<BufferPoolMXBean> bufferPoolBeans = this.bufferPoolBeans; | ||
if (bufferPoolBeans == null) { | ||
bufferPoolBeans = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class); | ||
} | ||
new JvmBufferPoolMetrics(bufferPoolBeans, config).register(registry); | ||
} | ||
} | ||
} |
Oops, something went wrong.