public override Task Execute() { _process.Refresh(); // A very simple and not that accruate evaluation of how much CPU the process is take out of a core. double cpuPercentage = (_process.TotalProcessorTime - _startCpuTime).TotalMilliseconds / _cpuWatch.ElapsedMilliseconds; ApplicationInsightsTelemetry.TrackMetric(CpuMetric, cpuPercentage); if (_cpuThreshold.HasValue && _cpuThreshold.Value <= cpuPercentage) { _log.WriteMonitor(nameof(ResourcesMonitor), "", $"CPU usage is {cpuPercentage:0.##}"); } double memoryInMBytes = _process.WorkingSet64 / Mb; ApplicationInsightsTelemetry.TrackMetric(RamMetric, memoryInMBytes); if (_ramMbThreshold.HasValue && _ramMbThreshold.Value <= memoryInMBytes) { _log.WriteMonitor(nameof(ResourcesMonitor), "", $"RAM usage is {memoryInMBytes:0.##}"); } return(Task.CompletedTask); }
public IHttpActionResult Metric([FromBody] BasicMetricMetadata metricMetadata) { if (metricMetadata == null) { return(BadRequest("No metadata about the metric was specified")); } if (string.IsNullOrWhiteSpace(metricMetadata.Name)) { return(BadRequest("No metric name was specified")); } try { var applicationInsightsTelemetry = new ApplicationInsightsTelemetry(metricMetadata.InstrumentationKey); applicationInsightsTelemetry.TrackMetric(metricMetadata.Name, metricMetadata.Value, metricMetadata.CustomProperties); return(StatusCode(HttpStatusCode.NoContent)); } catch (InstrumentationKeyNotSpecifiedException) { return(Content(HttpStatusCode.InternalServerError, Constants.Errors.MissingInstrumentationKey)); } }