Meter configuration

By default, Tanzu GemFire is instrumented and can emit meters with a properly created Meter Registry.

However, any meters that are timers based on the system clock time, similar to the time statistics, will obey the existing enable-time-statistics geode.properties setting. For more information, see Setting up Statistics.

Publishing metrics using a meter registry

A meter registry is required to emit metrics to an Application Performance Monitor (APM) or other tool that can store or display metrics. Micrometer implements many different meter registries as project imports. For a list of meter registries supported by Micrometer, see the Micrometer website.

The example below describes how you can create a publishing service using the MetricsSession and MetricsPublishingService interface in Tanzu GemFire.

Example class that would enable Prometheus metrics to be emitted:

public class SimpleMetricsPublishingService implements MetricsPublishingService {

  private static final String PORT_PROPERTY = "prometheus.metrics.port";
  private static final int DEFAULT_PORT = 0; // If no port specified, use any port
  private static final String HOSTNAME = "localhost";
  private static final int PORT = getInteger(PORT_PROPERTY, DEFAULT_PORT);

  private static Logger LOG = getLogger(SimpleMetricsPublishingService.class);

  private final int port;
  private PrometheusMeterRegistry registry;
  private HttpServer server;

  public SimpleMetricsPublishingService() {
    this(PORT);
  }

  public SimpleMetricsPublishingService(int port) {
    this.port = port;
  }

  @Override
  public void start(MetricsSession session) {
    registry = new PrometheusMeterRegistry(DEFAULT);
    session.addSubregistry(registry);

    InetSocketAddress address = new InetSocketAddress(HOSTNAME, port);
    server = null;
    try {
      server = HttpServer.create(address, 0);
    } catch (IOException thrown) {
      LOG.error("Exception while starting " + getClass().getSimpleName(), thrown);
    }
    HttpContext context = server.createContext("/");
    context.setHandler(this::requestHandler);
    server.start();

    int boundPort = server.getAddress().getPort();
    LOG.info("Started {} http://{}:{}/", getClass().getSimpleName(), HOSTNAME, boundPort);
  }

  private void requestHandler(HttpExchange httpExchange) throws IOException {
    final byte[] scrapeBytes = registry.scrape().getBytes();
    httpExchange.sendResponseHeaders(200, scrapeBytes.length);
    final OutputStream responseBody = httpExchange.getResponseBody();
    responseBody.write(scrapeBytes);
    responseBody.close();
  }

  @Override
  public void stop(MetricsSession session) {
    session.removeSubregistry(registry);
    registry = null;
    server.stop(0);
  }
}

To make your service discoverable, add the following provider-configuration file in the resource directory of your publishing service jar file:

META-INF/services/org.apache.geode.metrics.MetricsPublishingService

Add a line inside the file indicating the fully qualified class name of your implementation:

my.domain.SimpleMetricsPublishingService

Add Your Jar Files When You Start a Server and Locator

To add your metrics publishing service to a server or locator, add your jar files to the class path when you start the server or locator using gfsh and specify the prometheus.metrics.port listed in SimpleMetricsPublishingService:

gfsh>create locator --name my-locator --classpath=<path-to-my-jar-file>/my.jar --J=-Dprometheus.metrics.port=9914
 
gfsh>create server --name my-server --classpath=<path-to-my-jar-file>/my.jar --J=-Dprometheus.metrics.port=9915

Alternatively, you can add your jar files to the extensions directory in your GemFire installation and specify only the prometheus.metrics.port. If you do this, gfsh will add your jar file to the class path whenever it creates a server or locator.

check-circle-line exclamation-circle-line close-line
Scroll to top icon