Skip to content

Commit

Permalink
Merge branch 'main' into buffer-sync
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger authored Oct 1, 2024
2 parents 6048ee9 + a6815dc commit aaa0a45
Show file tree
Hide file tree
Showing 23 changed files with 1,931 additions and 1,825 deletions.
27 changes: 0 additions & 27 deletions .circleci/config.yml

This file was deleted.

25 changes: 25 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Build

on: [push]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up JDK
uses: actions/setup-java@v4
with:
java-version: 17
distribution: temurin
cache: 'maven'
- name: Shaded dependencies
run: |
cd prometheus-metrics-shaded-dependencies
../mvnw clean install
- name: Run the Maven verify phase
run: |
./mvnw clean install
./mvnw javadoc:jar
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ Prometheus uses GitHub to manage reviews of pull requests.
on our [mailing list](https://groups.google.com/forum/?fromgroups#!forum/prometheus-developers).
This will avoid unnecessary work and surely give you and us a good deal
of inspiration.

## Formatting

This repository uses [Google Java Format](https://github.com/google/google-java-format) to format the code.

Run `./mvnw spotless:apply` to format the code (only changed files) before committing.
1 change: 1 addition & 0 deletions MAINTAINERS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Fabian Stäber <[email protected]> @fstab
* Doug Hoard <[email protected]> @dhoard
* Tom Wilkie <[email protected]> @tomwilkie
* Gregor Zeitlinger <[email protected]> @zeitlinger
30 changes: 28 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?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">
<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">
<packaging>pom</packaging>
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -156,9 +157,32 @@
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
</plugin>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.43.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<configuration>
<ratchetFrom>origin/main</ratchetFrom>
<java>
<googleJavaFormat/>
</java>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
Expand Down Expand Up @@ -217,7 +241,9 @@
<linksource>true</linksource>
<noqualifier>all</noqualifier>
<show>public</show>
<skippedModules>benchmarks,examples,integration-tests,integration_tests,,simpleclient,simpleclient_bom,simpleclient_caffeine,simpleclient_common,simpleclient_dropwizard,simpleclient_graphite_bridge,simpleclient_guava,simpleclient_hibernate,simpleclient_hotspot,simpleclient_httpserver,simpleclient_jetty,simpleclient_jetty_jdk8,simpleclient_log4j,simpleclient_log4j2,simpleclient_logback,simpleclient_pushgateway,simpleclient_servlet,simpleclient_servlet_common,simpleclient_servlet_jakarta,simpleclient_spring_boot,simpleclient_spring_web,simpleclient_tracer,simpleclient_vertx,simpleclient_vertx4</skippedModules>
<skippedModules>
benchmarks,examples,integration-tests,integration_tests,,simpleclient,simpleclient_bom,simpleclient_caffeine,simpleclient_common,simpleclient_dropwizard,simpleclient_graphite_bridge,simpleclient_guava,simpleclient_hibernate,simpleclient_hotspot,simpleclient_httpserver,simpleclient_jetty,simpleclient_jetty_jdk8,simpleclient_log4j,simpleclient_log4j2,simpleclient_logback,simpleclient_pushgateway,simpleclient_servlet,simpleclient_servlet_common,simpleclient_servlet_jakarta,simpleclient_spring_boot,simpleclient_spring_web,simpleclient_tracer,simpleclient_vertx,simpleclient_vertx4
</skippedModules>
<excludePackageNames>io.prometheus.metrics.expositionformats.generated.*</excludePackageNames>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.model.registry.PrometheusRegistry;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
* Registers all JVM metrics. Example usage:
Expand All @@ -13,15 +14,14 @@
*/
public class JvmMetrics {

private static AtomicBoolean registeredWithTheDefaultRegistry = new AtomicBoolean(false);

private static final Set<PrometheusRegistry> REGISTERED = ConcurrentHashMap.newKeySet();
public static Builder builder() {
return new Builder(PrometheusProperties.get());
}

// Note: Currently there is no configuration for JVM metrics, so it doesn't matter whether you pass a config or not.
// However, we will add config options in the future, like whether you want to use Prometheus naming conventions
//'or OpenTelemetry semantic conventions for JVM metrics.
// or OpenTelemetry semantic conventions for JVM metrics.
public static Builder builder(PrometheusProperties config) {
return new Builder(config);
}
Expand All @@ -37,32 +37,32 @@ private Builder(PrometheusProperties config) {
/**
* Register all JVM metrics with the default registry.
* <p>
* It's safe to call this multiple times:
* Only the first call will register the metrics, all subsequent calls will be ignored.
* It's safe to call this multiple times, only the first call will register the metrics, all subsequent calls
* will be ignored.
*/
public void register() {
if (!registeredWithTheDefaultRegistry.getAndSet(true)) {
register(PrometheusRegistry.defaultRegistry);
}
register(PrometheusRegistry.defaultRegistry);
}

/**
* Register all JVM metrics with the {@code registry}.
* <p>
* You must make sure to call this only once per {@code registry}, otherwise it will
* throw an Exception because you are trying to register duplicate metrics.
* It's safe to call this multiple times, only the first call will register the metrics, all subsequent calls
* will be ignored.
*/
public void register(PrometheusRegistry registry) {
JvmThreadsMetrics.builder(config).register(registry);
JvmBufferPoolMetrics.builder(config).register(registry);
JvmClassLoadingMetrics.builder(config).register(registry);
JvmCompilationMetrics.builder(config).register(registry);
JvmGarbageCollectorMetrics.builder(config).register(registry);
JvmMemoryPoolAllocationMetrics.builder(config).register(registry);
JvmMemoryMetrics.builder(config).register(registry);
JvmNativeMemoryMetrics.builder(config).register(registry);
JvmRuntimeInfoMetric.builder(config).register(registry);
ProcessMetrics.builder(config).register(registry);
if (REGISTERED.add(registry)) {
JvmThreadsMetrics.builder(config).register(registry);
JvmBufferPoolMetrics.builder(config).register(registry);
JvmClassLoadingMetrics.builder(config).register(registry);
JvmCompilationMetrics.builder(config).register(registry);
JvmGarbageCollectorMetrics.builder(config).register(registry);
JvmMemoryPoolAllocationMetrics.builder(config).register(registry);
JvmMemoryMetrics.builder(config).register(registry);
JvmNativeMemoryMetrics.builder(config).register(registry);
JvmRuntimeInfoMetric.builder(config).register(registry);
ProcessMetrics.builder(config).register(registry);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.prometheus.metrics.instrumentation.jvm;

import io.prometheus.metrics.model.registry.PrometheusRegistry;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class JvmMetricsTest {

@Test
public void testRegisterIdempotent() {
PrometheusRegistry registry = new PrometheusRegistry();
assertEquals(0, registry.scrape().size());
JvmMetrics.builder().register(registry);
assertTrue(registry.scrape().size() > 0);
JvmMetrics.builder().register(registry);
}
}
Original file line number Diff line number Diff line change
@@ -1,76 +1,77 @@
package io.prometheus.metrics.model.registry;

import io.prometheus.metrics.model.snapshots.MetricSnapshot;

import java.util.function.Predicate;

import static io.prometheus.metrics.model.snapshots.PrometheusNaming.prometheusName;

/**
* To be registered with the Prometheus collector registry.
* See <i>Overall Structure</i> on
* <a href="https://prometheus.io/docs/instrumenting/writing_clientlibs/">https://prometheus.io/docs/instrumenting/writing_clientlibs/</a>.
* To be registered with the Prometheus collector registry. See <i>Overall Structure</i> on <a
* href="https://prometheus.io/docs/instrumenting/writing_clientlibs/">https://prometheus.io/docs/instrumenting/writing_clientlibs/</a>.
*/
@FunctionalInterface
public interface Collector {

/**
* Called when the Prometheus server scrapes metrics.
*/
MetricSnapshot collect();
/** Called when the Prometheus server scrapes metrics. */
MetricSnapshot collect();

/**
* Provides Collector with the details of the request issued by Prometheus to allow multi-target pattern implementation
* Override to implement request dependent logic to provide MetricSnapshot
*/
default MetricSnapshot collect(PrometheusScrapeRequest scrapeRequest) {
return collect();
}

/**
* Like {@link #collect()}, but returns {@code null} if {@code includedNames.test(name)} is {@code false}.
* <p>
* Override this if there is a more efficient way than first collecting the snapshot and then discarding it.
*/
default MetricSnapshot collect(Predicate<String> includedNames) {
MetricSnapshot result = collect();
if (includedNames.test(result.getMetadata().getPrometheusName())) {
return result;
} else {
return null;
}
}

/**
* Like {@link #collect(Predicate)}, but with support for multi-target pattern.
* <p>
* Override this if there is a more efficient way than first collecting the snapshot and then discarding it.
*/
default MetricSnapshot collect(Predicate<String> includedNames, PrometheusScrapeRequest scrapeRequest) {
MetricSnapshot result = collect(scrapeRequest);
if (includedNames.test(result.getMetadata().getPrometheusName())) {
return result;
} else {
return null;
}
/**
* Provides Collector with the details of the request issued by Prometheus to allow multi-target
* pattern implementation Override to implement request dependent logic to provide MetricSnapshot
*/
default MetricSnapshot collect(PrometheusScrapeRequest scrapeRequest) {
return collect();
}

/**
* Like {@link #collect()}, but returns {@code null} if {@code includedNames.test(name)} is {@code
* false}.
*
* <p>Override this if there is a more efficient way than first collecting the snapshot and then
* discarding it.
*/
default MetricSnapshot collect(Predicate<String> includedNames) {
MetricSnapshot result = collect();
if (includedNames.test(result.getMetadata().getPrometheusName())) {
return result;
} else {
return null;
}
}

/**
* This is called in two places:
* <ol>
* <li>During registration to check if a metric with that name already exists.</li>
* <li>During scrape to check if this collector can be skipped because a name filter is present and the metric name is excluded.</li>
* </ol>
* Returning {@code null} means checks are omitted (registration the metric always succeeds),
* and the collector is always scraped (the result is dropped after scraping if a name filter is present and
* the metric name is excluded).
* <p>
* If your metric has a name that does not change at runtime it is a good idea to overwrite this and return the name.
* <p>
* All metrics in {@code prometheus-metrics-core} override this.
*/
default String getPrometheusName() {
return null;
/**
* Like {@link #collect(Predicate)}, but with support for multi-target pattern.
*
* <p>Override this if there is a more efficient way than first collecting the snapshot and then
* discarding it.
*/
default MetricSnapshot collect(
Predicate<String> includedNames, PrometheusScrapeRequest scrapeRequest) {
MetricSnapshot result = collect(scrapeRequest);
if (includedNames.test(result.getMetadata().getPrometheusName())) {
return result;
} else {
return null;
}
}

/**
* This is called in two places:
*
* <ol>
* <li>During registration to check if a metric with that name already exists.
* <li>During scrape to check if this collector can be skipped because a name filter is present
* and the metric name is excluded.
* </ol>
*
* Returning {@code null} means checks are omitted (registration the metric always succeeds), and
* the collector is always scraped (the result is dropped after scraping if a name filter is
* present and the metric name is excluded).
*
* <p>If your metric has a name that does not change at runtime it is a good idea to overwrite
* this and return the name.
*
* <p>All metrics in {@code prometheus-metrics-core} override this.
*/
default String getPrometheusName() {
return null;
}
}
Loading

0 comments on commit aaa0a45

Please sign in to comment.