PrometheusCollection.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using NLog;
  3. using OpenHardwareMonitor.Hardware;
  4. using Prometheus;
  5. namespace OhmGraphite
  6. {
  7. public class PrometheusCollection
  8. {
  9. private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
  10. private readonly IGiveSensors _collector;
  11. private readonly string _localHost;
  12. private MetricFactory _metrics;
  13. public PrometheusCollection(IGiveSensors collector, string localHost, CollectorRegistry registry)
  14. {
  15. _collector = collector;
  16. _localHost = localHost;
  17. registry.AddBeforeCollectCallback(UpdateMetrics);
  18. _metrics = Metrics.WithCustomRegistry(registry);
  19. }
  20. public void UpdateMetrics()
  21. {
  22. Logger.LogAction("prometheus update metrics", PollSensors);
  23. }
  24. private void PollSensors()
  25. {
  26. foreach (var sensor in _collector.ReadAllSensors())
  27. {
  28. _metrics.CreateGauge(
  29. sensor.Identifier.Substring(1)
  30. .Replace('/', '_')
  31. .Replace("{", null)
  32. .Replace("}", null)
  33. .Replace('-', '_'),
  34. "Metric reported by open hardware sensor",
  35. "host", "app", "hardware", "hardware_type", "sensor", "sensor_index")
  36. .WithLabels(_localHost, "ohm", sensor.Hardware,
  37. Enum.GetName(typeof(HardwareType), sensor.HardwareType),
  38. sensor.Sensor,
  39. sensor.SensorIndex.ToString())
  40. .Set(sensor.Value);
  41. }
  42. }
  43. }
  44. }