@@ -1334,21 +1334,25 @@ abstract class AbstractStatsCollector extends ManagedContextRunnable {
13341334 protected void sendMetricsToInfluxdb (Map <Object , Object > metrics ) {
13351335 InfluxDB influxDbConnection = createInfluxDbConnection ();
13361336
1337- Pong response = influxDbConnection .ping ();
1338- if (response .getVersion ().equalsIgnoreCase ("unknown" )) {
1339- throw new CloudRuntimeException (String .format ("Cannot ping influxdb host %s:%s." , externalStatsHost , externalStatsPort ));
1340- }
1337+ try {
1338+ Pong response = influxDbConnection .ping ();
1339+ if (response .getVersion ().equalsIgnoreCase ("unknown" )) {
1340+ throw new CloudRuntimeException (String .format ("Cannot ping influxdb host %s:%s." , externalStatsHost , externalStatsPort ));
1341+ }
13411342
1342- Collection <Object > metricsObjects = metrics .values ();
1343- List <Point > points = new ArrayList <>();
1343+ Collection <Object > metricsObjects = metrics .values ();
1344+ List <Point > points = new ArrayList <>();
13441345
1345- s_logger .debug (String .format ("Sending stats to %s host %s:%s" , externalStatsType , externalStatsHost , externalStatsPort ));
1346+ s_logger .debug (String .format ("Sending stats to %s host %s:%s" , externalStatsType , externalStatsHost , externalStatsPort ));
13461347
1347- for (Object metricsObject : metricsObjects ) {
1348- Point vmPoint = creteInfluxDbPoint (metricsObject );
1349- points .add (vmPoint );
1348+ for (Object metricsObject : metricsObjects ) {
1349+ Point vmPoint = creteInfluxDbPoint (metricsObject );
1350+ points .add (vmPoint );
1351+ }
1352+ writeBatches (influxDbConnection , databaseName , points );
1353+ } finally {
1354+ influxDbConnection .close ();
13501355 }
1351- writeBatches (influxDbConnection , databaseName , points );
13521356 }
13531357
13541358 /**
@@ -1521,7 +1525,9 @@ protected InfluxDB createInfluxDbConnection() {
15211525 */
15221526 protected void writeBatches (InfluxDB influxDbConnection , String dbName , List <Point > points ) {
15231527 BatchPoints batchPoints = BatchPoints .database (dbName ).build ();
1524- influxDbConnection .enableBatch (BatchOptions .DEFAULTS );
1528+ if (!influxDbConnection .isBatchEnabled ()){
1529+ influxDbConnection .enableBatch (BatchOptions .DEFAULTS );
1530+ }
15251531
15261532 for (Point point : points ) {
15271533 batchPoints .point (point );
0 commit comments