JVM Profiler Reporter介紹

開篇

?JVM Profiler采集完數(shù)據(jù)后可以通過多種途徑上報(bào)數(shù)據(jù),對接Console,F(xiàn)ile,redis,kafka等,這篇文章會把源碼羅列一下畢竟都很簡單。
?JVM Profiler提供靈活的框架可以集成更多的Reporter,只要實(shí)現(xiàn)Reporter接口即可,看你個(gè)人意愿了,反正github上有源碼,直接集成編譯打包即可。



ConsoleOutputReporter

  • 簡單明了的通過Sytem.out.println來上報(bào)監(jiān)控?cái)?shù)據(jù)。
public class ConsoleOutputReporter implements Reporter {
    @Override
    public void report(String profilerName, Map<String, Object> metrics) {
        System.out.println(String.format("ConsoleOutputReporter - %s: %s", profilerName, JsonUtils.serialize(metrics)));
    }

    @Override
    public void close() {
    }
}


FileOutputReporter

  • 在指定的目錄創(chuàng)建采集數(shù)據(jù)記錄文件。
  • 通過FileWriter來往文件寫入數(shù)據(jù)。
public class FileOutputReporter implements Reporter {
    private static final AgentLogger logger = AgentLogger.getLogger(FileOutputReporter.class.getName());
    
    private String directory;
    private ConcurrentHashMap<String, FileWriter> fileWriters = new ConcurrentHashMap<>();
    private volatile boolean closed = false;
    
    public FileOutputReporter() {
    }

    public String getDirectory() {
        return directory;
    }

    public void setDirectory(String directory) {
        synchronized (this) {
            if (this.directory == null || this.directory.isEmpty()) {
                this.directory = directory;
            } else {
                throw new RuntimeException(String.format("Cannot set directory to %s because it is already has value %s", directory, this.directory));
            }
        }
    }

    @Override
    public synchronized void report(String profilerName, Map<String, Object> metrics) {
        if (closed) {
            logger.info("Report already closed, do not report metrics");
            return;
        }
        
        FileWriter writer = ensureFile(profilerName);
        try {
            writer.write(JsonUtils.serialize(metrics));
            writer.write(System.lineSeparator());
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public synchronized void close() {
        closed = true;
        
        List<FileWriter> copy = new ArrayList<>(fileWriters.values());
        for (FileWriter entry : copy) {
            try {
                entry.flush();
                entry.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    private FileWriter ensureFile(String profilerName) {
        synchronized (this) {
            if (directory == null || directory.isEmpty()) {
                try {
                    directory = Files.createTempDirectory("jvm_profiler_").toString();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return fileWriters.computeIfAbsent(profilerName, t -> createFileWriter(t));
    }
    
    private FileWriter createFileWriter(String profilerName) {
        String path = Paths.get(directory, profilerName + ".json").toString();
        try {
            return new FileWriter(path, true);
        } catch (IOException e) {
            throw new RuntimeException("Failed to create file writer: " + path, e);
        }
    }
}


KafkaOutputReporter

  • 依賴kafka-client的jar包來構(gòu)建KafkaProducer。
  • 通過producer.send來發(fā)送采集數(shù)據(jù)。
public class KafkaOutputReporter implements Reporter {
    private String brokerList = "localhost:9092";
    private boolean syncMode = false;
    
    private String topicPrefix;
    
    private ConcurrentHashMap<String, String> profilerTopics = new ConcurrentHashMap<>();

    private Producer<String, byte[]> producer;

    public KafkaOutputReporter() {
    }
    
    public KafkaOutputReporter(String brokerList, boolean syncMode, String topicPrefix) {
        this.brokerList = brokerList;
        this.syncMode = syncMode;
        this.topicPrefix = topicPrefix;
    }

    @Override
    public void report(String profilerName, Map<String, Object> metrics) {
        ensureProducer();

        String topicName = getTopic(profilerName);
        
        String str = JsonUtils.serialize(metrics);
        byte[] message = str.getBytes(StandardCharsets.UTF_8);

        Future<RecordMetadata> future = producer.send(
                new ProducerRecord<String, byte[]>(topicName, message));

        if (syncMode) {
            producer.flush();
            try {
                future.get();
            } catch (InterruptedException | ExecutionException e) {
                throw new RuntimeException(e);
            }
        }
    }

   // 省略一些非核心的代碼
    private void ensureProducer() {
        synchronized (this) {
            if (producer != null) {
                return;
            }

            Properties props = new Properties();
            props.put("bootstrap.servers", brokerList);
            props.put("retries", 10);
            props.put("batch.size", 16384);
            props.put("linger.ms", 0);
            props.put("buffer.memory", 16384000);
            props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
            props.put("value.serializer", org.apache.kafka.common.serialization.ByteArraySerializer.class.getName());

            if (syncMode) {
                props.put("acks", "all");
            }

            producer = new KafkaProducer<>(props);
        }
    }
}


RedisOutputReporter

  • 依賴jedis包來實(shí)現(xiàn)redis的讀寫。
  • redis當(dāng)中存儲的采集數(shù)據(jù)的key是機(jī)器ip和時(shí)間戳的組合,value是采集的數(shù)據(jù)。
public class RedisOutputReporter implements Reporter {

    private static final AgentLogger logger = AgentLogger.getLogger(RedisOutputReporter.class.getName());
    private JedisPool redisConn = null;

    //JedisPool should always be used as it is thread safe.
    public void report(String profilerName, Map<String, Object> metrics) {
        ensureJedisConn();
        try {
            Jedis jedisClient = redisConn.getResource();
            jedisClient.set(createOriginStamp(profilerName), JsonUtils.serialize(metrics));
            redisConn.returnResource(jedisClient);
        } catch (Exception err) {
            logger.warn(err.toString());
        }
    }

    public String createOriginStamp(String profilerName) {
        try {
            return (profilerName + "-" + InetAddress.getLocalHost().getHostAddress() + "-" + System.currentTimeMillis());
        } catch (UnknownHostException err) {
            logger.warn("Address could not be determined and will be omitted!");
            return (profilerName + "-" + System.currentTimeMillis());
        }
    }

    public void close() {
        synchronized (this) {
            redisConn.close();
            redisConn = null;
        }
    }

    private void ensureJedisConn() {
        synchronized (this) {
            if (redisConn == null || redisConn.isClosed()) {
                redisConn = new JedisPool(System.getenv("JEDIS_PROFILER_CONNECTION"));
                return;
            }
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容