|
| 1 | +using App.Metrics; |
| 2 | +using App.Metrics.Serialization; |
| 3 | +using Newtonsoft.Json; |
| 4 | +using Newtonsoft.Json.Serialization; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.IO; |
| 8 | +using System.Linq; |
| 9 | + |
| 10 | +namespace Picton.Messaging.IntegrationTests.Datadog |
| 11 | +{ |
| 12 | + class MetricSnapshotDatadogWriter : IMetricSnapshotWriter |
| 13 | + { |
| 14 | + private StreamWriter _streamWriter; |
| 15 | + private readonly DatadogFormatterOptions _options; |
| 16 | + private readonly List<MetricJson> _metrics = new List<MetricJson>(); |
| 17 | + |
| 18 | + static readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings |
| 19 | + { |
| 20 | + ContractResolver = new LowercaseContractResolver() |
| 21 | + }; |
| 22 | + |
| 23 | + private class LowercaseContractResolver : DefaultContractResolver |
| 24 | + { |
| 25 | + protected override string ResolvePropertyName(string propertyName) |
| 26 | + { |
| 27 | + return propertyName.ToLower(); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public MetricSnapshotDatadogWriter(StreamWriter streamWriter, DatadogFormatterOptions options) |
| 32 | + { |
| 33 | + _streamWriter = streamWriter; |
| 34 | + _options = options; |
| 35 | + } |
| 36 | + |
| 37 | + public void Dispose() |
| 38 | + { |
| 39 | + if (_streamWriter != null) |
| 40 | + { |
| 41 | + Flush(); |
| 42 | + |
| 43 | + _streamWriter?.Dispose(); |
| 44 | + _streamWriter = null; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// <inheritdoc /> |
| 49 | + public void Write(string context, string name, object value, MetricTags tags, DateTime timestamp) |
| 50 | + { |
| 51 | + Write(context, name, new[] { "value" }, new[] { value }, tags, timestamp); |
| 52 | + } |
| 53 | + |
| 54 | + /// <inheritdoc /> |
| 55 | + public void Write(string context, string name, IEnumerable<string> columns, IEnumerable<object> values, MetricTags tags, DateTime timestamp) |
| 56 | + { |
| 57 | + var posixTimestamp = (timestamp - new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero)).TotalSeconds; //TODO: Check this |
| 58 | + |
| 59 | + var dict = columns.Zip(values, (column, value) => new { column, value }).ToDictionary(p => p.column, p => p.value); |
| 60 | + |
| 61 | + switch (tags.Values[Array.IndexOf(tags.Keys, "mtype")]) |
| 62 | + { |
| 63 | + case "apdex": |
| 64 | + Write(posixTimestamp, context, name, "count", dict["samples"]); |
| 65 | + Write(posixTimestamp, context, name, "score", dict["score"]); |
| 66 | + Write(posixTimestamp, context, name, "satisfied", dict["satisfied"]); |
| 67 | + Write(posixTimestamp, context, name, "tolerating", dict["tolerating"]); |
| 68 | + Write(posixTimestamp, context, name, "frustrating", dict["frustrating"]); |
| 69 | + break; |
| 70 | + case "gauge": |
| 71 | + Write(posixTimestamp, context, name, dict["value"]); |
| 72 | + break; |
| 73 | + case "counter": |
| 74 | + if (dict.ContainsKey("value")) |
| 75 | + Write(posixTimestamp, context, name, dict["value"]); |
| 76 | + break; |
| 77 | + case "meter": |
| 78 | + Write(posixTimestamp, context, name, "count", dict["count.meter"]); |
| 79 | + Write(posixTimestamp, context, name, "15m", dict["rate15m"]); |
| 80 | + Write(posixTimestamp, context, name, "5m", dict["rate5m"]); |
| 81 | + Write(posixTimestamp, context, name, "1m", dict["rate1m"]); |
| 82 | + Write(posixTimestamp, context, name, "avg", dict["rate.mean"]); |
| 83 | + break; |
| 84 | + case "timer": |
| 85 | + Write(posixTimestamp, context, name, "1mrate", dict["rate1m"]); |
| 86 | + Write(posixTimestamp, context, name, "5mrate", dict["rate5m"]); |
| 87 | + Write(posixTimestamp, context, name, "15mrate", dict["rate15m"]); |
| 88 | + WriteHistogram(posixTimestamp, context, name, dict); |
| 89 | + break; |
| 90 | + case "histogram": |
| 91 | + WriteHistogram(posixTimestamp, context, name, dict); |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private void WriteHistogram(double posixTimestamp, string context, string name, Dictionary<string, object> dict) |
| 97 | + { |
| 98 | + Write(posixTimestamp, context, name, "count", dict["count.hist"]); |
| 99 | + |
| 100 | + Write(posixTimestamp, context, name, "max", dict["max"]); |
| 101 | + Write(posixTimestamp, context, name, "avg", dict["mean"]); |
| 102 | + Write(posixTimestamp, context, name, "median", dict["median"]); |
| 103 | + Write(posixTimestamp, context, name, "min", dict["min"]); |
| 104 | + Write(posixTimestamp, context, name, "stdDev", dict["stddev"]); |
| 105 | + |
| 106 | + Write(posixTimestamp, context, name, "75percentile", dict["p75"]); |
| 107 | + Write(posixTimestamp, context, name, "95percentile", dict["p95"]); |
| 108 | + Write(posixTimestamp, context, name, "98percentile", dict["p98"]); |
| 109 | + Write(posixTimestamp, context, name, "99percentile", dict["p99"]); |
| 110 | + Write(posixTimestamp, context, name, "999percentile", dict["p999"]); |
| 111 | + } |
| 112 | + |
| 113 | + private void Write(double timestamp, string context, string name, string subname, object value) |
| 114 | + { |
| 115 | + Write(timestamp, context, name + "." + subname, value); |
| 116 | + } |
| 117 | + |
| 118 | + private void Write(double timestamp, string context, string name, object value) |
| 119 | + { |
| 120 | + _metrics.Add(new MetricJson |
| 121 | + { |
| 122 | + Host = _options.Hostname, |
| 123 | + Metric = context + "." + name, |
| 124 | + //Tags = tags.Values, |
| 125 | + Points = new[] |
| 126 | + { |
| 127 | + new[] { timestamp, value } |
| 128 | + } |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + private void Flush() |
| 133 | + { |
| 134 | + _streamWriter.Write(JsonConvert.SerializeObject(new SeriesJson { Series = _metrics.ToArray() }, JsonSettings)); |
| 135 | + } |
| 136 | + |
| 137 | + public GeneratedMetricNameMapping MetricNameMapping { get; } = new GeneratedMetricNameMapping(); |
| 138 | + } |
| 139 | +} |
0 commit comments