GraphiteWriter.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using NLog;
  9. using static System.FormattableString;
  10. namespace OhmGraphite
  11. {
  12. public class GraphiteWriter : IWriteMetrics
  13. {
  14. private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
  15. private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
  16. private readonly string _localHost;
  17. private readonly string _remoteHost;
  18. private readonly int _remotePort;
  19. private readonly bool _tags;
  20. private TcpClient _client = new TcpClient();
  21. private bool _failure = true;
  22. private static readonly Encoding Utf8NoBom = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
  23. public GraphiteWriter(string remoteHost, int remotePort, string localHost, bool tags)
  24. {
  25. _remoteHost = remoteHost;
  26. _remotePort = remotePort;
  27. _tags = tags;
  28. _localHost = localHost;
  29. }
  30. public async Task ReportMetrics(DateTime reportTime, IEnumerable<ReportedValue> sensors)
  31. {
  32. // Since the graphite writer keeps the same connection open across
  33. // writes, we need to ensure that only one thread has access to
  34. // the connection at a time. Multiple threads can be in this
  35. // method when the time it takes to poll and write the data is
  36. // longer than the interval time. However we don't want an
  37. // unbounded number of threads stuck waiting to write, so
  38. // jettison any attempt after waiting for more than a second.
  39. if (!await _semaphore.WaitAsync(TimeSpan.FromSeconds(1)))
  40. {
  41. throw new ApplicationException("unable to acquire lock on graphite connection");
  42. }
  43. try
  44. {
  45. await SendGraphite(reportTime, sensors);
  46. }
  47. finally
  48. {
  49. _semaphore.Release();
  50. }
  51. }
  52. private async Task SendGraphite(DateTime reportTime, IEnumerable<ReportedValue> sensors)
  53. {
  54. try
  55. {
  56. // Reconnect whenever the previous network attempt failed or first
  57. // time connections
  58. if (_failure || !_client.Connected)
  59. {
  60. _client.Close();
  61. _client = new TcpClient();
  62. Logger.Debug($"New connection to {_remoteHost}:{_remotePort}");
  63. await _client.ConnectAsync(_remoteHost, _remotePort);
  64. }
  65. // We don't want to transmit metrics across multiple seconds as they
  66. // are being retrieved so calculate the timestamp of the signaled event
  67. // only once.
  68. long epoch = new DateTimeOffset(reportTime).ToUnixTimeSeconds();
  69. // Create a stream writer that leaves the underlying stream open
  70. // when the writer is closed, as we don't want our TCP connection
  71. // closed too. Since this requires the four param constructor for
  72. // the stream writer, the encoding and buffer sized are copied from
  73. // the C# reference source.
  74. using (var writer = new StreamWriter(_client.GetStream(), Utf8NoBom, bufferSize: 1024, leaveOpen: true))
  75. {
  76. foreach (var sensor in sensors)
  77. {
  78. await writer.WriteLineAsync(FormatGraphiteData(epoch, sensor));
  79. }
  80. }
  81. await _client.GetStream().FlushAsync();
  82. _failure = false;
  83. }
  84. catch (SocketException)
  85. {
  86. _failure = true;
  87. throw;
  88. }
  89. }
  90. private static string NormalizedIdentifier(string host, ReportedValue sensor)
  91. {
  92. // Take the sensor's identifier (eg. /nvidiagpu/0/load/0)
  93. // and transform into nvidiagpu.0.load.<name> where <name>
  94. // is the name of the sensor lowercased with spaces removed.
  95. // A name like "GPU Core" is turned into "gpucore". Also
  96. // since some names are like "cpucore#2", turn them into
  97. // separate metrics by replacing "#" with "."
  98. string identifier = sensor.Identifier.Replace('/', '.').Substring(1);
  99. identifier = identifier.Remove(identifier.LastIndexOf('.')).Replace("{", null).Replace("}", null);
  100. string name = sensor.Sensor.ToLower().Replace(" ", null).Replace('#', '.');
  101. return $"ohm.{host}.{identifier}.{name}";
  102. }
  103. public static string GraphiteEscape(string src)
  104. {
  105. // Formula for escaping graphite data is taken from
  106. // collectd's utils_format_graphite.c
  107. var builder = new StringBuilder(src.Length);
  108. foreach (char c in src)
  109. {
  110. if (c == '.' || char.IsWhiteSpace(c) || char.IsControl(c))
  111. {
  112. builder.Append('-');
  113. }
  114. else
  115. {
  116. builder.Append(c);
  117. }
  118. }
  119. return builder.ToString();
  120. }
  121. public string FormatGraphiteData(long epoch, ReportedValue data)
  122. {
  123. // Graphite API wants <metric> <value> <timestamp>. We prefix the metric
  124. // with `ohm` as to not overwrite potentially existing metrics
  125. string id = NormalizedIdentifier(_localHost, data);
  126. if (!_tags)
  127. {
  128. return Invariant($"{id} {data.Value} {epoch:d}");
  129. }
  130. return $"{id};" +
  131. $"host={_localHost};" +
  132. "app=ohm;" +
  133. $"hardware={GraphiteEscape(data.Hardware)};" +
  134. $"hardware_type={Enum.GetName(typeof(HardwareType), data.HardwareType)};" +
  135. $"sensor_type={Enum.GetName(typeof(SensorType), data.SensorType)};" +
  136. $"sensor_index={data.SensorIndex};" +
  137. $"raw_name={GraphiteEscape(data.Sensor)} " +
  138. Invariant($"{data.Value} {epoch:d}");
  139. }
  140. public void Dispose()
  141. {
  142. _client?.Dispose();
  143. }
  144. }
  145. }