MetricConfig.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. INameResolution nameLookup = NameLookup(config["name_lookup"] ?? "netbios");
  31. var type = config["type"] ?? "graphite";
  32. GraphiteConfig gconfig = null;
  33. InfluxConfig iconfig = null;
  34. PrometheusConfig pconfig = null;
  35. TimescaleConfig timescale = null;
  36. switch (type.ToLowerInvariant())
  37. {
  38. case "graphite":
  39. gconfig = GraphiteConfig.ParseAppSettings(config);
  40. break;
  41. case "influxdb":
  42. case "influx":
  43. iconfig = InfluxConfig.ParseAppSettings(config);
  44. break;
  45. case "prometheus":
  46. pconfig = PrometheusConfig.ParseAppSettings(config);
  47. break;
  48. case "timescale":
  49. case "timescaledb":
  50. timescale = TimescaleConfig.ParseAppSettings(config);
  51. break;
  52. }
  53. return new MetricConfig(interval, nameLookup, gconfig, iconfig, pconfig, timescale);
  54. }
  55. private static INameResolution NameLookup(string lookup)
  56. {
  57. switch (lookup.ToLowerInvariant())
  58. {
  59. case "dns":
  60. return new DnsResolution();
  61. case "netbios":
  62. return new NetBiosResolution();
  63. default:
  64. return new StaticResolution(lookup);
  65. }
  66. }
  67. }
  68. }