首页  

jmx_exporter JmxCollector 源码要点     所属分类 prometheus 浏览量 1258
http://www.github.com/prometheus/jmx_exporter

jmx_exporter
A process for exposing JMX Beans via HTTP for Prometheus consumption


4个子项目
collector
example_configs
jmx_prometheus_httpserver
jmx_prometheus_javaagent

io.prometheus.jmx.JmxCollector


jmx_prometheus_httpserver  
独立的httpserver应用 ,暴露jmx端口5555, 使用javaagent 抓取 自己的 jmx 指标 ,暴露出去 http端口 5556
http://127.0.0.1:5556/
http://127.0.0.1:5556/metrics

io.prometheus.jmx.WebServer

int port = 5556;
InetSocketAddress socket = new InetSocketAddress(port);
String file = "httpserver_sample_config.yml";
new BuildInfoCollector().register();
new JmxCollector(new File(args[1])).register();
new HTTPServer(socket, CollectorRegistry.defaultRegistry);


run_sample_httpserver.sh
java -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=5555 -jar jmx_prometheus_httpserver/target/jmx_prometheus_httpserver-jar-with-dependencies.jar 5556 example_configs/httpserver_sample_config.yml

-Dcom.sun.management.jmxremote.port=5555

example_configs/httpserver_sample_config.yml



hostPort: localhost:5555
username: 
password: 
rules:
- pattern: ".*"


hostPort	The host and port to connect to via remote JMX. 
If neither this nor jmxUrl is specified, will talk to the local JVM.
如果 hostPort 和 jmxUrl 都没有配置则直接连接本地jvm  


JmxCollector
  Rule  Config 

public JmxCollector(File in)
public JmxCollector(String yamlConfig) 

private Config loadConfig(Map yamlConfig)

        if (yamlConfig.containsKey("hostPort")) {
          if (yamlConfig.containsKey("jmxUrl")) {
            throw new IllegalArgumentException("At most one of hostPort and jmxUrl must be provided");
          }
          cfg.jmxUrl ="service:jmx:rmi:///jndi/rmi://" + (String)yamlConfig.get("hostPort") + "/jmxrmi";
        } else if (yamlConfig.containsKey("jmxUrl")) {
          cfg.jmxUrl = (String)yamlConfig.get("jmxUrl");
        }
        
cfg.jmxUrl ="service:jmx:rmi:///jndi/rmi://" + (String)yamlConfig.get("hostPort") + "/jmxrmi";


if (yamlRule.containsKey("pattern")) {
    rule.pattern = Pattern.compile("^.*(?:" + (String)yamlRule.get("pattern") + ").*$");
}
            
static String safeName(String name)

Change invalid chars to underscore, and merge underscores.
非法字符转换为下划线 , 多个 下划线  只保留一个


  private static boolean isLegalCharacter(char input) {
    return ((input == ':') ||
            (input == '_') ||
            (input >= 'a' && input <= 'z') ||
            (input >= 'A' && input <= 'Z') ||
            (input >= '0' && input <= '9'));
  }
  
                     
抓取指标的核心方法
public List collect()

Receiver receiver = new Receiver();
JmxScraper scraper = new JmxScraper(config.jmxUrl, config.username, config.password, config.ssl,
              config.whitelistObjectNames, config.blacklistObjectNames, receiver, jmxMBeanPropertyCache);
              
scraper.doScrape();


      // [] and () are special in regexes, so swtich to <>.
      private String angleBrackets(String s) {
        return "<" + s.substring(1, s.length() - 1) + ">";
      }
     
使用尖括号
[] 和 ()  在正则表达式里有特殊含义

启动 sample_httpserver , 设置 参数  127.0.0.1:5555 ,可直接运行


   public static void main(String[] args) throws Exception {
      String hostPort = "";
      if (args.length > 0) {
        hostPort = args[0];
      }
      // 小技巧, 避免双引号转义
      JmxCollector jc = new JmxCollector(("{"
      + "`hostPort`: `" + hostPort + "`,"
      + "}").replace('`', '"'));
      for(MetricFamilySamples mfs : jc.collect()) {
        System.out.println(mfs);
      }
    }

上一篇     下一篇
ZooKeeper面试题

springboot 读取中文配置乱码

jmx_prometheus_javaagent 使用

jedis 获取 redis info 信息

Redis内部存储结构

kafka-topics.sh 无法获取topic列表及topic信息