Skip to content

Commit

Permalink
Add armeria-prometheus1 module for Prometheus version 1 and deprecate…
Browse files Browse the repository at this point in the history
… older classes. (#5698)

Motivation:
Micrometer 1.13.0 updates its Prometheus dependency from 0.x to 1.x. In Prometheus 1.x, the package of `PrometheusMeterRegistry` has changed and `CollectorRegistry` is no longer used. More details can be found in the [migration guide](https://github.com/micrometer-metrics/micrometer/wiki/1.13-Migration-Guide).

Modifications:
- Updated Micrometer from 1.12.4 to 1.13.0.
- Removed the Micrometer 1.3 integration test module.
  - We dropped supporting Micrometer <= 1.5 already: #5661
- Updated Prometheus from 0.16.0 to 1.3.0.
- Added `armeria-prometheus1` module.
  -  `PrometheusMeterRegistries`, `PrometheusVersion1ExpositionService`, and its builder classes are added.
- Deprecated `PrometheusMeterRegistries`, `PrometheusExpositionService`, and its builder classes.

Result:
- The older `PrometheusMeterRegistries`, `PrometheusExpositionService`, and its builders are deprecated.
  - Use the same classes in the `armeria-prometheus1`.
  • Loading branch information
minwoox authored Jun 3, 2024
1 parent 7f2aa00 commit 00d421f
Show file tree
Hide file tree
Showing 62 changed files with 725 additions and 190 deletions.
6 changes: 4 additions & 2 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ dependencies {

// Micrometer and other metric-related stuff
api libs.micrometer.core
optionalApi libs.micrometer.prometheus
optionalApi libs.micrometer.prometheus.legacy
optionalApi libs.dropwizard.metrics.core
optionalApi libs.prometheus
optionalApi libs.prometheus.legacy

// Netty
api libs.netty.transport
Expand Down Expand Up @@ -154,6 +154,8 @@ dependencies {
// Jetty, for testing interoperability with other servers.
testImplementation libs.jetty94.webapp

testImplementation project(':prometheus1')

// Brotli
implementation libs.brotli4j
optionalImplementation libs.brotli4j.linux
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
/**
* Provides the convenient factory methods for {@link PrometheusMeterRegistry} with more sensible defaults for
* {@link NamingConvention}.
*
* @deprecated Use {@code PrometheusMeterRegistries} in {@code armeria-prometheus1} module instead.
*/
@Deprecated
public final class PrometheusMeterRegistries {

private static final PrometheusMeterRegistry defaultRegistry =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@
/**
* Exposes Prometheus metrics in <a href="https://prometheus.io/docs/instrumenting/exposition_formats/">text
* format 0.0.4 or OpenMetrics format</a>.
*
* @deprecated Use {@code PrometheusExpositionService} in {@code armeria-prometheus1} module instead.
*/
@Deprecated
public final class PrometheusExpositionService extends AbstractHttpService implements TransientHttpService {

/**
Expand Down Expand Up @@ -106,7 +109,7 @@ protected HttpResponse doGet(ServiceRequestContext ctx, HttpRequest req) throws
final ByteBuf buffer = ctx.alloc().buffer();
boolean success = false;
try (ByteBufOutputStream byteBufOutputStream = new ByteBufOutputStream(buffer);
OutputStreamWriter writer = new OutputStreamWriter(byteBufOutputStream)) {
OutputStreamWriter writer = new OutputStreamWriter(byteBufOutputStream)) {
TextFormat.writeFormat(format, writer, collectorRegistry.metricFamilySamples());
success = true;
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@

/**
* Builds a {@link PrometheusExpositionService}.
*
* @deprecated Use {@code PrometheusExpositionServiceBuilder} in {@code armeria-prometheus1} module instead.
*/
@Deprecated
public final class PrometheusExpositionServiceBuilder implements TransientServiceBuilder {

private final CollectorRegistry collectorRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.common.metric.MoreMeters;
import com.linecorp.armeria.common.metric.PrometheusMeterRegistries;
import com.linecorp.armeria.common.prometheus.PrometheusMeterRegistries;

import io.micrometer.core.instrument.MeterRegistry;
import io.netty.util.AttributeMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
import com.linecorp.armeria.client.endpoint.dns.TestDnsServer;
import com.linecorp.armeria.client.retry.Backoff;
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.metric.PrometheusMeterRegistries;
import com.linecorp.armeria.common.prometheus.PrometheusMeterRegistries;
import com.linecorp.armeria.internal.client.dns.ByteArrayDnsRecord;
import com.linecorp.armeria.internal.client.dns.DnsQuestionWithoutTrailingDot;
import com.linecorp.armeria.testing.junit5.common.EventLoopExtension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.junit.jupiter.api.Test;

import com.linecorp.armeria.common.metric.MoreMeters;
import com.linecorp.armeria.common.metric.PrometheusMeterRegistries;
import com.linecorp.armeria.common.prometheus.PrometheusMeterRegistries;

import io.micrometer.core.instrument.MeterRegistry;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import com.linecorp.armeria.client.logging.LoggingClient;
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.common.metric.MoreMeters;
import com.linecorp.armeria.common.metric.PrometheusMeterRegistries;
import com.linecorp.armeria.common.prometheus.PrometheusMeterRegistries;
import com.linecorp.armeria.server.ServerBuilder;
import com.linecorp.armeria.server.healthcheck.HealthCheckService;
import com.linecorp.armeria.testing.junit5.server.ServerExtension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,26 @@
import static org.assertj.core.api.Assertions.fail;

import java.time.Duration;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.Test;

import com.codahale.metrics.MetricRegistry;
import com.google.common.collect.ImmutableList;

import com.linecorp.armeria.common.prometheus.PrometheusMeterRegistries;

import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import io.micrometer.core.instrument.dropwizard.DropwizardMeterRegistry;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.prometheus.client.Collector.MetricFamilySamples;
import io.prometheus.client.Collector.MetricFamilySamples.Sample;
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry;
import io.prometheus.metrics.model.snapshots.DataPointSnapshot;
import io.prometheus.metrics.model.snapshots.MetricMetadata;
import io.prometheus.metrics.model.snapshots.MetricSnapshot;
import io.prometheus.metrics.model.snapshots.SummarySnapshot.SummaryDataPointSnapshot;

class DropwizardMeterRegistriesTest {
@Test
Expand Down Expand Up @@ -131,24 +134,33 @@ void filteredGaugesDoNotAffectOthers() {
assertThat(dropwizard.getDropwizardRegistry().getMetrics()).containsOnlyKeys("summary");

// Make sure Prometheus registry collects all samples.
final MetricFamilySamples prometheusSamples = findPrometheusSample(prometheus, "summary");
assertThat(prometheusSamples.samples).containsExactly(
new Sample("summary", ImmutableList.of("quantile"), ImmutableList.of("0.5"), 42),
new Sample("summary", ImmutableList.of("quantile"), ImmutableList.of("0.99"), 42),
new Sample("summary_count", ImmutableList.of(), ImmutableList.of(), 1),
new Sample("summary_sum", ImmutableList.of(), ImmutableList.of(), 42));
final List<? extends DataPointSnapshot> dataPointSnapshots =
findPrometheusDataPointSnapshot(prometheus, "summary");
assertThat(dataPointSnapshots.size()).isOne();
final DataPointSnapshot snapshot = dataPointSnapshots.get(0);
assertThat(snapshot).isInstanceOf(SummaryDataPointSnapshot.class);
// SummaryDataPointSnapshot and its values do not override equals().
final SummaryDataPointSnapshot summarySnapshot = (SummaryDataPointSnapshot) snapshot;
assertThat(summarySnapshot.getCount()).isOne();
assertThat(summarySnapshot.getSum()).isEqualTo(42.0);
assertThat(summarySnapshot.getQuantiles().size()).isEqualTo(2);
assertThat(summarySnapshot.getQuantiles().get(0).getQuantile()).isEqualTo(0.5);
assertThat(summarySnapshot.getQuantiles().get(0).getValue()).isEqualTo(42.0);
assertThat(summarySnapshot.getQuantiles().get(1).getQuantile()).isEqualTo(0.99);
assertThat(summarySnapshot.getQuantiles().get(1).getValue()).isEqualTo(42.0);
}

private static MetricFamilySamples findPrometheusSample(PrometheusMeterRegistry registry, String name) {
for (final Enumeration<MetricFamilySamples> e = registry.getPrometheusRegistry().metricFamilySamples();
e.hasMoreElements();) {
final MetricFamilySamples samples = e.nextElement();
if (name.equals(samples.name)) {
return samples;
private static List<? extends DataPointSnapshot> findPrometheusDataPointSnapshot(
PrometheusMeterRegistry registry, String name) {
for (final MetricSnapshot snapshot : registry.getPrometheusRegistry().scrape()) {
final MetricMetadata metadata = snapshot.getMetadata();
if (name.equals(metadata.getName())) {
return snapshot.getDataPoints();
}
}

fail("Could not find a Prometheus sample: " + name);
throw new Error(); // Never reaches here.
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.logging.RequestLog;
import com.linecorp.armeria.common.logging.RequestOnlyLog;
import com.linecorp.armeria.common.prometheus.PrometheusMeterRegistries;
import com.linecorp.armeria.server.ServiceRequestContext;

import io.micrometer.core.instrument.MeterRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

import com.linecorp.armeria.common.metric.MeterIdPrefix;
import com.linecorp.armeria.common.metric.MoreMeters;
import com.linecorp.armeria.common.metric.PrometheusMeterRegistries;
import com.linecorp.armeria.common.prometheus.PrometheusMeterRegistries;

import io.micrometer.core.instrument.MeterRegistry;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.junit.jupiter.api.Test;

import com.linecorp.armeria.common.metric.MeterIdPrefix;
import com.linecorp.armeria.common.metric.PrometheusMeterRegistries;
import com.linecorp.armeria.common.prometheus.PrometheusMeterRegistries;

import io.micrometer.core.instrument.MeterRegistry;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import com.linecorp.armeria.common.SuccessFunction;
import com.linecorp.armeria.common.logging.ClientConnectionTimings;
import com.linecorp.armeria.common.metric.MeterIdPrefixFunction;
import com.linecorp.armeria.common.metric.PrometheusMeterRegistries;
import com.linecorp.armeria.common.prometheus.PrometheusMeterRegistries;
import com.linecorp.armeria.common.util.SafeCloseable;
import com.linecorp.armeria.internal.testing.ImmediateEventLoop;
import com.linecorp.armeria.server.RequestTimeoutException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.HttpStatus;
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.common.metric.PrometheusMeterRegistries;
import com.linecorp.armeria.common.prometheus.PrometheusMeterRegistries;
import com.linecorp.armeria.common.util.DomainSocketAddress;
import com.linecorp.armeria.common.util.TransportType;
import com.linecorp.armeria.internal.common.util.MinifiedBouncyCastleProvider;
Expand All @@ -61,7 +61,7 @@
import com.linecorp.armeria.testing.junit5.server.ServerExtension;

import io.micrometer.core.instrument.Metrics;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry;
import io.netty.channel.ChannelOption;
import io.netty.handler.ssl.SslContextBuilder;
import reactor.core.scheduler.Schedulers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.junit.jupiter.params.provider.ArgumentsSource;

import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.metric.PrometheusMeterRegistries;
import com.linecorp.armeria.common.prometheus.PrometheusMeterRegistries;
import com.linecorp.armeria.testing.junit5.server.ServerExtension;

class ServerListenerTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
import com.linecorp.armeria.common.ResponseHeaders;
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.common.metric.MeterIdPrefix;
import com.linecorp.armeria.common.metric.PrometheusMeterRegistries;
import com.linecorp.armeria.common.prometheus.PrometheusMeterRegistries;
import com.linecorp.armeria.common.util.CompletionActions;
import com.linecorp.armeria.common.util.Exceptions;
import com.linecorp.armeria.common.util.ThreadFactories;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.junit.jupiter.api.Test;

import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.metric.PrometheusMeterRegistries;
import com.linecorp.armeria.common.prometheus.PrometheusMeterRegistries;
import com.linecorp.armeria.internal.common.util.SelfSignedCertificate;

import io.micrometer.core.instrument.Gauge;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import com.linecorp.armeria.common.HttpStatus;
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.common.metric.MoreMeters;
import com.linecorp.armeria.common.metric.PrometheusMeterRegistries;
import com.linecorp.armeria.common.prometheus.PrometheusMeterRegistries;
import com.linecorp.armeria.testing.junit5.server.ServerExtension;

import io.micrometer.core.instrument.Counter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.logging.ContentPreviewerFactory;
import com.linecorp.armeria.common.metric.MeterIdPrefixFunction;
import com.linecorp.armeria.common.metric.PrometheusMeterRegistries;
import com.linecorp.armeria.common.prometheus.PrometheusMeterRegistries;
import com.linecorp.armeria.common.stream.CancelledSubscriptionException;
import com.linecorp.armeria.common.util.Functions;
import com.linecorp.armeria.internal.logging.ContentPreviewingUtil;
Expand All @@ -51,10 +51,10 @@
import com.linecorp.armeria.server.annotation.Get;
import com.linecorp.armeria.server.cors.CorsService;
import com.linecorp.armeria.server.metric.MetricCollectingService;
import com.linecorp.armeria.server.metric.PrometheusExpositionService;
import com.linecorp.armeria.server.prometheus.PrometheusExpositionService;
import com.linecorp.armeria.testing.junit5.server.ServerExtension;

import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry;

class ContentPreviewerCancellationTest {

Expand All @@ -77,7 +77,8 @@ protected void configure(ServerBuilder sb) {
.newDecorator()
)
.annotatedService(new TestService())
.service("/metrics", PrometheusExpositionService.of(registry.getPrometheusRegistry()));
.service("/metrics",
PrometheusExpositionService.of(registry.getPrometheusRegistry()));
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@

import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.metric.MeterIdPrefixFunction;
import com.linecorp.armeria.common.metric.PrometheusMeterRegistries;
import com.linecorp.armeria.common.prometheus.PrometheusMeterRegistries;
import com.linecorp.armeria.server.ServerBuilder;
import com.linecorp.armeria.testing.junit5.server.ServerExtension;

import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry;

class MetricCollectingServiceTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ class FormatTest {
void prometheusRequestsPrometheusFormat() throws InterruptedException {
final WebClient client = WebClient.of(server.httpUri());
final HttpRequest request = HttpRequest.builder()
.get("/enabled")
.header(HttpHeaderNames.ACCEPT, TextFormat.CONTENT_TYPE_004)
.build();
.get("/enabled")
.header(HttpHeaderNames.ACCEPT, TextFormat.CONTENT_TYPE_004)
.build();
final AggregatedHttpResponse response = client.execute(request).aggregate().join();
assertThat(response.headers().get(HttpHeaderNames.CONTENT_TYPE))
.isEqualTo(TextFormat.CONTENT_TYPE_004);
Expand All @@ -142,9 +142,10 @@ void prometheusRequestsPrometheusFormat() throws InterruptedException {
void prometheusRequestsOpenMetricsFormat() throws InterruptedException {
final WebClient client = WebClient.of(server.httpUri());
final HttpRequest request = HttpRequest.builder()
.get("/enabled")
.header(HttpHeaderNames.ACCEPT, TextFormat.CONTENT_TYPE_OPENMETRICS_100)
.build();
.get("/enabled")
.header(HttpHeaderNames.ACCEPT,
TextFormat.CONTENT_TYPE_OPENMETRICS_100)
.build();
final AggregatedHttpResponse response = client.execute(request).aggregate().join();
assertThat(response.headers().get(HttpHeaderNames.CONTENT_TYPE))
.isEqualTo(TextFormat.CONTENT_TYPE_OPENMETRICS_100);
Expand Down
Loading

0 comments on commit 00d421f

Please sign in to comment.