Ver Fonte

Move to async metric writers

Nick Babcock há 7 anos atrás
pai
commit
3b9e7b3517

+ 3 - 2
OhmGraphite/GraphiteWriter.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.IO;
 using System.IO;
 using System.Net.Sockets;
 using System.Net.Sockets;
 using System.Text;
 using System.Text;
+using System.Threading.Tasks;
 using OpenHardwareMonitor.Hardware;
 using OpenHardwareMonitor.Hardware;
 using static System.FormattableString;
 using static System.FormattableString;
 
 
@@ -23,7 +24,7 @@ namespace OhmGraphite
             _localHost = localHost;
             _localHost = localHost;
         }
         }
 
 
-        public void ReportMetrics(DateTime reportTime, IEnumerable<ReportedValue> sensors)
+        public async Task ReportMetrics(DateTime reportTime, IEnumerable<ReportedValue> sensors)
         {
         {
             // We don't want to transmit metrics across multiple seconds as they
             // We don't want to transmit metrics across multiple seconds as they
             // are being retrieved so calculate the timestamp of the signaled event
             // are being retrieved so calculate the timestamp of the signaled event
@@ -35,7 +36,7 @@ namespace OhmGraphite
             {
             {
                 foreach (var sensor in sensors)
                 foreach (var sensor in sensors)
                 {
                 {
-                    writer.WriteLine(FormatGraphiteData(epoch, sensor));
+                    await writer.WriteLineAsync(FormatGraphiteData(epoch, sensor));
                 }
                 }
             }
             }
         }
         }

+ 2 - 1
OhmGraphite/IWriteMetrics.cs

@@ -1,10 +1,11 @@
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
+using System.Threading.Tasks;
 
 
 namespace OhmGraphite
 namespace OhmGraphite
 {
 {
     public interface IWriteMetrics
     public interface IWriteMetrics
     {
     {
-        void ReportMetrics(DateTime reportTime, IEnumerable<ReportedValue> sensors);
+        Task ReportMetrics(DateTime reportTime, IEnumerable<ReportedValue> sensors);
     }
     }
 }
 }

+ 3 - 3
OhmGraphite/InfluxWriter.cs

@@ -1,6 +1,7 @@
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
+using System.Threading.Tasks;
 using InfluxDB.LineProtocol;
 using InfluxDB.LineProtocol;
 using InfluxDB.LineProtocol.Client;
 using InfluxDB.LineProtocol.Client;
 using InfluxDB.LineProtocol.Payload;
 using InfluxDB.LineProtocol.Payload;
@@ -22,7 +23,7 @@ namespace OhmGraphite
             _localHost = localHost;
             _localHost = localHost;
         }
         }
 
 
-        public void ReportMetrics(DateTime reportTime, IEnumerable<ReportedValue> sensors)
+        public async Task ReportMetrics(DateTime reportTime, IEnumerable<ReportedValue> sensors)
         {
         {
             var payload = new LineProtocolPayload();
             var payload = new LineProtocolPayload();
             var client = new LineProtocolClient(_config.Address, _config.Db, _config.User, _config.Password);
             var client = new LineProtocolClient(_config.Address, _config.Db, _config.User, _config.Password);
@@ -32,8 +33,7 @@ namespace OhmGraphite
                 payload.Add(point);
                 payload.Add(point);
             }
             }
 
 
-            var task = client.SendAsync(writer);
-            var result = task.GetAwaiter().GetResult();
+            var result = await client.SendAsync(writer);
             if (!result.Success)
             if (!result.Success)
             {
             {
                 Logger.Error("Influxdb encountered an error: {0}", result.ErrorMessage);
                 Logger.Error("Influxdb encountered an error: {0}", result.ErrorMessage);

+ 2 - 2
OhmGraphite/MetricTimer.cs

@@ -40,7 +40,7 @@ namespace OhmGraphite
             });
             });
         }
         }
 
 
-        private void ReportMetrics(object sender, ElapsedEventArgs e)
+        private async void ReportMetrics(object sender, ElapsedEventArgs e)
         {
         {
             Logger.Debug("Starting to report metrics");
             Logger.Debug("Starting to report metrics");
             try
             try
@@ -52,7 +52,7 @@ namespace OhmGraphite
                 // don't affect future results
                 // don't affect future results
                 var stopwatch = Stopwatch.StartNew();
                 var stopwatch = Stopwatch.StartNew();
                 var sensors = _collector.ReadAllSensors().ToList();
                 var sensors = _collector.ReadAllSensors().ToList();
-                _writer.ReportMetrics(e.SignalTime, sensors);
+                await _writer.ReportMetrics(e.SignalTime, sensors);
                 Logger.Info($"Sent {sensors.Count} metrics in {stopwatch.Elapsed.TotalMilliseconds}ms");
                 Logger.Info($"Sent {sensors.Count} metrics in {stopwatch.Elapsed.TotalMilliseconds}ms");
             }
             }
             catch (Exception ex)
             catch (Exception ex)