From b5119ad516d648f8572a2f831ea41a93fc4307b8 Mon Sep 17 00:00:00 2001 From: Max Melentyev Date: Thu, 24 Oct 2024 12:20:58 -0400 Subject: [PATCH] Reset connection if scrape failed Signed-off-by: Max Melentyev --- .../main/java/io/prometheus/jmx/JmxScraper.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/collector/src/main/java/io/prometheus/jmx/JmxScraper.java b/collector/src/main/java/io/prometheus/jmx/JmxScraper.java index 0dac2391..13930d93 100644 --- a/collector/src/main/java/io/prometheus/jmx/JmxScraper.java +++ b/collector/src/main/java/io/prometheus/jmx/JmxScraper.java @@ -196,12 +196,18 @@ public synchronized void doScrape(MBeanReceiver receiver) throws Exception { // Method is synchronized to avoid multiple scrapes running concurrently // and let one of them refresh the cache in the middle of the scrape. - MBeanServerConnection beanConn = getMBeanServerConnection(); + try { + MBeanServerConnection beanConn = getMBeanServerConnection(); - for (ObjectName objectName : cache.mBeanNames) { - long start = System.nanoTime(); - scrapeBean(receiver, beanConn, objectName); - LOGGER.log(FINE, "TIME: %d ns for %s", System.nanoTime() - start, objectName); + for (ObjectName objectName : cache.mBeanNames) { + long start = System.nanoTime(); + scrapeBean(receiver, beanConn, objectName); + LOGGER.log(FINE, "TIME: %d ns for %s", System.nanoTime() - start, objectName); + } + } finally { + // reconnect to resolve connection issues + // TODO: should it make a single retry with a new connection? + _beanConn = null; } }