示例#1
0
        /// <summary>
        /// Instantiates a new collector. You should typically only instantiate one collector for the lifetime of your
        /// application. It will manage the serialization of metrics and sending data to metric handlers.
        /// </summary>
        /// <param name="options">
        /// <see cref="MetricsCollectorOptions" /> representing the options to use for this collector.
        /// </param>
        public MetricsCollector(MetricsCollectorOptions options)
        {
            ExceptionHandler  = options.ExceptionHandler ?? (_ => { });
            ThrowOnPostFail   = options.ThrowOnPostFail;
            ThrowOnQueueFull  = options.ThrowOnQueueFull;
            ReportingInterval = options.SnapshotInterval;
            FlushInterval     = options.FlushInterval;
            RetryInterval     = options.RetryInterval;
            RetryCount        = options.RetryCount;

            _endpoints = options.Endpoints?.ToImmutableArray() ?? ImmutableArray <MetricEndpoint> .Empty;
            _sources   = options.Sources?.ToImmutableArray() ?? ImmutableArray <MetricSource> .Empty;
            _batches   = _endpoints.IsDefaultOrEmpty ? Array.Empty <IMetricReadingBatch>() : new IMetricReadingBatch[_endpoints.Length];
        }
示例#2
0
        /// <summary>
        /// Instantiates a new collector. You should typically only instantiate one collector for the lifetime of your
        /// application. It will manage the serialization of metrics and sending data to metric handlers.
        /// </summary>
        /// <param name="options">
        /// <see cref="MetricsCollectorOptions" /> representing the options to use for this collector.
        /// </param>
        public MetricsCollector(MetricsCollectorOptions options)
        {
            ExceptionHandler  = options.ExceptionHandler ?? (_ => { });
            MetricsNamePrefix = options.MetricsNamePrefix ?? "";
            if (MetricsNamePrefix != "" && !MetricValidation.IsValidMetricName(MetricsNamePrefix))
            {
                throw new Exception("\"" + MetricsNamePrefix + "\" is not a valid metric name prefix.");
            }

            _endpoints = options.Endpoints?.ToImmutableArray() ?? ImmutableArray <MetricEndpoint> .Empty;
            _sets      = options.Sets?.ToImmutableArray() ?? ImmutableArray <IMetricSet> .Empty;

            ThrowOnPostFail   = options.ThrowOnPostFail;
            ThrowOnQueueFull  = options.ThrowOnQueueFull;
            ReportingInterval = options.SnapshotInterval;
            FlushInterval     = options.FlushInterval;
            RetryInterval     = options.RetryInterval;
            PropertyToTagName = options.PropertyToTagName;
            TagValueConverter = options.TagValueConverter;
            DefaultTags       = ValidateDefaultTags(options.DefaultTags);

            _maxRetries          = 3;
            _shutdownTokenSource = new CancellationTokenSource();

            // initialize any metric sets
            foreach (var set in _sets)
            {
                set.Initialize(this);
            }

            // start continuous queue-flushing
            _flushTask = Task.Run(
                async() =>
            {
                while (!_shutdownTokenSource.IsCancellationRequested)
                {
                    await Task.Delay(FlushInterval);

                    try
                    {
                        await FlushAsync(true);
                    }
                    catch (Exception ex)
                    {
                        SendExceptionToHandler(ex);
                    }
                }
            });

            // start reporting timer
            _reportingTask = Task.Run(
                async() =>
            {
                while (!_shutdownTokenSource.IsCancellationRequested)
                {
                    await Task.Delay(ReportingInterval);

                    try
                    {
                        await SnapshotAsync(true);
                    }
                    catch (Exception ex)
                    {
                        SendExceptionToHandler(ex);
                    }
                }
            });
        }