Explorar el Código

Merge pull request #94 from nickbabcock/code-cleanup

General code cleanup
Nick Babcock hace 5 años
padre
commit
82ee57335b

+ 4 - 4
OhmGraphite.Test/PrometheusTest.cs

@@ -12,8 +12,8 @@ namespace OhmGraphite.Test
         public async void PrometheusTestServer()
         {
             var collector = new TestSensorCreator();
-            _ = new PrometheusCollection(collector, Metrics.DefaultRegistry);
-            var mserver = new MetricServer("localhost", 21881);
+            var registry = PrometheusCollection.SetupDefault(collector);
+            var mserver = new MetricServer("localhost", 21881, registry: registry);
             var server = new PrometheusServer(mserver, collector);
             try
             {
@@ -34,8 +34,8 @@ namespace OhmGraphite.Test
         public async void PrometheusNicGuid()
         {
             var collector = new NicGuidSensor();
-            _ = new PrometheusCollection(collector, Metrics.DefaultRegistry);
-            var mserver = new MetricServer("localhost", 21882);
+            var registry = PrometheusCollection.SetupDefault(collector);
+            var mserver = new MetricServer("localhost", 21882, registry: registry);
             var server = new PrometheusServer(mserver, collector);
             try
             {

+ 1 - 1
OhmGraphite/GraphiteWriter.cs

@@ -75,7 +75,7 @@ namespace OhmGraphite
         private static string NormalizedIdentifier(string host, ReportedValue sensor)
         {
             // Take the sensor's identifier (eg. /nvidiagpu/0/load/0)
-            // and tranform into nvidiagpu.0.load.<name> where <name>
+            // and transform into nvidiagpu.0.load.<name> where <name>
             // is the name of the sensor lowercased with spaces removed.
             // A name like "GPU Core" is turned into "gpucore". Also
             // since some names are like "cpucore#2", turn them into

+ 1 - 1
OhmGraphite/MetricTimer.cs

@@ -45,7 +45,7 @@ namespace OhmGraphite
             Logger.Debug("Starting to report metrics");
             try
             {
-                // Every 5 seconds (or superceding interval) we connect to graphite
+                // Every 5 seconds (or superseding interval) we connect to graphite
                 // and poll the hardware. It may be inefficient to open a new connection
                 // every 5 seconds, and there are ways to optimize this, but opening a
                 // new connection is the easiest way to ensure that previous failures

+ 4 - 5
OhmGraphite/Program.cs

@@ -1,5 +1,4 @@
-using System;
-using NLog;
+using NLog;
 using OpenHardwareMonitor.Hardware;
 using Prometheus;
 using Topshelf;
@@ -10,7 +9,7 @@ namespace OhmGraphite
     {
         private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
 
-        private static void Main(string[] args)
+        private static void Main()
         {
             HostFactory.Run(x =>
             {
@@ -67,8 +66,8 @@ namespace OhmGraphite
             else if (config.Prometheus != null)
             {
                 Logger.Info($"Prometheus port: {config.Prometheus.Port}");
-                var prometheusCollection = new PrometheusCollection(collector, Metrics.DefaultRegistry);
-                var server = new MetricServer(config.Prometheus.Host, config.Prometheus.Port);
+                var registry = PrometheusCollection.SetupDefault(collector);
+                var server = new MetricServer(config.Prometheus.Host, config.Prometheus.Port, registry: registry);
                 return new PrometheusServer(server, collector);
             }
             else if (config.Timescale != null)

+ 16 - 8
OhmGraphite/PrometheusCollection.cs

@@ -9,15 +9,23 @@ namespace OhmGraphite
     public class PrometheusCollection
     {
         private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
-        private static readonly Regex rx = new Regex("[^a-zA-Z0-9_:]", RegexOptions.Compiled);
+        private static readonly Regex Rx = new Regex("[^a-zA-Z0-9_:]", RegexOptions.Compiled);
         private readonly IGiveSensors _collector;
-        private MetricFactory _metrics;
+        private readonly MetricFactory _metrics;
 
-        public PrometheusCollection(IGiveSensors collector, CollectorRegistry registry)
+        public PrometheusCollection(IGiveSensors collector, MetricFactory metrics)
         {
             _collector = collector;
-            registry.AddBeforeCollectCallback(UpdateMetrics);
-            _metrics = Metrics.WithCustomRegistry(registry);
+            _metrics = metrics;
+        }
+
+        public static CollectorRegistry SetupDefault(IGiveSensors collector)
+        {
+            var registry = Metrics.DefaultRegistry;
+            var metrics = Metrics.WithCustomRegistry(registry);
+            var prometheusCollection = new PrometheusCollection(collector, metrics);
+            registry.AddBeforeCollectCallback(() => prometheusCollection.UpdateMetrics());
+            return registry;
         }
 
         public void UpdateMetrics()
@@ -25,7 +33,7 @@ namespace OhmGraphite
             Logger.LogAction("prometheus update metrics", PollSensors);
         }
 
-        private (string, double) BaseReport(ReportedValue report)
+        private static (string, double) BaseReport(ReportedValue report)
         {
             // Convert reported value into a base value by converting MB and GB into bytes, etc.
             // Flow rate is still liters per hour, even though liters per second may seem more
@@ -90,8 +98,8 @@ namespace OhmGraphite
             foreach (var sensor in _collector.ReadAllSensors())
             {
                 var (unit, value) = BaseReport(sensor);
-                var hw = Enum.GetName(typeof(HardwareType), sensor.HardwareType).ToLowerInvariant();
-                var name = rx.Replace($"ohm_{hw}_{unit}", "_");
+                var hw = Enum.GetName(typeof(HardwareType), sensor.HardwareType)?.ToLowerInvariant();
+                var name = Rx.Replace($"ohm_{hw}_{unit}", "_");
                 _metrics.CreateGauge(name, "Metric reported by open hardware sensor", "hardware", "sensor")
                     .WithLabels(sensor.Hardware, sensor.Sensor)
                     .Set(value);

+ 1 - 2
OhmGraphite/PrometheusConfig.cs

@@ -1,5 +1,4 @@
-
-namespace OhmGraphite
+namespace OhmGraphite
 {
     public class PrometheusConfig
     {

+ 1 - 1
OhmGraphite/TimescaleWriter.cs

@@ -66,7 +66,7 @@ namespace OhmGraphite
                         //
                         // OhmGraphite could never recover because Npgsql seemed adamant that the
                         // prepared statement existed. And since Npgsql persists prepared statements in
-                        // it's connection pool all future connections are "poisioned" with this
+                        // it's connection pool all future connections are "poisoned" with this
                         // prepared statement. The best solution appears to be unpreparing everything on
                         // db failure. For our use case, recreating these prepared statements is a small
                         // price to pay even if preparation is redundant.