This repository was archived by the owner on Jul 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathQuickPulseServiceClient.cs
More file actions
391 lines (332 loc) · 16.2 KB
/
QuickPulseServiceClient.cs
File metadata and controls
391 lines (332 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
namespace Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.Implementation.QuickPulse
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
#if NETSTANDARD2_0
using System.Runtime.InteropServices;
#endif
using System.Runtime.Serialization.Json;
using System.Threading;
using Helpers;
using Microsoft.ApplicationInsights.Extensibility.Filtering;
using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing;
using Microsoft.ManagementServices.RealTimeDataProcessing.QuickPulseService;
/// <summary>
/// Service client for QPS service.
/// </summary>
internal sealed class QuickPulseServiceClient : IQuickPulseServiceClient
{
private readonly string instanceName;
private readonly string streamId;
private readonly string machineName;
private readonly string version;
private readonly TimeSpan timeout = TimeSpan.FromSeconds(3);
private readonly Clock timeProvider;
private readonly bool isWebApp;
private readonly int processorCount;
private readonly DataContractJsonSerializer serializerDataPoint = new DataContractJsonSerializer(typeof(MonitoringDataPoint));
private readonly DataContractJsonSerializer serializerDataPointArray = new DataContractJsonSerializer(typeof(MonitoringDataPoint[]));
private readonly DataContractJsonSerializer deserializerServerResponse = new DataContractJsonSerializer(typeof(CollectionConfigurationInfo));
private readonly Dictionary<string, string> authOpaqueHeaderValues = new Dictionary<string, string>(StringComparer.Ordinal);
private readonly HttpClient httpClient = new HttpClient();
public QuickPulseServiceClient(
Uri serviceUri,
string instanceName,
string streamId,
string machineName,
string version,
Clock timeProvider,
bool isWebApp,
int processorCount,
TimeSpan? timeout = null)
{
this.ServiceUri = serviceUri;
this.instanceName = instanceName;
this.streamId = streamId;
this.machineName = machineName;
this.version = version;
this.timeProvider = timeProvider;
this.isWebApp = isWebApp;
this.processorCount = processorCount;
this.timeout = timeout ?? this.timeout;
foreach (string headerName in QuickPulseConstants.XMsQpsAuthOpaqueHeaderNames)
{
this.authOpaqueHeaderValues.Add(headerName, null);
}
}
public Uri ServiceUri { get; }
public bool? Ping(
string instrumentationKey,
DateTimeOffset timestamp,
string configurationETag,
string authApiKey,
out CollectionConfigurationInfo configurationInfo)
{
var requestUri = string.Format(
CultureInfo.InvariantCulture,
"{0}/ping?ikey={1}",
this.ServiceUri.AbsoluteUri.TrimEnd('/'),
Uri.EscapeUriString(instrumentationKey));
return this.SendRequest(
requestUri,
true,
configurationETag,
authApiKey,
out configurationInfo,
requestStream => this.WritePingData(timestamp, requestStream));
}
public bool? SubmitSamples(
IEnumerable<QuickPulseDataSample> samples,
string instrumentationKey,
string configurationETag,
string authApiKey,
out CollectionConfigurationInfo configurationInfo,
CollectionConfigurationError[] collectionConfigurationErrors)
{
var requestUri = string.Format(
CultureInfo.InvariantCulture,
"{0}/post?ikey={1}",
this.ServiceUri.AbsoluteUri.TrimEnd('/'),
Uri.EscapeUriString(instrumentationKey));
return this.SendRequest(
requestUri,
false,
configurationETag,
authApiKey,
out configurationInfo,
requestStream => this.WriteSamples(samples, instrumentationKey, requestStream, collectionConfigurationErrors));
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
private bool? SendRequest(
string requestUri,
bool includeIdentityHeaders,
string configurationETag,
string authApiKey,
out CollectionConfigurationInfo configurationInfo,
Action<Stream> onWriteRequestBody)
{
try
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUri);
this.AddHeaders(request, includeIdentityHeaders, configurationETag, authApiKey);
using (MemoryStream stream = new MemoryStream())
{
onWriteRequestBody(stream);
stream.Flush();
ArraySegment<byte> buffer = stream.TryGetBuffer(out buffer) ? buffer : new ArraySegment<byte>();
request.Content = new ByteArrayContent(buffer.Array, buffer.Offset, buffer.Count);
HttpResponseMessage response = this.httpClient.SendAsync(request, new CancellationTokenSource(this.timeout).Token).GetAwaiter().GetResult();
if (response == null)
{
configurationInfo = null;
return null;
}
return this.ProcessResponse(response, configurationETag, out configurationInfo);
}
}
catch (Exception e)
{
QuickPulseEventSource.Log.ServiceCommunicationFailedEvent(e.ToInvariantString());
}
configurationInfo = null;
return null;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "Dispose is known to perform safely on Stream and StreamReader types.")]
private bool? ProcessResponse(HttpResponseMessage response, string configurationETag, out CollectionConfigurationInfo configurationInfo)
{
configurationInfo = null;
bool isSubscribed;
if (!bool.TryParse(response.Headers.GetValuesSafe(QuickPulseConstants.XMsQpsSubscribedHeaderName).FirstOrDefault(), out isSubscribed))
{
// could not parse the isSubscribed value
// read the response out to avoid issues with TCP connections not being freed up
try
{
response.Content.LoadIntoBufferAsync().GetAwaiter().GetResult();
}
catch (Exception)
{
// we did our best, we don't care about the outcome anyway
}
return null;
}
foreach (string headerName in QuickPulseConstants.XMsQpsAuthOpaqueHeaderNames)
{
this.authOpaqueHeaderValues[headerName] = response.Headers.GetValuesSafe(headerName).FirstOrDefault();
}
string configurationETagHeaderValue = response.Headers.GetValuesSafe(QuickPulseConstants.XMsQpsConfigurationETagHeaderName).FirstOrDefault();
try
{
using (Stream responseStream = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult())
{
if (isSubscribed && !string.IsNullOrEmpty(configurationETagHeaderValue)
&& !string.Equals(configurationETagHeaderValue, configurationETag, StringComparison.Ordinal) && responseStream != null)
{
configurationInfo = this.deserializerServerResponse.ReadObject(responseStream) as CollectionConfigurationInfo;
}
}
}
catch (Exception e)
{
// couldn't read or deserialize the response
QuickPulseEventSource.Log.ServiceCommunicationFailedEvent(e.ToInvariantString());
}
return isSubscribed;
}
private static double Round(double value)
{
return Math.Round(value, 4, MidpointRounding.AwayFromZero);
}
private void WritePingData(DateTimeOffset timestamp, Stream stream)
{
var dataPoint = new MonitoringDataPoint
{
Version = this.version,
InvariantVersion = MonitoringDataPoint.CurrentInvariantVersion,
// InstrumentationKey = instrumentationKey, // ikey is currently set in query string parameter
Instance = this.instanceName,
StreamId = this.streamId,
MachineName = this.machineName,
Timestamp = timestamp.UtcDateTime,
IsWebApp = this.isWebApp,
PerformanceCollectionSupported = PerformanceCounterUtility.IsPerfCounterSupported(),
ProcessorCount = this.processorCount
};
this.serializerDataPoint.WriteObject(stream, dataPoint);
}
private void WriteSamples(IEnumerable<QuickPulseDataSample> samples, string instrumentationKey, Stream stream, CollectionConfigurationError[] errors)
{
var monitoringPoints = new List<MonitoringDataPoint>();
foreach (var sample in samples)
{
var metricPoints = new List<MetricPoint>();
metricPoints.AddRange(CreateDefaultMetrics(sample));
metricPoints.AddRange(
sample.PerfCountersLookup.Select(counter => new MetricPoint { Name = counter.Key, Value = Round(counter.Value), Weight = 1 }));
metricPoints.AddRange(CreateCalculatedMetrics(sample));
ITelemetryDocument[] documents = sample.TelemetryDocuments.ToArray();
Array.Reverse(documents);
ProcessCpuData[] topCpuProcesses =
sample.TopCpuData.Select(p => new ProcessCpuData() { ProcessName = p.Item1, CpuPercentage = p.Item2 }).ToArray();
var dataPoint = new MonitoringDataPoint
{
Version = this.version,
InvariantVersion = MonitoringDataPoint.CurrentInvariantVersion,
InstrumentationKey = instrumentationKey,
Instance = this.instanceName,
StreamId = this.streamId,
MachineName = this.machineName,
Timestamp = sample.EndTimestamp.UtcDateTime,
IsWebApp = this.isWebApp,
PerformanceCollectionSupported = PerformanceCounterUtility.IsPerfCounterSupported(),
ProcessorCount = this.processorCount,
Metrics = metricPoints.ToArray(),
Documents = documents,
GlobalDocumentQuotaReached = sample.GlobalDocumentQuotaReached,
TopCpuProcesses = topCpuProcesses.Length > 0 ? topCpuProcesses : null,
TopCpuDataAccessDenied = sample.TopCpuDataAccessDenied,
CollectionConfigurationErrors = errors
};
monitoringPoints.Add(dataPoint);
}
this.serializerDataPointArray.WriteObject(stream, monitoringPoints.ToArray());
}
private static IEnumerable<MetricPoint> CreateCalculatedMetrics(QuickPulseDataSample sample)
{
var metrics = new List<MetricPoint>();
foreach (AccumulatedValues metricAccumulatedValues in sample.CollectionConfigurationAccumulator.MetricAccumulators.Values)
{
try
{
MetricPoint metricPoint = new MetricPoint
{
Name = metricAccumulatedValues.MetricId,
Value = metricAccumulatedValues.CalculateAggregation(out long count),
Weight = (int)count
};
metrics.Add(metricPoint);
}
catch (Exception e)
{
// skip this metric
QuickPulseEventSource.Log.UnknownErrorEvent(e.ToString());
}
}
return metrics;
}
private static IEnumerable<MetricPoint> CreateDefaultMetrics(QuickPulseDataSample sample)
{
return new[]
{
new MetricPoint { Name = @"\ApplicationInsights\Requests/Sec", Value = Round(sample.AIRequestsPerSecond), Weight = 1 },
new MetricPoint
{
Name = @"\ApplicationInsights\Request Duration",
Value = Round(sample.AIRequestDurationAveInMs),
Weight = sample.AIRequests
},
new MetricPoint { Name = @"\ApplicationInsights\Requests Failed/Sec", Value = Round(sample.AIRequestsFailedPerSecond), Weight = 1 },
new MetricPoint
{
Name = @"\ApplicationInsights\Requests Succeeded/Sec",
Value = Round(sample.AIRequestsSucceededPerSecond),
Weight = 1
},
new MetricPoint { Name = @"\ApplicationInsights\Dependency Calls/Sec", Value = Round(sample.AIDependencyCallsPerSecond), Weight = 1 },
new MetricPoint
{
Name = @"\ApplicationInsights\Dependency Call Duration",
Value = Round(sample.AIDependencyCallDurationAveInMs),
Weight = sample.AIDependencyCalls
},
new MetricPoint
{
Name = @"\ApplicationInsights\Dependency Calls Failed/Sec",
Value = Round(sample.AIDependencyCallsFailedPerSecond),
Weight = 1
},
new MetricPoint
{
Name = @"\ApplicationInsights\Dependency Calls Succeeded/Sec",
Value = Round(sample.AIDependencyCallsSucceededPerSecond),
Weight = 1
},
new MetricPoint { Name = @"\ApplicationInsights\Exceptions/Sec", Value = Round(sample.AIExceptionsPerSecond), Weight = 1 }
};
}
private void AddHeaders(HttpRequestMessage request, bool includeIdentityHeaders, string configurationETag, string authApiKey)
{
request.Headers.TryAddWithoutValidation(QuickPulseConstants.XMsQpsTransmissionTimeHeaderName, this.timeProvider.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture));
request.Headers.TryAddWithoutValidation(QuickPulseConstants.XMsQpsConfigurationETagHeaderName, configurationETag);
request.Headers.TryAddWithoutValidation(QuickPulseConstants.XMsQpsAuthApiKeyHeaderName, authApiKey ?? string.Empty);
foreach (string headerName in QuickPulseConstants.XMsQpsAuthOpaqueHeaderNames)
{
request.Headers.TryAddWithoutValidation(headerName, this.authOpaqueHeaderValues[headerName]);
}
if (includeIdentityHeaders)
{
request.Headers.TryAddWithoutValidation(QuickPulseConstants.XMsQpsInstanceNameHeaderName, this.instanceName);
request.Headers.TryAddWithoutValidation(QuickPulseConstants.XMsQpsStreamIdHeaderName, this.streamId);
request.Headers.TryAddWithoutValidation(QuickPulseConstants.XMsQpsMachineNameHeaderName, this.machineName);
request.Headers.TryAddWithoutValidation(QuickPulseConstants.XMsQpsInvariantVersionHeaderName,
MonitoringDataPoint.CurrentInvariantVersion.ToString(CultureInfo.InvariantCulture));
}
}
private void Dispose(bool disposing)
{
if (disposing)
{
this.httpClient.Dispose();
}
}
}
}