PrometheusTest.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.Collections.Generic;
  2. using System.Net.Http;
  3. using Prometheus;
  4. using Xunit;
  5. namespace OhmGraphite.Test
  6. {
  7. public class PrometheusTest
  8. {
  9. [Fact]
  10. public async void PrometheusTestServer()
  11. {
  12. var collector = new TestSensorCreator();
  13. var registry = PrometheusCollection.SetupDefault(collector);
  14. var mserver = new MetricServer("localhost", 21881, registry: registry);
  15. using (var server = new PrometheusServer(mserver, collector))
  16. using (var client = new HttpClient())
  17. {
  18. server.Start();
  19. var resp = await client.GetAsync("http://localhost:21881/metrics");
  20. Assert.True(resp.IsSuccessStatusCode);
  21. var content = await resp.Content.ReadAsStringAsync();
  22. Assert.Contains("# HELP ohm_cpu_celsius Metric reported by open hardware sensor", content);
  23. }
  24. }
  25. [Fact]
  26. public async void PrometheusNicGuid()
  27. {
  28. var collector = new NicGuidSensor();
  29. var registry = PrometheusCollection.SetupDefault(collector);
  30. var mserver = new MetricServer("localhost", 21882, registry: registry);
  31. using (var server = new PrometheusServer(mserver, collector))
  32. using (var client = new HttpClient())
  33. {
  34. server.Start();
  35. var resp = await client.GetAsync("http://localhost:21882/metrics");
  36. Assert.True(resp.IsSuccessStatusCode);
  37. var content = await resp.Content.ReadAsStringAsync();
  38. Assert.Contains("Bluetooth Network Connection 2", content);
  39. }
  40. }
  41. [Fact]
  42. public void PrometheusStopServerWithoutStarting()
  43. {
  44. var collector = new NicGuidSensor();
  45. var registry = PrometheusCollection.SetupDefault(collector);
  46. var mserver = new MetricServer("localhost", 21883, registry: registry);
  47. var server = new PrometheusServer(mserver, collector);
  48. // Asserting that the following doesn't throw when the server
  49. // is disposed before starting
  50. server.Dispose();
  51. }
  52. public class NicGuidSensor : IGiveSensors
  53. {
  54. public IEnumerable<ReportedValue> ReadAllSensors()
  55. {
  56. yield return new ReportedValue("/nic/{my-guid}/throughput/7", "Bluetooth Network Connection 2", 1.06f, SensorType.Throughput, "cpu", HardwareType.NIC, "{my-guid}", 7);
  57. }
  58. public void Start()
  59. {
  60. }
  61. public void Dispose()
  62. {
  63. }
  64. }
  65. }
  66. }