瀏覽代碼

Merge pull request #98 from nickbabcock/prom-hard

Prometheus: transmit hardware instance for disambiguation
Nick Babcock 5 年之前
父節點
當前提交
b617d51141

+ 5 - 5
OhmGraphite.Test/FormatMetricsTest.cs

@@ -13,7 +13,7 @@ namespace OhmGraphite.Test
         {
         {
             var writer = new GraphiteWriter("localhost", 2003, "MY-PC", false);
             var writer = new GraphiteWriter("localhost", 2003, "MY-PC", false);
             var epoch = new DateTimeOffset(new DateTime(2001, 1, 13), TimeSpan.Zero).ToUnixTimeSeconds();
             var epoch = new DateTimeOffset(new DateTime(2001, 1, 13), TimeSpan.Zero).ToUnixTimeSeconds();
-            var sensor = new ReportedValue("/my/cpu/identifier/1", "voltage", 1.06f, SensorType.Voltage, "cpu", HardwareType.CPU, 1);
+            var sensor = new ReportedValue("/my/cpu/identifier/1", "voltage", 1.06f, SensorType.Voltage, "cpu", HardwareType.CPU, "identifier", 1);
             string actual = writer.FormatGraphiteData(epoch, sensor);
             string actual = writer.FormatGraphiteData(epoch, sensor);
             Assert.Equal("ohm.MY-PC.my.cpu.identifier.voltage 1.06 979344000", actual);
             Assert.Equal("ohm.MY-PC.my.cpu.identifier.voltage 1.06 979344000", actual);
         }
         }
@@ -23,7 +23,7 @@ namespace OhmGraphite.Test
         {
         {
             var writer = new GraphiteWriter("localhost", 2003, "MY-PC", false);
             var writer = new GraphiteWriter("localhost", 2003, "MY-PC", false);
             var epoch = new DateTimeOffset(new DateTime(2001, 1, 13), TimeSpan.Zero).ToUnixTimeSeconds();
             var epoch = new DateTimeOffset(new DateTime(2001, 1, 13), TimeSpan.Zero).ToUnixTimeSeconds();
-            var sensor = new ReportedValue("/nic/{my-guid}/throughput/7", "Bluetooth Network Connection 2", 1.06f, SensorType.Throughput, "cpu", HardwareType.NIC, 7);
+            var sensor = new ReportedValue("/nic/{my-guid}/throughput/7", "Bluetooth Network Connection 2", 1.06f, SensorType.Throughput, "cpu", HardwareType.NIC, "{my-guid}", 7);
             string actual = writer.FormatGraphiteData(epoch, sensor);
             string actual = writer.FormatGraphiteData(epoch, sensor);
             Assert.Equal("ohm.MY-PC.nic.my-guid.throughput.bluetoothnetworkconnection2 1.06 979344000", actual);
             Assert.Equal("ohm.MY-PC.nic.my-guid.throughput.bluetoothnetworkconnection2 1.06 979344000", actual);
         }
         }
@@ -39,7 +39,7 @@ namespace OhmGraphite.Test
                 Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");
                 Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");
 
 
                 var epoch = new DateTimeOffset(new DateTime(2001, 1, 13), TimeSpan.Zero).ToUnixTimeSeconds();
                 var epoch = new DateTimeOffset(new DateTime(2001, 1, 13), TimeSpan.Zero).ToUnixTimeSeconds();
-                var sensor = new ReportedValue("/my/cpu/identifier/1", "voltage", 1.06f, SensorType.Voltage, "cpu", HardwareType.CPU, 1);
+                var sensor = new ReportedValue("/my/cpu/identifier/1", "voltage", 1.06f, SensorType.Voltage, "cpu", HardwareType.CPU, "identifier", 1);
                 string actual = writer.FormatGraphiteData(epoch, sensor);
                 string actual = writer.FormatGraphiteData(epoch, sensor);
                 Assert.Equal("ohm.MY-PC.my.cpu.identifier.voltage 1.06 979344000", actual);
                 Assert.Equal("ohm.MY-PC.my.cpu.identifier.voltage 1.06 979344000", actual);
             }
             }
