MetricConfig.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. namespace OhmGraphite
  3. {
  4. public class MetricConfig
  5. {
  6. private readonly INameResolution _nameLookup;
  7. public MetricConfig(TimeSpan interval, INameResolution nameLookup, GraphiteConfig graphite, InfluxConfig influx,
  8. PrometheusConfig prometheus, TimescaleConfig timescale)
  9. {
  10. _nameLookup = nameLookup;
  11. Interval = interval;
  12. Graphite = graphite;
  13. Influx = influx;
  14. Prometheus = prometheus;
  15. Timescale = timescale;
  16. }
  17. public string LookupName() => _nameLookup.LookupName();
  18. public TimeSpan Interval { get; }
  19. public GraphiteConfig Graphite { get; }
  20. public InfluxConfig Influx { get; }
  21. public PrometheusConfig Prometheus { get; }
  22. public TimescaleConfig Timescale { get; }
  23. public static MetricConfig ParseAppSettings(IAppConfig config)
  24. {
  25. if (!int.TryParse(config["interval"], out int seconds))
  26. {
  27. seconds = 5;
  28. }
  29. var interval = TimeSpan.FromSeconds(seconds);
  30. var lookup = config["name_lookup"] ?? "netbios";
  31. INameResolution nameLookup;
  32. switch (lookup.ToLowerInvariant())
  33. {
  34. case "dns":
  35. nameLookup = new DnsResolution();
  36. break;
  37. default:
  38. nameLookup = new NetBiosResolution();
  39. break;
  40. }
  41. var type = config["type"] ?? "graphite";
  42. GraphiteConfig gconfig = null;
  43. InfluxConfig iconfig = null;
  44. PrometheusConfig pconfig = null;
  45. TimescaleConfig timescale = null;
  46. switch (type.ToLowerInvariant())
  47. {
  48. case "graphite":
  49. gconfig = GraphiteConfig.ParseAppSettings(config);
  50. break;
  51. case "influxdb":
  52. case "influx":
  53. iconfig = InfluxConfig.ParseAppSettings(config);
  54. break;
  55. case "prometheus":
  56. pconfig = PrometheusConfig.ParseAppSettings(config);
  57. break;
  58. case "timescale":
  59. case "timescaledb":
  60. timescale = TimescaleConfig.ParseAppSettings(config);
  61. break;
  62. }
  63. return new MetricConfig(interval, nameLookup, gconfig, iconfig, pconfig, timescale);
  64. }
  65. }
  66. }