|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Net; |
| 7 | +using System.Reactive.Concurrency; |
| 8 | +using System.Text; |
| 9 | +using System.Threading; |
| 10 | +using Prometheus.Advanced; |
| 11 | + |
| 12 | +namespace Prometheus |
| 13 | +{ |
| 14 | + public class MetricPusher : MetricHandler |
| 15 | + { |
| 16 | + private const string ContentType = "text/plain; version=0.0.4"; |
| 17 | + private readonly TimeSpan _schedulerInterval; |
| 18 | + private readonly Uri _endpoint; |
| 19 | + private readonly object _syncObj = new object(); |
| 20 | + |
| 21 | + public MetricPusher(string endpoint, string job, string instance = null, long intervalMilliseconds = 1000, IEnumerable<Tuple<string, string>> additionalLabels = null, IEnumerable<IOnDemandCollector> standardCollectors = null, ICollectorRegistry registry = null) : base(standardCollectors, registry) |
| 22 | + { |
| 23 | + if (string.IsNullOrEmpty(endpoint)) |
| 24 | + { |
| 25 | + throw new ArgumentNullException("endpoint"); |
| 26 | + } |
| 27 | + if (string.IsNullOrEmpty(job)) |
| 28 | + { |
| 29 | + throw new ArgumentNullException("job"); |
| 30 | + } |
| 31 | + if (intervalMilliseconds <= 0) |
| 32 | + { |
| 33 | + throw new ArgumentException("Interval must be greater than zero", "intervalMilliseconds"); |
| 34 | + } |
| 35 | + StringBuilder sb = new StringBuilder(string.Format("{0}/job/{1}", endpoint.TrimEnd('/'), job)); |
| 36 | + if (!string.IsNullOrEmpty(instance)) |
| 37 | + { |
| 38 | + sb.AppendFormat("/instance/{0}", instance); |
| 39 | + } |
| 40 | + if (additionalLabels != null) |
| 41 | + { |
| 42 | + foreach (var pair in additionalLabels) |
| 43 | + { |
| 44 | + if (pair == null || string.IsNullOrEmpty(pair.Item1) || string.IsNullOrEmpty(pair.Item2)) |
| 45 | + { |
| 46 | + Trace.WriteLine("Ignoring invalid label set"); |
| 47 | + continue; |
| 48 | + } |
| 49 | + sb.AppendFormat("/{0}/{1}", pair.Item1, pair.Item2); |
| 50 | + } |
| 51 | + } |
| 52 | + if (!Uri.TryCreate(sb.ToString(), UriKind.Absolute, out _endpoint)) |
| 53 | + { |
| 54 | + throw new ArgumentException("Endpoint must be a valid url", "endpoint"); |
| 55 | + } |
| 56 | + |
| 57 | + _schedulerInterval = TimeSpan.FromMilliseconds(intervalMilliseconds); |
| 58 | + } |
| 59 | + |
| 60 | + protected override IDisposable StartLoop(IScheduler scheduler) |
| 61 | + { |
| 62 | + return scheduler.SchedulePeriodic(_schedulerInterval, SendMetrics); |
| 63 | + } |
| 64 | + |
| 65 | + private void SendMetrics() |
| 66 | + { |
| 67 | + if (Monitor.TryEnter(_syncObj)) |
| 68 | + { |
| 69 | + try |
| 70 | + { |
| 71 | + using (var stream = new MemoryStream()) |
| 72 | + { |
| 73 | + ScrapeHandler.ProcessScrapeRequest(_registry.CollectAll(), ContentType, stream); |
| 74 | + using (var client = new WebClient()) |
| 75 | + { |
| 76 | + client.UploadData(_endpoint, "POST", stream.ToArray()); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + catch (Exception e) |
| 81 | + { |
| 82 | + Trace.WriteLine(string.Format("Exception in send metrics: {0}", e)); |
| 83 | + } |
| 84 | + finally |
| 85 | + { |
| 86 | + Monitor.Exit(_syncObj); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments