Esempio n. 1
0
        /// <summary>
        /// Resets the current snapshot and returns the previous one as the result.
        /// </summary>
        /// <returns>Returns the previous snapshot.</returns>
        public DnsMetricSnapshot ResetSnapshot()
        {
            DnsMetricSnapshot previousSnapshot = this.snapshot;

            Interlocked.Exchange(ref this.snapshot, new DnsMetricSnapshot());
            return(previousSnapshot);
        }
        /// <summary>
        /// Logs metrics periodically to the console.
        /// </summary>
        private void LogMetrics()
        {
            this.logger.LogTrace("()");

            try
            {
                // Print out total and period values.
                StringBuilder metricOutput = new StringBuilder();
                metricOutput.AppendLine("==========DNS Metrics==========");
                metricOutput.AppendLine();
                metricOutput.AppendFormat(this.MetricsOutputFormat, "Snapshot Time", this.dateTimeProvider.GetAdjustedTime());
                metricOutput.AppendLine();

                // Metrics since start.
                metricOutput.AppendLine(">>> Metrics since start");
                metricOutput.AppendFormat(this.MetricsOutputFormat, "Total DNS Requests", this.metrics.DnsRequestCountSinceStart);
                metricOutput.AppendFormat(this.MetricsOutputFormat, "Total DNS Server Failures (Restarted)", this.metrics.DnsServerFailureCountSinceStart);
                metricOutput.AppendFormat(this.MetricsOutputFormat, "Total DNS Request Failures", this.metrics.DnsRequestFailureCountSinceStart);
                metricOutput.AppendFormat(this.MetricsOutputFormat, "Maximum Peer Count", this.metrics.MaxPeerCountSinceStart);
                metricOutput.AppendLine();

                // Reset period values.
                DnsMetricSnapshot snapshot = this.metrics.ResetSnapshot();

                // Calculate averages.
                double averagePeerCount    = snapshot.DnsRequestCountSinceLastPeriod == 0 ? 0 : snapshot.PeerCountSinceLastPeriod / snapshot.DnsRequestCountSinceLastPeriod;
                double averageElapsedTicks = snapshot.DnsRequestCountSinceLastPeriod == 0 ? 0 : snapshot.DnsRequestElapsedTicksSinceLastPeriod / snapshot.DnsRequestCountSinceLastPeriod;

                // Metrics since last period.
                metricOutput.AppendLine($">>> Metrics for last period ({MetricsLogRate} secs)");
                metricOutput.AppendFormat(this.MetricsOutputFormat, "DNS Requests", snapshot.DnsRequestCountSinceLastPeriod);
                metricOutput.AppendFormat(this.MetricsOutputFormat, "DNS Server Failures (Restarted)", snapshot.DnsServerFailureCountSinceLastPeriod);
                metricOutput.AppendFormat(this.MetricsOutputFormat, "DNS Request Failures", snapshot.DnsRequestFailureCountSinceLastPeriod);
                metricOutput.AppendFormat(this.MetricsOutputFormat, "Average Peer Count", averagePeerCount);
                metricOutput.AppendFormat(this.MetricsOutputFormat, "Last Peer Count", snapshot.LastPeerCount);
                metricOutput.AppendFormat(this.MetricsOutputFormat, "Average Elapsed Time Processing DNS Requests (ms)", new TimeSpan((long)averageElapsedTicks).TotalMilliseconds);
                metricOutput.AppendFormat(this.MetricsOutputFormat, "Last Elapsed Time Processing DNS Requests (ms)", new TimeSpan(snapshot.LastDnsRequestElapsedTicks).TotalMilliseconds);
                metricOutput.AppendLine();

                // Output.
                this.logger.LogInformation(metricOutput.ToString());
            }
            catch (Exception e)
            {
                // If metrics fail, just log.
                this.logger.LogWarning(e, "Failed to output DNS metrics.");
            }

            this.logger.LogTrace("(-)");
        }