Exemplo n.º 1
0
        internal void CollectAllMetricsImpl()
        {
            _logger.Trace()?.Log("CollectAllMetrics started");

            var samplesFromAllProviders = CollectMetricsFromProviders();

            if (samplesFromAllProviders.IsEmpty())
            {
                _logger.Debug()?.Log("No metrics collected (no provider returned valid samples) - nothing to send to APM Server");
                return;
            }

            var metricSet = new MetricSet(TimeUtils.TimestampNow(), samplesFromAllProviders);

            try
            {
                _payloadSender.QueueMetrics(metricSet);
                _logger.Debug()
                ?.Log("Metrics collected: {data}",
                      samplesFromAllProviders.Any()
                                                        ? samplesFromAllProviders.Select(n => n.ToString()).Aggregate((i, j) => i + ", " + j)
                                                        : "no metrics collected");
            }
            catch (Exception e)
            {
                _logger.Error()
                ?.LogException(e, "Failed sending metrics through PayloadSender - metrics collection stops");
                _timer.Stop();
                _timer.Dispose();
            }
        }
Exemplo n.º 2
0
        public override Task Process(IEnumerable <MetricSample> items)
        {
            try
            {
                var metrics = new MyMetricSet {
                    Samples = items?.ToArray() ?? new MetricSample[0]
                };
                payloadSender.QueueMetrics(metrics);
            }
            catch
            {
                /* Add Logging and error handling */
                failureCount++;

                if (failureCount >= MaxTryWithoutSuccess)
                {
                    // TODO: Log failure exceeds threshold
                    base.Dispose();
                }
            }

            return(Task.CompletedTask);
        }
Exemplo n.º 3
0
        internal void CollectAllMetricsImpl()
        {
            _logger.Trace()?.Log("CollectAllMetrics started");

            var samplesFromAllProviders = new List <MetricSample>();

            // ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
            foreach (var metricsProvider in MetricsProviders)
            {
                if (metricsProvider.ConsecutiveNumberOfFailedReads == MaxTryWithoutSuccess)
                {
                    continue;
                }

                try
                {
                    _logger.Trace()?.Log("Start collecting {MetricsProviderName}", metricsProvider.DbgName);
                    var samplesFromCurrentProvider = metricsProvider.GetSamples();
                    if (samplesFromCurrentProvider != null)
                    {
                        var sampleArray = samplesFromCurrentProvider as MetricSample[] ?? samplesFromCurrentProvider.ToArray();
                        if (sampleArray.Any())
                        {
                            _logger.Trace()?.Log("Collected {MetricsProviderName} - adding it to MetricSet", metricsProvider.DbgName);
                            samplesFromAllProviders.AddRange(sampleArray);
                        }

                        metricsProvider.ConsecutiveNumberOfFailedReads = 0;
                    }
                    else
                    {
                        metricsProvider.ConsecutiveNumberOfFailedReads++;
                    }
                }
                catch (Exception e)
                {
                    metricsProvider.ConsecutiveNumberOfFailedReads++;
                    _logger.Error()
                    ?.LogException(e, "Failed reading {ProviderName} {NumberOfFail} times", metricsProvider.DbgName,
                                   metricsProvider.ConsecutiveNumberOfFailedReads);
                }

                if (metricsProvider.ConsecutiveNumberOfFailedReads != MaxTryWithoutSuccess)
                {
                    continue;
                }

                _logger.Info()
                ?.Log("Failed reading {operationName} {numberOfTimes} consecutively - the agent won't try reading {operationName} anymore",
                      metricsProvider.DbgName, metricsProvider.ConsecutiveNumberOfFailedReads, metricsProvider.DbgName);
            }

            var metricSet = new MetricSet(TimeUtils.TimestampNow(), samplesFromAllProviders);

            try
            {
                _payloadSender.QueueMetrics(metricSet);
                _logger.Debug()
                ?.Log("Metrics collected: {data}",
                      samplesFromAllProviders.Any()
                                                        ? samplesFromAllProviders.Select(n => n.ToString()).Aggregate((i, j) => i + ", " + j)
                                                        : "no metrics collected");
            }
            catch (Exception e)
            {
                _logger.Error()
                ?.LogException(e, "Failed sending metrics through PayloadSender - metrics collection stops");
                _timer.Stop();
                _timer.Dispose();
            }
        }
Exemplo n.º 4
0
        internal void CollectAllMetrics()
        {
            var sync = Interlocked.CompareExchange(ref _syncPoint, 1, 0);

            if (sync != 0)
            {
                return;
            }

            try
            {
                var samplesFromAllProviders = new List <MetricSample>();

                foreach (var metricsProvider in MetricsProviders)
                {
                    if (metricsProvider.ConsecutiveNumberOfFailedReads == MaxTryWithoutSuccess)
                    {
                        continue;
                    }

                    try
                    {
                        var samplesFromCurrentProvider = metricsProvider.GetSamples();
                        if (samplesFromCurrentProvider != null)
                        {
                            var sampleArray = samplesFromCurrentProvider as MetricSample[] ?? samplesFromCurrentProvider.ToArray();
                            if (sampleArray.Any())
                            {
                                samplesFromAllProviders.AddRange(sampleArray);
                            }

                            metricsProvider.ConsecutiveNumberOfFailedReads = 0;
                        }
                        else
                        {
                            metricsProvider.ConsecutiveNumberOfFailedReads++;
                        }
                    }
                    catch (Exception e)
                    {
                        metricsProvider.ConsecutiveNumberOfFailedReads++;
                        _logger.Error()
                        ?.LogException(e, "Failed reading {ProviderName} {NumberOfFail} times", metricsProvider.DbgName,
                                       metricsProvider.ConsecutiveNumberOfFailedReads);
                    }

                    if (metricsProvider.ConsecutiveNumberOfFailedReads != MaxTryWithoutSuccess)
                    {
                        continue;
                    }

                    _logger.Info()
                    ?.Log("Failed reading {operationName} {numberOfTimes} consecutively - the agent won't try reading {operationName} anymore",
                          metricsProvider.DbgName, metricsProvider.ConsecutiveNumberOfFailedReads, metricsProvider.DbgName);
                }

                var metricSet = new MetricSet(TimeUtils.TimestampNow(), samplesFromAllProviders);

                try
                {
                    _payloadSender.QueueMetrics(metricSet);
                    _logger.Debug()
                    ?.Log("Metrics collected: {data}",
                          samplesFromAllProviders.Any()
                                                                ? samplesFromAllProviders.Select(n => n.ToString()).Aggregate((i, j) => i + ", " + j)
                                                                : "no metrics collected");
                }
                catch (Exception e)
                {
                    _logger.Error()
                    ?.LogException(e, "Failed sending metrics through PayloadSender - metrics collection stops");
                    _timer.Stop();
                    _timer.Dispose();
                }
            }
            catch (Exception e)
            {
                _logger.Error()
                ?.LogExceptionWithCaller(e);
            }
            finally
            {
                _syncPoint = 0;
            }
        }