/// <summary> /// Used to make sure we report 0 values if nothing new comes in /// </summary> public static void HandleZeroReports(DateTime currentMinute) { foreach (var item in _LastAggregates) { MetricSetting setting; if (_MetricSettings.TryGetValue(item.Value.NameKey, out setting)) { if (setting == null) { MetricSetting remove; _MetricSettings.TryRemove(item.Value.NameKey, out remove); continue; } MetricAggregate agg = new MetricAggregate(item.Value.Category, item.Value.Name, item.Value.MetricType); agg.OccurredUtc = currentMinute; switch (item.Value.MetricType) { case MetricType.Counter: setting.AutoReportLastValueIfNothingReported = false;//do not allow this break; case MetricType.CounterTime: setting.AutoReportLastValueIfNothingReported = false; //do not allow this break; case MetricType.MetricAverage: break; case MetricType.MetricLast: break; } if (setting.AutoReportZeroIfNothingReported) { agg.Count = 1; agg.Value = 0; } else if (setting.AutoReportLastValueIfNothingReported) { agg.Count = item.Value.Count; agg.Value = item.Value.Value; } else { continue; } string aggKey = agg.AggregateKey(); if (!_AggregateMetrics.ContainsKey(aggKey)) { agg.NameKey = item.Value.NameKey; StackifyAPILogger.Log("Creating 0 default value for " + aggKey); _AggregateMetrics[aggKey] = agg; } } } }
private static GetMetricResponse GetMonitorInfo(MetricAggregate metric) { try { if (_httpClient.IsRecentError() || !_httpClient.MatchedClientDeviceApp()) { return null; } GetMetricRequest request = new GetMetricRequest(); if (_httpClient.AppIdentity != null) { request.DeviceAppID = _httpClient.AppIdentity.DeviceAppID; request.DeviceID = _httpClient.AppIdentity.DeviceID; request.AppNameID = _httpClient.AppIdentity.AppNameID; } request.MetricName = metric.Name; request.MetricTypeID = (short)metric.MetricType; request.Category = metric.Category; string jsonData = JsonConvert.SerializeObject(request); var response = _httpClient.SendAndGetResponse( System.Web.VirtualPathUtility.AppendTrailingSlash(_httpClient.BaseAPIUrl) + "Metrics/GetMetricInfo", jsonData); if (response.Exception == null && response.StatusCode == HttpStatusCode.OK) { var metricResponse = JsonConvert.DeserializeObject<GetMetricResponse>(response.ResponseText); return metricResponse; } return null; } catch (Exception e) { StackifyAPILogger.Log("Error getting monitor info " + e.Message); return null; } }
private static void Aggregate(MetricAggregate aggregate) { try { string aggKey = aggregate.AggregateKey(); MetricAggregate agg; if (!_AggregateMetrics.TryGetValue(aggKey, out agg)) { if (_AggregateMetrics.Count > 1000) { Utils.StackifyAPILogger.Log("No longer aggregating new metrics because more than 1000 are queued"); return; } StackifyAPILogger.Log("Creating aggregate for " + aggKey); _AggregateMetrics[aggKey] = aggregate; agg = aggregate; } if (aggregate.MetricType == MetricType.MetricLast) { agg.Count = 1; agg.Value = aggregate.Value; } else { agg.Count += aggregate.Count; agg.Value += aggregate.Value; } _AggregateMetrics[aggKey] = agg; } catch (Exception ex) { StackifyAPILogger.Log("Error in StackifyLib with aggregating metrics"); } }