|
| 1 | +// <copyright file="MetricsExporterThread.cs" company="OpenCensus Authors"> |
| 2 | +// Copyright 2018, OpenCensus Authors |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of theLicense at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// </copyright> |
| 16 | + |
| 17 | +namespace OpenCensus.Exporter.ApplicationInsights.Implementation |
| 18 | +{ |
| 19 | + using System; |
| 20 | + using System.Diagnostics; |
| 21 | + using System.Threading; |
| 22 | + using Microsoft.ApplicationInsights; |
| 23 | + using Microsoft.ApplicationInsights.DataContracts; |
| 24 | + using Microsoft.ApplicationInsights.Extensibility; |
| 25 | + using OpenCensus.Stats; |
| 26 | + using OpenCensus.Stats.Aggregations; |
| 27 | + |
| 28 | + internal class MetricsExporterThread |
| 29 | + { |
| 30 | + private readonly IViewManager viewManager; |
| 31 | + |
| 32 | + private readonly TelemetryClient telemetryClient; |
| 33 | + |
| 34 | + private readonly TimeSpan collectionInterval = TimeSpan.FromSeconds(10); |
| 35 | + |
| 36 | + private readonly TimeSpan cancellationInterval = TimeSpan.FromMilliseconds(10); |
| 37 | + |
| 38 | + private readonly CancellationToken token; |
| 39 | + |
| 40 | + public MetricsExporterThread(TelemetryConfiguration telemetryConfiguration, IViewManager viewManager, CancellationToken token, TimeSpan collectionInterval) |
| 41 | + { |
| 42 | + this.telemetryClient = new TelemetryClient(telemetryConfiguration); |
| 43 | + this.viewManager = viewManager; |
| 44 | + this.collectionInterval = collectionInterval; |
| 45 | + this.token = token; |
| 46 | + } |
| 47 | + |
| 48 | + public void WorkerThread() |
| 49 | + { |
| 50 | + try |
| 51 | + { |
| 52 | + var sleepInterval = this.collectionInterval; |
| 53 | + Stopwatch sw = new Stopwatch(); |
| 54 | + |
| 55 | + while (!this.token.IsCancellationRequested) |
| 56 | + { |
| 57 | + sw.Start(); |
| 58 | + this.Export(); |
| 59 | + sw.Stop(); |
| 60 | + |
| 61 | + // adjust interval for data collection time |
| 62 | + sleepInterval = this.collectionInterval.Subtract(sw.Elapsed); |
| 63 | + |
| 64 | + // allow faster thread cancellation |
| 65 | + while (sleepInterval > this.cancellationInterval && !this.token.IsCancellationRequested) |
| 66 | + { |
| 67 | + Thread.Sleep(this.cancellationInterval); |
| 68 | + sleepInterval.Subtract(this.cancellationInterval); |
| 69 | + } |
| 70 | + |
| 71 | + Thread.Sleep(sleepInterval); |
| 72 | + } |
| 73 | + } |
| 74 | + catch (Exception) |
| 75 | + { |
| 76 | + // TODO: report error |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + internal void Export() |
| 81 | + { |
| 82 | + foreach (var view in this.viewManager.AllExportedViews) |
| 83 | + { |
| 84 | + var data = this.viewManager.GetView(view.Name); |
| 85 | + |
| 86 | + foreach (var value in data.AggregationMap) |
| 87 | + { |
| 88 | + var metricTelemetry = new MetricTelemetry |
| 89 | + { |
| 90 | + Name = data.View.Name.AsString |
| 91 | + }; |
| 92 | + |
| 93 | + for (int i = 0; i < value.Key.Values.Count; i++) |
| 94 | + { |
| 95 | + var name = data.View.Columns[i].Name; |
| 96 | + var val = value.Key.Values[i].AsString; |
| 97 | + metricTelemetry.Properties.Add(name, val); |
| 98 | + } |
| 99 | + |
| 100 | + // Now those propertis needs to be populated. |
| 101 | + // |
| 102 | + // metricTelemetry.Sum |
| 103 | + // metricTelemetry.Count |
| 104 | + // metricTelemetry.Max |
| 105 | + // metricTelemetry.Min |
| 106 | + // metricTelemetry.StandardDeviation |
| 107 | + // |
| 108 | + // See data model for clarification on the meaning of those fields. |
| 109 | + // https://docs.microsoft.com/azure/application-insights/application-insights-data-model-metric-telemetry |
| 110 | + |
| 111 | + value.Value.Match<object>( |
| 112 | + (combined) => |
| 113 | + { |
| 114 | + if (combined is ISumDataDouble sum) |
| 115 | + { |
| 116 | + metricTelemetry.Sum = sum.Sum; |
| 117 | + } |
| 118 | + return null; |
| 119 | + }, |
| 120 | + (combined) => |
| 121 | + { |
| 122 | + if (combined is ISumDataLong sum) |
| 123 | + { |
| 124 | + metricTelemetry.Sum = sum.Sum; |
| 125 | + } |
| 126 | + return null; |
| 127 | + }, |
| 128 | + (combined) => |
| 129 | + { |
| 130 | + if (combined is ICountData count) |
| 131 | + { |
| 132 | + metricTelemetry.Sum = count.Count; |
| 133 | + } |
| 134 | + return null; |
| 135 | + }, |
| 136 | + (combined) => |
| 137 | + { |
| 138 | + if (combined is IMeanData mean) |
| 139 | + { |
| 140 | + } |
| 141 | + return null; |
| 142 | + }, |
| 143 | + (combined) => |
| 144 | + { |
| 145 | + if (combined is IDistributionData dist) |
| 146 | + { |
| 147 | + metricTelemetry.Sum = dist.Mean; |
| 148 | + metricTelemetry.Min = dist.Min; |
| 149 | + metricTelemetry.Max = dist.Max; |
| 150 | + metricTelemetry.StandardDeviation = dist.SumOfSquaredDeviations; |
| 151 | + } |
| 152 | + |
| 153 | + return null; |
| 154 | + }, |
| 155 | + (combined) => |
| 156 | + { |
| 157 | + if (combined is ILastValueDataDouble lastValue) |
| 158 | + { |
| 159 | + metricTelemetry.Sum = lastValue.LastValue; |
| 160 | + } |
| 161 | + return null; |
| 162 | + }, |
| 163 | + (combined) => |
| 164 | + { |
| 165 | + if (combined is ILastValueDataLong lastValue) |
| 166 | + { |
| 167 | + metricTelemetry.Sum = lastValue.LastValue; |
| 168 | + } |
| 169 | + return null; |
| 170 | + }, |
| 171 | + (combined) => |
| 172 | + { |
| 173 | + if (combined is IAggregationData aggregationData) |
| 174 | + { |
| 175 | + // TODO: report an error |
| 176 | + } |
| 177 | + return null; |
| 178 | + }); |
| 179 | + this.telemetryClient.TrackMetric(metricTelemetry); |
| 180 | + } |
| 181 | + |
| 182 | + Console.WriteLine(view); |
| 183 | + Console.WriteLine(data); |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments