Ver Fonte

Format graphite tag values culture invariant

Nick Babcock há 6 anos atrás
pai
commit
7e487029f5
2 ficheiros alterados com 22 adições e 1 exclusões
  1. 21 0
      OhmGraphite.Test/FormatMetricsTest.cs
  2. 1 1
      OhmGraphite/GraphiteWriter.cs

+ 21 - 0
OhmGraphite.Test/FormatMetricsTest.cs

@@ -58,5 +58,26 @@ namespace OhmGraphite.Test
             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);
         }
+
+        [Fact]
+        public void FormatTagsCultureInvariant()
+        {
+            var writer = new GraphiteWriter("localhost", 2003, "MY-PC", true);
+            CultureInfo original = Thread.CurrentThread.CurrentCulture;
+            try
+            {
+                // de-DE culture will format 1.06 as 1,06 which graphite doesn't like
+                Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");
+
+                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);
+                string actual = writer.FormatGraphiteData(epoch, sensor);
+                Assert.Contains("1.06", actual);
+            }
+            finally
+            {
+                Thread.CurrentThread.CurrentCulture = original;
+            }
+        }
     }
 }

+ 1 - 1
OhmGraphite/GraphiteWriter.cs

@@ -125,7 +125,7 @@ namespace OhmGraphite
                    $"sensor_type={Enum.GetName(typeof(SensorType), data.SensorType)};" +
                    $"sensor_index={data.SensorIndex};" +
                    $"raw_name={GraphiteEscape(data.Sensor)} " +
-                   $"{data.Value} {epoch:d}";
+                   Invariant($"{data.Value} {epoch:d}");
         }
     }
 }