1
0
Эх сурвалжийг харах

Merge pull request #44 from nickbabcock/prom-guid

Fix prometheus and nic guids
Nick Babcock 6 жил өмнө
parent
commit
88a884cd45

+ 41 - 1
OhmGraphite.Test/PrometheusTest.cs

@@ -1,4 +1,6 @@
-using System.Net.Http;
+using System.Collections.Generic;
+using System.Net.Http;
+using OpenHardwareMonitor.Hardware;
 using Prometheus;
 using Xunit;
 
@@ -27,5 +29,43 @@ namespace OhmGraphite.Test
                 server.Stop();
             }
         }
+
+        [Fact]
+        public async void PrometheusNicGuid()
+        {
+            var collector = new NicGuidSensor();
+            var prometheusCollection = new PrometheusCollection(collector, "my-pc", Metrics.DefaultRegistry);
+            var mserver = new MetricServer("localhost", 21881);
+            var server = new PrometheusServer(mserver, collector);
+            try
+            {
+                server.Start();
+                var client = new HttpClient();
+                var resp = await client.GetAsync("http://localhost:21881/metrics");
+                Assert.True(resp.IsSuccessStatusCode);
+                var content = await resp.Content.ReadAsStringAsync();
+                Assert.Contains("Bluetooth Network Connection 2", content);
+            }
+            finally
+            {
+                server.Stop();
+            }
+        }
+
+        public class NicGuidSensor : IGiveSensors
+        {
+            public IEnumerable<ReportedValue> ReadAllSensors()
+            {
+                yield return new ReportedValue("/nic/{my-guid}/throughput/7", "Bluetooth Network Connection 2", 1.06f, SensorType.Throughput, "cpu", HardwareType.NIC, 7);
+            }
+
+            public void Start()
+            {
+            }
+
+            public void Stop()
+            {
+            }
+        }
     }
 }

+ 5 - 1
OhmGraphite/PrometheusCollection.cs

@@ -30,7 +30,11 @@ namespace OhmGraphite
             foreach (var sensor in _collector.ReadAllSensors())
             {
                 _metrics.CreateGauge(
-                        sensor.Identifier.Substring(1).Replace('/', '_'),
+                        sensor.Identifier.Substring(1)
+                            .Replace('/', '_')
+                            .Replace("{", null)
+                            .Replace("}", null)
+                            .Replace('-', '_'),
                         "Metric reported by open hardware sensor",
                         "host", "app", "hardware", "hardware_type", "sensor", "sensor_index")
                     .WithLabels(_localHost, "ohm", sensor.Hardware,