net.thisptr.java.prometheus.metrics.agent.PrometheusExporterServerHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-prometheus-metrics-agent Show documentation
Show all versions of java-prometheus-metrics-agent Show documentation
Java agent for collecting and reporting metrics to Prometheus
package net.thisptr.java.prometheus.metrics.agent;
import java.io.IOException;
import java.io.StringWriter;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.NullNode;
import fi.iki.elonen.NanoHTTPD.IHTTPSession;
import fi.iki.elonen.NanoHTTPD.Response;
import net.thisptr.jackson.jq.JsonQuery;
import net.thisptr.java.prometheus.metrics.agent.config.Config.OptionsConfig;
import net.thisptr.java.prometheus.metrics.agent.config.Config.PrometheusScrapeRule;
import net.thisptr.java.prometheus.metrics.agent.scraper.Scraper;
/**
* https://github.com/prometheus/docs/blob/master/content/docs/instrumenting/exposition_formats.md
*/
public class PrometheusExporterServerHandler {
private static final ObjectMapper MAPPER = new ObjectMapper();
private final Scraper scraper;
private final JsonQuery labels;
private final OptionsConfig options;
public PrometheusExporterServerHandler(final List rules, final JsonQuery labels, final OptionsConfig options) {
this.labels = labels;
this.options = options;
this.scraper = new Scraper<>(ManagementFactory.getPlatformMBeanServer(), rules);
}
private Map makeLabels() throws IOException {
if (labels == null)
return Collections.emptyMap();
final List nodes = labels.apply(RootScope.getInstance(), NullNode.getInstance());
if (nodes.isEmpty())
return Collections.emptyMap();
final JsonNode in = nodes.get(nodes.size() - 1);
try (JsonParser jp = MAPPER.treeAsTokens(in)) {
return MAPPER.readValue(jp, new TypeReference
© 2015 - 2024 Weber Informatics LLC | Privacy Policy