@@ -54,7 +54,7 @@ namespace OhmGraphite.Test
         {
         {
             var writer = new GraphiteWriter("localhost", 2003, "MY-PC", true);
             var writer = new GraphiteWriter("localhost", 2003, "MY-PC", true);
             var epoch = new DateTimeOffset(new DateTime(2001, 1, 13), TimeSpan.Zero).ToUnixTimeSeconds();
             var epoch = new DateTimeOffset(new DateTime(2001, 1, 13), TimeSpan.Zero).ToUnixTimeSeconds();
-            var sensor = new ReportedValue("/my/cpu/identifier/1", "voltage", 1.06f, SensorType.Voltage, "cpu", HardwareType.CPU, 1);
+            var sensor = new ReportedValue("/my/cpu/identifier/1", "voltage", 1.06f, SensorType.Voltage, "cpu", HardwareType.CPU, "identifier", 1);
             string actual = writer.FormatGraphiteData(epoch, sensor);
             string actual = writer.FormatGraphiteData(epoch, sensor);
             Assert.Equal("ohm.MY-PC.my.cpu.identifier.voltage;host=MY-PC;app=ohm;hardware=cpu;hardware_type=CPU;sensor_type=Voltage;sensor_index=1;raw_name=voltage 1.06 979344000", actual);
             Assert.Equal("ohm.MY-PC.my.cpu.identifier.voltage;host=MY-PC;app=ohm;hardware=cpu;hardware_type=CPU;sensor_type=Voltage;sensor_index=1;raw_name=voltage 1.06 979344000", actual);
         }
         }
@@ -70,7 +70,7 @@ namespace OhmGraphite.Test
                 Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");
                 Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");
 
 
                 var epoch = new DateTimeOffset(new DateTime(2001, 1, 13), TimeSpan.Zero).ToUnixTimeSeconds();
                 var epoch = new DateTimeOffset(new DateTime(2001, 1, 13), TimeSpan.Zero).ToUnixTimeSeconds();
-                var sensor = new ReportedValue("/my/cpu/identifier/1", "voltage", 1.06f, SensorType.Voltage, "cpu", HardwareType.CPU, 1);
+                var sensor = new ReportedValue("/my/cpu/identifier/1", "voltage", 1.06f, SensorType.Voltage, "cpu", HardwareType.CPU, "identifier", 1);
                 string actual = writer.FormatGraphiteData(epoch, sensor);
                 string actual = writer.FormatGraphiteData(epoch, sensor);
                 Assert.Contains("1.06", actual);
                 Assert.Contains("1.06", actual);
             }
             }

+ 1 - 1
OhmGraphite.Test/PrometheusTest.cs

@@ -59,7 +59,7 @@ namespace OhmGraphite.Test
         {
         {
             public IEnumerable<ReportedValue> ReadAllSensors()
             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);
+                yield return new ReportedValue("/nic/{my-guid}/throughput/7", "Bluetooth Network Connection 2", 1.06f, SensorType.Throughput, "cpu", HardwareType.NIC, "{my-guid}", 7);
             }
             }
 
 
             public void Start()
             public void Start()

+ 3 - 3
OhmGraphite.Test/TestSensorCreator.cs

