Skip to content

Commit

Permalink
OAP-211 Metric to count the number of active metrics for prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
galaxina committed Jan 29, 2024
1 parent de7b9d5 commit b5d65d4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@

package oap.prometheus;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Metrics;
import oap.http.Client;
import oap.http.server.nio.NioHttpServer;
import oap.testng.EnvFixture;
import oap.testng.Fixtures;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;

public class PrometheusExporterTest extends Fixtures {
private static final Counter TEST_1 = Metrics.counter( "test1" );
private final EnvFixture envFixture;

public PrometheusExporterTest() {
Expand All @@ -49,16 +49,34 @@ public void server() throws IOException {
var port = envFixture.portFor( "prometheus" );
try( var server = new NioHttpServer( new NioHttpServer.DefaultPort( port ) ) ) {
var exporter = new PrometheusExporter( server );

var metric1 = Metrics.counter( "test1" );
var metric2 = Metrics.timer( "test2" );

server.start();

TEST_1.increment( 2 );
metric1.increment( 2 );
metric2.record( 2, TimeUnit.SECONDS );

var response = Client.DEFAULT.get( "http://localhost:" + port + "/metrics" ).contentString();
assertThat( response ).contains( """
# HELP test1_total \s
# TYPE test1_total counter
test1_total 2.0
""" );
assertThat( response ).contains( "test2_seconds_count 1.0" );
assertThat( response ).contains( "test2_seconds_max 2.0" );
assertThat( response ).contains( "system_metrics_total 5.0" );
}
}

@AfterMethod
public void beforeMethod() {
PrometheusExporter.prometheusRegistry.getPrometheusRegistry().clear();
}

@AfterMethod
public void afterMethod() {
PrometheusExporter.prometheusRegistry.getPrometheusRegistry().clear();
}
}
19 changes: 17 additions & 2 deletions oap-stdlib/src/main/java/oap/prometheus/PrometheusExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@
import io.micrometer.core.instrument.Metrics;
import io.micrometer.prometheus.PrometheusConfig;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.prometheus.client.exporter.common.TextFormat;
import lombok.extern.slf4j.Slf4j;
import oap.http.server.nio.HttpHandler;
import oap.http.server.nio.HttpServerExchange;
import oap.http.server.nio.NioHttpServer;
import org.apache.http.entity.ContentType;

@Slf4j
public class PrometheusExporter implements HttpHandler {
public static final PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry( PrometheusConfig.DEFAULT );
private static long metricCount = 0L;

static {
Metrics.addRegistry( prometheusRegistry );

Metrics.gauge( "system_metrics_total", prometheusRegistry, pmr -> metricCount );
}

public PrometheusExporter( NioHttpServer server ) {
Expand All @@ -51,7 +54,19 @@ public PrometheusExporter( NioHttpServer server, String port ) {

@Override
public void handleRequest( HttpServerExchange exchange ) throws Exception {
metricCount = getMetricCount();

var response = prometheusRegistry.scrape();
exchange.responseOk( response, ContentType.TEXT_PLAIN.getMimeType() );
exchange.responseOk( response, TextFormat.CONTENT_TYPE_004 );
}

private static long getMetricCount() {
long count = 0;
var en = prometheusRegistry.getPrometheusRegistry().metricFamilySamples();
while( en.hasMoreElements() ) {
var s = en.nextElement();
count += s.samples.size();
}
return count;
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</repositories>

<properties>
<oap.project.version>21.3.3</oap.project.version>
<oap.project.version>21.3.4</oap.project.version>

<oap.deps.config.version>21.0.0</oap.deps.config.version>

Expand Down

0 comments on commit b5d65d4

Please sign in to comment.