Skip to content

Commit 05c8517

Browse files
author
mwatson
committed
Changes around stopping metrics so it doesn't initialize HttpClient when it was never used or needed
1 parent db46624 commit 05c8517

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

Src/StackifyLib/Internal/Metrics/MetricClient.cs

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,18 @@ namespace StackifyLib.Internal.Metrics
1212
{
1313
public static class MetricClient
1414
{
15-
private static HttpClient _httpClient = new HttpClient(null, null);
15+
private static HttpClient _HttpClient = new HttpClient(null, null);
16+
17+
private static HttpClient HttpClient
18+
{
19+
get
20+
{
21+
if(_HttpClient == null)
22+
_HttpClient = new HttpClient(null, null);
23+
24+
return _HttpClient;
25+
}
26+
}
1627

1728
private readonly static ConcurrentQueue<Metric> _MetricQueue;
1829

@@ -25,6 +36,8 @@ public static class MetricClient
2536

2637
private static bool _StopRequested = false;
2738

39+
private static bool _MetricsEverUsed = false;
40+
2841
static MetricClient()
2942
{
3043
_MetricQueue = new ConcurrentQueue<Metric>();
@@ -147,6 +160,7 @@ public static LatestAggregate GetLatestMetric(string category, string metricName
147160

148161
public static void QueueMetric(Metric metric)
149162
{
163+
_MetricsEverUsed = true;
150164
try
151165
{
152166
//set a sanity cap
@@ -323,7 +337,8 @@ private static void UploadMetricsCheck(object state)
323337

324338
public static void StopMetricsQueue(string reason = "Unknown")
325339
{
326-
340+
if (!_MetricsEverUsed)
341+
return;
327342

328343
try
329344
{
@@ -367,18 +382,21 @@ public static bool UploadMetrics(DateTime currentMinute)
367382

368383
SetLatestAggregates(getForRecent);
369384

385+
//skip messing with HttpClient if nothing to do
386+
if (_AggregateMetrics.Count == 0)
387+
return true;
370388

371-
if (!_httpClient.MatchedClientDeviceApp())
389+
if (!HttpClient.MatchedClientDeviceApp())
372390
{
373391
// purgeOlderThan = DateTime.UtcNow;
374392
StackifyAPILogger.Log("Upload metrics skipped because we were unable to match the app to an app in Stackify");
375393
}
376-
else if (!_httpClient.IsAuthorized())
394+
else if (!HttpClient.IsAuthorized())
377395
{
378396
// purgeOlderThan = DateTime.UtcNow;
379397
StackifyAPILogger.Log("Upload metrics skipped authorization failure");
380398
}
381-
else if (!_httpClient.IsRecentError())
399+
else if (!HttpClient.IsRecentError())
382400
{
383401

384402
//If something happens at 2:39:45. The OccurredUtc is a rounded down value to 2:39. So we add a minute to ensure the minute has fully elapsed
@@ -480,10 +498,12 @@ private static bool UploadAggregates(List<KeyValuePair<string,MetricAggregate>>
480498
{
481499
bool allSuccess = true;
482500

501+
if (metrics.Count == 0)
502+
return true;
483503

484-
bool identifyResult = _httpClient.IdentifyApp();
504+
bool identifyResult = HttpClient.IdentifyApp();
485505

486-
if (identifyResult && _httpClient.MatchedClientDeviceApp() && !_httpClient.IsRecentError() && _httpClient.IsAuthorized())
506+
if (identifyResult && HttpClient.MatchedClientDeviceApp() && !HttpClient.IsRecentError() && HttpClient.IsAuthorized())
487507
{
488508
StackifyAPILogger.Log("Uploading Aggregate Metrics: " + metrics.Count);
489509

@@ -493,7 +513,7 @@ private static bool UploadAggregates(List<KeyValuePair<string,MetricAggregate>>
493513

494514
//in case the appid changes on the server side somehow and we need to update the monitorids we are adding the appid to the key
495515
//calling IdentifyApp() above will sometimes cause the library to sync with the server with the appid
496-
string keyWithAppID = string.Format("{0}-{1}", keyValuePair.Value.NameKey, _httpClient.AppIdentity.DeviceAppID);
516+
string keyWithAppID = string.Format("{0}-{1}", keyValuePair.Value.NameKey, HttpClient.AppIdentity.DeviceAppID);
497517

498518
if (!_MontorIDList.ContainsKey(keyWithAppID))
499519
{
@@ -550,7 +570,7 @@ private static bool UploadAggregates(List<KeyValuePair<string,MetricAggregate>>
550570
}
551571
else
552572
{
553-
StackifyAPILogger.Log("Metrics not uploaded. Identify Result: " + identifyResult + ", Metrics API Enabled: " + _httpClient.MatchedClientDeviceApp());
573+
StackifyAPILogger.Log("Metrics not uploaded. Identify Result: " + identifyResult + ", Metrics API Enabled: " + HttpClient.MatchedClientDeviceApp());
554574

555575
//if there was an issue trying to identify the app we could end up here and will want to try again later
556576
allSuccess = false;
@@ -581,9 +601,9 @@ private static bool UploadMetrics(List<MetricAggregate> metrics)
581601
model.Count = metric.Count;
582602
model.MonitorTypeID = (short)metric.MetricType;
583603

584-
if (_httpClient.AppIdentity != null)
604+
if (HttpClient.AppIdentity != null)
585605
{
586-
model.ClientDeviceID = _httpClient.AppIdentity.DeviceID;
606+
model.ClientDeviceID = HttpClient.AppIdentity.DeviceID;
587607
}
588608

589609
records.Add(model);
@@ -595,8 +615,8 @@ private static bool UploadMetrics(List<MetricAggregate> metrics)
595615

596616
string jsonData = JsonConvert.SerializeObject(records);
597617

598-
var response = _httpClient.SendJsonAndGetResponse(
599-
(_httpClient.BaseAPIUrl) +
618+
var response = HttpClient.SendJsonAndGetResponse(
619+
(HttpClient.BaseAPIUrl) +
600620
"Metrics/SubmitMetricsByID",
601621
jsonData);
602622

@@ -624,18 +644,18 @@ private static GetMetricResponse GetMonitorInfo(MetricAggregate metric)
624644
{
625645
try
626646
{
627-
if (_httpClient.IsRecentError() || !_httpClient.MatchedClientDeviceApp())
647+
if (HttpClient.IsRecentError() || !HttpClient.MatchedClientDeviceApp())
628648
{
629649
return null;
630650
}
631651

632652
GetMetricRequest request = new GetMetricRequest();
633653

634-
if (_httpClient.AppIdentity != null)
654+
if (HttpClient.AppIdentity != null)
635655
{
636-
request.DeviceAppID = _httpClient.AppIdentity.DeviceAppID;
637-
request.DeviceID = _httpClient.AppIdentity.DeviceID;
638-
request.AppNameID = _httpClient.AppIdentity.AppNameID;
656+
request.DeviceAppID = HttpClient.AppIdentity.DeviceAppID;
657+
request.DeviceID = HttpClient.AppIdentity.DeviceID;
658+
request.AppNameID = HttpClient.AppIdentity.AppNameID;
639659
}
640660
request.MetricName = metric.Name;
641661
request.MetricTypeID = (short)metric.MetricType;
@@ -645,8 +665,8 @@ private static GetMetricResponse GetMonitorInfo(MetricAggregate metric)
645665

646666

647667
var response =
648-
_httpClient.SendJsonAndGetResponse(
649-
(_httpClient.BaseAPIUrl) +
668+
HttpClient.SendJsonAndGetResponse(
669+
(HttpClient.BaseAPIUrl) +
650670
"Metrics/GetMetricInfo",
651671
jsonData);
652672

0 commit comments

Comments
 (0)