@@ -7,9 +7,9 @@ namespace OhmGraphite.Test
     {
     {
         public static IEnumerable<ReportedValue> Values()
         public static IEnumerable<ReportedValue> Values()
         {
         {
-            yield return new ReportedValue("/intelcpu/0/temperature/0", "CPU Core #1", 20, SensorType.Temperature, "Intel Core i7-6700K", HardwareType.CPU, 0);
-            yield return new ReportedValue("/intelcpu/0/temperature/1", "CPU Core #2", 15, SensorType.Temperature, "Intel Core i7-6700K", HardwareType.CPU, 1);
-            yield return new ReportedValue("/intelcpu/0/temperature/2", "CPU Core #3", 10, SensorType.Temperature, "Intel Core i7-6700K", HardwareType.CPU, 2);
+            yield return new ReportedValue("/intelcpu/0/temperature/0", "CPU Core #1", 20, SensorType.Temperature, "Intel Core i7-6700K", HardwareType.CPU, "0", 0);
+            yield return new ReportedValue("/intelcpu/0/temperature/1", "CPU Core #2", 15, SensorType.Temperature, "Intel Core i7-6700K", HardwareType.CPU, "0", 1);
+            yield return new ReportedValue("/intelcpu/0/temperature/2", "CPU Core #3", 10, SensorType.Temperature, "Intel Core i7-6700K", HardwareType.CPU, "0", 2);
         }
         }
 
 
         public IEnumerable<ReportedValue> ReadAllSensors() => Values();
         public IEnumerable<ReportedValue> ReadAllSensors() => Values();

+ 2 - 2
OhmGraphite/PrometheusCollection.cs

@@ -100,8 +100,8 @@ namespace OhmGraphite
                 var (unit, value) = BaseReport(sensor);
                 var (unit, value) = BaseReport(sensor);
                 var hw = Enum.GetName(typeof(HardwareType), sensor.HardwareType)?.ToLowerInvariant();
                 var hw = Enum.GetName(typeof(HardwareType), sensor.HardwareType)?.ToLowerInvariant();
                 var name = Rx.Replace($"ohm_{hw}_{unit}", "_");
                 var name = Rx.Replace($"ohm_{hw}_{unit}", "_");
-                _metrics.CreateGauge(name, "Metric reported by open hardware sensor", "hardware", "sensor")
-                    .WithLabels(sensor.Hardware, sensor.Sensor)
+                _metrics.CreateGauge(name, "Metric reported by open hardware sensor", "hardware", "sensor", "hw_instance")
+                    .WithLabels(sensor.Hardware, sensor.Sensor, sensor.HardwareInstance)
                     .Set(value);
                     .Set(value);
             }
             }
         }
         }

+ 4 - 0
OhmGraphite/ReportedValue.cs

@@ -10,6 +10,7 @@ namespace OhmGraphite
             SensorType sensorType,
             SensorType sensorType,
             string hardware,
             string hardware,
             HardwareType hardwareType,
             HardwareType hardwareType,
+            string hwInstance,
             int sensorIndex)
             int sensorIndex)
         {
         {
             Identifier = identifier;
             Identifier = identifier;
@@ -19,6 +20,7 @@ namespace OhmGraphite
             Hardware = hardware;
             Hardware = hardware;
             HardwareType = hardwareType;
             HardwareType = hardwareType;
             SensorIndex = sensorIndex;
             SensorIndex = sensorIndex;
+            HardwareInstance = hwInstance;
         }
         }
 
 
         public string Identifier { get; }
         public string Identifier { get; }
@@ -28,5 +30,7 @@ namespace OhmGraphite
         public string Hardware { get; }
         public string Hardware { get; }
         public HardwareType HardwareType { get; }
         public HardwareType HardwareType { get; }
         public int SensorIndex { get; }
         public int SensorIndex { get; }
+        public string HardwareInstance { get; }
+
     }
     }
 }
 }

+ 5 - 0
OhmGraphite/SensorCollector.cs

@@ -120,12 +120,17 @@ namespace OhmGraphite
             }
             }
             else
             else
             {
             {
+                var hwInstance = sensor.Hardware.Identifier.ToString();
+                var ind = hwInstance.LastIndexOf('/');
+                hwInstance = hwInstance.Substring(ind + 1);
+
                 yield return new ReportedValue(id,
                 yield return new ReportedValue(id,
                     sensor.Name,
                     sensor.Name,
                     sensor.Value.Value,
                     sensor.Value.Value,
                     sensor.SensorType,
                     sensor.SensorType,
                     sensor.Hardware.Name,
                     sensor.Hardware.Name,
                     sensor.Hardware.HardwareType,
                     sensor.Hardware.HardwareType,
+                    hwInstance,
                     sensor.Index);
                     sensor.Index);
             }
             }
         }
         }