@@ -1336,21 +1336,25 @@ abstract class AbstractStatsCollector extends ManagedContextRunnable {
13361336 protected void sendMetricsToInfluxdb (Map <Object , Object > metrics ) {
13371337 InfluxDB influxDbConnection = createInfluxDbConnection ();
13381338
1339- Pong response = influxDbConnection .ping ();
1340- if (response .getVersion ().equalsIgnoreCase ("unknown" )) {
1341- throw new CloudRuntimeException (String .format ("Cannot ping influxdb host %s:%s." , externalStatsHost , externalStatsPort ));
1342- }
1339+ try {
1340+ Pong response = influxDbConnection .ping ();
1341+ if (response .getVersion ().equalsIgnoreCase ("unknown" )) {
1342+ throw new CloudRuntimeException (String .format ("Cannot ping influxdb host %s:%s." , externalStatsHost , externalStatsPort ));
1343+ }
13431344
1344- Collection <Object > metricsObjects = metrics .values ();
1345- List <Point > points = new ArrayList <>();
1345+ Collection <Object > metricsObjects = metrics .values ();
1346+ List <Point > points = new ArrayList <>();
13461347
1347- s_logger .debug (String .format ("Sending stats to %s host %s:%s" , externalStatsType , externalStatsHost , externalStatsPort ));
1348+ s_logger .debug (String .format ("Sending stats to %s host %s:%s" , externalStatsType , externalStatsHost , externalStatsPort ));
13481349
1349- for (Object metricsObject : metricsObjects ) {
1350- Point vmPoint = creteInfluxDbPoint (metricsObject );
1351- points .add (vmPoint );
1350+ for (Object metricsObject : metricsObjects ) {
1351+ Point vmPoint = creteInfluxDbPoint (metricsObject );
1352+ points .add (vmPoint );
1353+ }
1354+ writeBatches (influxDbConnection , databaseName , points );
1355+ } finally {
1356+ influxDbConnection .close ();
13521357 }
1353- writeBatches (influxDbConnection , databaseName , points );
13541358 }
13551359
13561360 /**
@@ -1523,7 +1527,9 @@ protected InfluxDB createInfluxDbConnection() {
15231527 */
15241528 protected void writeBatches (InfluxDB influxDbConnection , String dbName , List <Point > points ) {
15251529 BatchPoints batchPoints = BatchPoints .database (dbName ).build ();
1526- influxDbConnection .enableBatch (BatchOptions .DEFAULTS );
1530+ if (!influxDbConnection .isBatchEnabled ()){
1531+ influxDbConnection .enableBatch (BatchOptions .DEFAULTS );
1532+ }
15271533
15281534 for (Point point : points ) {
15291535 batchPoints .point (point );
0 commit comments