All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.heroku.agent.metrics.MetricsAgent Maven / Gradle / Ivy

Go to download

This artifact is for use with the JVM Runtime Metrics features. It is a lightweight Java agent that uses a Prometheus Java client to report metrics.

There is a newer version: 4.0.3
Show newest version
package com.heroku.agent.metrics;

import java.io.IOException;
import java.lang.instrument.Instrumentation;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.heroku.prometheus.client.BufferPoolsExports;
import io.prometheus.client.hotspot.DefaultExports;

public class MetricsAgent {

  public static void premain(String agentArgs, Instrumentation instrumentation) {
    try {
      DefaultExports.initialize();
      new BufferPoolsExports().register();

      final Reporter reporter = new Reporter();
      new Poller().poll(new Poller.Callback() {
        @Override
        public void apply(ObjectMapper mapper, ObjectNode metricsJson) {
          try {
            reporter.report(mapper.writer().writeValueAsString(metricsJson));
          } catch (IOException e) {
            logError("report-metrics", e);
          }
        }
      });
    } catch (Exception e) {
      logError("poll-metrics", e);
    }
  }

  private static void logError(String at, Throwable t) {
    System.out.println("error at=\"" + at + "\" component=heroku-java-metrics-agent message=\"" + t.getMessage() + "\"");

    String debug = System.getenv("HEROKU_METRICS_DEBUG");
    if ("1".equals(debug) || "true".equals(debug)) {
      t.printStackTrace();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy