MetricConfig.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. namespace OhmGraphite
  3. {
  4. public class MetricConfig
  5. {
  6. public MetricConfig(TimeSpan interval, GraphiteConfig graphite, InfluxConfig influx, PrometheusConfig prometheus, string timescale)
  7. {
  8. Interval = interval;
  9. Graphite = graphite;
  10. Influx = influx;
  11. Prometheus = prometheus;
  12. Timescale = timescale;
  13. }
  14. public TimeSpan Interval { get; }
  15. public GraphiteConfig Graphite { get; }
  16. public InfluxConfig Influx { get; }
  17. public PrometheusConfig Prometheus { get; }
  18. public string Timescale { get; }
  19. public static MetricConfig ParseAppSettings(IAppConfig config)
  20. {
  21. if (!int.TryParse(config["interval"], out int seconds))
  22. {
  23. seconds = 5;
  24. }
  25. var interval = TimeSpan.FromSeconds(seconds);
  26. var type = config["type"] ?? "graphite";
  27. GraphiteConfig gconfig = null;
  28. InfluxConfig iconfig = null;
  29. PrometheusConfig pconfig = null;
  30. string timescale = null;
  31. switch (type.ToLowerInvariant())
  32. {
  33. case "graphite":
  34. gconfig = GraphiteConfig.ParseAppSettings(config);
  35. break;
  36. case "influxdb":
  37. case "influx":
  38. iconfig = InfluxConfig.ParseAppSettings(config);
  39. break;
  40. case "prometheus":
  41. pconfig = PrometheusConfig.ParseAppSettings(config);
  42. break;
  43. case "timescale":
  44. case "timescaledb":
  45. timescale = config["timescale_connection"];
  46. break;
  47. }
  48. return new MetricConfig(interval, gconfig, iconfig, pconfig, timescale);
  49. }
  50. }
  51. }