protected void Spectate(CancellationToken token, TaskScheduler scheduler = null) { var metricPrefix = _configuration.MetricPrefix; try { Log.InfoFormat("Sampling for {0} configured metric definitions", _configuration.Metrics.Count); var options = new ParallelOptions { CancellationToken = token, MaxDegreeOfParallelism = 8, TaskScheduler = scheduler }; var allMetrics = new ConcurrentBag <Metric>(); Parallel.ForEach(_configuration.Metrics, options, metric => { var queryableSource = _queryableSourceFactory.Create(metric.Source); var metricValues = queryableSource.QueryValue(metric.Path).ToList(); Log.DebugFormat(" -> Queried '{0}' from '{1}', resulting in {2} metric samples (unfiltered)", metric.Path, metric.Source, metricValues.Count); foreach (var sample in metricValues) { if (!Included(metric.Include, sample.Instance) || Excluded(metric.Exclude, sample.Instance)) { continue; } var metricName = _metricFormatter.Format(metricPrefix, sample.Instance, metric.Template); allMetrics.Add(new Metric(metricName, sample.Value, metric.Type)); } }); Log.DebugFormat("Obtained {0} metrics to publish, publishing now...", allMetrics.Count); foreach (var metric in allMetrics) { _publisher.Publish(metric); } } catch (Exception ex) { Log.Error("Exception occurred while spectating", ex); } }