Exemple #1
0
        public static IMetricsWriter WriteSample(
            this IMetricsWriter writer,
            double value,
            string suffix = "",
            IReadOnlyList <string> labelNames  = null,
            IReadOnlyList <string> labelValues = null,
            long?timestamp = null)
        {
            var sampleWriter = writer.StartSample(suffix);

            if ((labelValues != null) && (labelValues.Count > 0))
            {
                var labelWriter = sampleWriter.StartLabels();
                labelWriter.WriteLabels(labelNames, labelValues);
                labelWriter.EndLabels();
            }

            sampleWriter.WriteValue(value);
            if (timestamp.HasValue)
            {
                sampleWriter.WriteTimestamp(timestamp.Value);
            }

            sampleWriter.EndSample();
            return(writer);
        }
        public void Collect(IMetricsWriter writer)
        {
            _process.Refresh();

            writer.WriteMetricHeader(_cpuSecondsTotalName, MetricType.Counter, "Total user and system CPU time spent in seconds");
            writer.WriteSample(_process.TotalProcessorTime.TotalSeconds);
            writer.EndMetric();

            writer.WriteMetricHeader(_virtualBytesName, MetricType.Gauge, "Process virtual memory size");
            writer.WriteSample(_process.VirtualMemorySize64);
            writer.EndMetric();

            writer.WriteMetricHeader(_workingSetName, MetricType.Gauge, "Process working set");
            writer.WriteSample(_process.WorkingSet64);
            writer.EndMetric();

            writer.WriteMetricHeader(_privateBytesName, MetricType.Gauge, "Process private memory size");
            writer.WriteSample(_process.PrivateMemorySize64);
            writer.EndMetric();

            writer.WriteMetricHeader(_numThreadsName, MetricType.Gauge, "Total number of threads");
            writer.WriteSample(_process.Threads.Count);
            writer.EndMetric();

            writer.WriteMetricHeader(_processIdName, MetricType.Gauge, "Process ID");
            writer.WriteSample(_process.Id);
            writer.EndMetric();

            writer.WriteMetricHeader(_startTimeSecondsName, MetricType.Gauge, "Start time of the process since unix epoch in seconds");
            writer.WriteSample(_processStartTime);
            writer.EndMetric();
        }
Exemple #3
0
        protected internal override void Collect(IMetricsWriter writer)
        {
            long cumulativeCount = 0L;

            for (int i = 0; i < _bucketsStore.Buckets.Length; i++)
            {
                cumulativeCount += _bucketsStore.Buckets[i].Value;
                var bucketSample = writer.StartSample("_bucket");
                var labelWriter  = bucketSample.StartLabels();
                if (LabelValues != null && LabelValues.Count > 0)
                {
                    labelWriter.WriteLabels(Configuration.LabelNames, LabelValues);
                }

                string labelValue = Configuration.FormattedBuckets[i];
                labelWriter.WriteLabel("le", labelValue);
                labelWriter.EndLabels();

                bucketSample.WriteValue(cumulativeCount);
                if (Timestamp.HasValue)
                {
                    bucketSample.WriteTimestamp(Timestamp.Value);
                }

                bucketSample.EndSample();
            }

            writer.WriteSample(_sum.Value, "_sum", Configuration.LabelNames, LabelValues, Timestamp);
            writer.WriteSample(cumulativeCount, "_count", Configuration.LabelNames, LabelValues, Timestamp);
        }
        protected internal override void Collect(IMetricsWriter writer)
        {
            long cumulativeCount = 0L;

            for (int i = 0; i < _bucketCounts.Length; i++)
            {
                cumulativeCount += _bucketCounts[i].Value;
                var bucketSample = writer.StartSample("_bucket");
                var labelWriter  = bucketSample.StartLabels();
                if (Labels != null)
                {
                    labelWriter.WriteLabels(Labels);
                }

                string labelValue = double.IsPositiveInfinity(Configuration.Buckets[i]) ? "+Inf" : Configuration.FormattedBuckets[i];
                labelWriter.WriteLabel("le", labelValue);
                labelWriter.EndLabels();

                bucketSample.WriteValue(cumulativeCount);
                if (Timestamp.HasValue)
                {
                    bucketSample.WriteTimestamp(Timestamp.Value);
                }

                bucketSample.EndSample();
            }

            writer.WriteSample(_sum.Value, "_sum", Labels, Timestamp);
            writer.WriteSample(cumulativeCount, "_count", Labels, Timestamp);
        }
Exemple #5
0
            protected internal override void Collect(IMetricsWriter writer)
            {
                var values = _arrayPool.Rent(Configuration.SortedObjectives.Count);

                try
                {
                    ForkState(DateTime.UtcNow, out var count, out var sum, values);

                    for (int i = 0; i < Configuration.SortedObjectives.Count; i++)
                    {
                        var bucketSample = writer.StartSample();
                        var labelWriter  = bucketSample.StartLabels();
                        labelWriter.WriteLabels(Labels);
                        labelWriter.WriteLabel("quantile", Configuration.FormattedObjectives[i]);
                        labelWriter.EndLabels();

                        bucketSample.WriteValue(values[i]);
                        if (Timestamp.HasValue)
                        {
                            bucketSample.WriteTimestamp(Timestamp.Value);
                        }

                        bucketSample.EndSample();
                    }

                    writer.WriteSample(sum, "_sum", Labels, Timestamp);
                    writer.WriteSample(count, "_count", Labels, Timestamp);
                }
                finally
                {
                    _arrayPool.Return(values);
                }
            }
 public CountingObjectReader(IObjectReader inner, IMetricsWriter writer, string key)
 {
     _inner  = inner ?? throw new ArgumentNullException(nameof(inner));
     _writer = writer ?? throw new ArgumentNullException(nameof(writer));
     _prefix = "reader.";
     if (key.Length != 0)
     {
         _prefix = _prefix + key + ".";
     }
 }
Exemple #7
0
        public void CollectTo(IMetricsWriter writer)
        {
            var wrapped = new MetricWriterWrapper(writer);

            foreach (var collector in _enumerableCollectors.Value)
            {
                wrapped.SetCurrentCollector(collector);
                collector.Collect(wrapped);
            }
        }
Exemple #8
0
        public void Collect(IMetricsWriter writer)
        {
            writer.WriteMetricHeader(Configuration.Name, Type, Configuration.Help);
            Unlabelled.Collect(writer);

            foreach (var labelledMetric in LabelledMetrics)
            {
                labelledMetric.Value.Collect(writer);
            }
        }
Exemple #9
0
 public OriginReaderResolver(
     IMetricsWriter metricsWriter,
     ILogger <TracingObjectSource> objectSourceLogger,
     ILogger <TracingObjectReader> objectReaderLogger,
     RecyclableMemoryStreamManager streamManager)
 {
     _metricsWriter      = metricsWriter ?? throw new ArgumentNullException(nameof(metricsWriter));
     _objectSourceLogger = objectSourceLogger ?? throw new ArgumentNullException(nameof(objectSourceLogger));
     _objectReaderLogger = objectReaderLogger ?? throw new ArgumentNullException(nameof(objectReaderLogger));
     _streamManager      = streamManager ?? throw new ArgumentNullException(nameof(streamManager));
 }
Exemple #10
0
        public async Task CollectToAsync(IMetricsWriter writer)
        {
            var wrapped = new MetricWriterWrapper(writer);

            foreach (var collector in _enumerableCollectors.Value)
            {
                wrapped.SetCurrentCollector(collector);
                collector.Collect(wrapped);
                await writer.FlushAsync().ConfigureAwait(false);
            }
        }
 public DestinationWriterResolver(
     IMetricsWriter metricsWriter,
     ILogger <TracingObjectWriter> objectLogger,
     ILogger <TracingObjectSource> objectSourceLogger,
     ILogger <TracingObjectWriterLocker> lockerLogger)
 {
     _metricsWriter      = metricsWriter ?? throw new ArgumentNullException(nameof(metricsWriter));
     _objectLogger       = objectLogger ?? throw new ArgumentNullException(nameof(objectLogger));
     _objectSourceLogger = objectSourceLogger ?? throw new ArgumentNullException(nameof(objectSourceLogger));
     _lockerLogger       = lockerLogger ?? throw new ArgumentNullException(nameof(lockerLogger));
 }
        public async Task Invoke(HttpContext context,
                                 IMetricQueriesProvider queriesProvider,
                                 IOptions <ExporterOptions> optionsProvider,
                                 IMetricQueryExecutor queryExecutor,
                                 IMetricsWriter metricsWriter,
                                 ILogger <OndemandMetricsEvaluationMiddleware> logger)
        {
            var options     = optionsProvider.Value;
            var defaultMode = options.Metrics.Evaluation.Mode;

            var queries = await queriesProvider.GetAsync(context.RequestAborted);

            var tasks = queries.Select(q =>
            {
                if (q.EvaluationMode == MetricsEvaluationMode.Scheduled)
                {
                    return(Task.CompletedTask);
                }

                if (!q.EvaluationMode.HasValue && defaultMode == MetricsEvaluationMode.Scheduled)
                {
                    return(Task.CompletedTask);
                }

                return(Task.Run(async() =>
                {
                    try
                    {
                        logger.LogInformation($"Executing on-demand query '{q.Name}'");

                        var timeout = q.Timeout.GetValueOrDefault(options.Metrics.Evaluation.Timeout);
                        var result = await queryExecutor.ExecuteAsync(q, timeout, context.RequestAborted);

                        await metricsWriter.WriteAsync(q, result, context.RequestAborted);

                        logger.LogInformation($"Executed on-demand query '{q.Name}'");
                    }
                    catch (OperationCanceledException)
                    {
                        // Ignore
                    }
                    catch (Exception e)
                    {
                        logger.LogError(e, $"Unable to write metric for query '{q.Name}'");
                    }
                }, context.RequestAborted));
            });

            await Task.WhenAll(tasks);

            await _next(context);
        }
        public void Collect(IMetricsWriter writer)
        {
            writer.WriteMetricHeader(Name, MetricType.Counter, _help);

            for (var gen = 0; gen <= GC.MaxGeneration; gen++)
            {
                writer.StartSample()
                .StartLabels()
                .WriteLabel("generation", gen.ToString())
                .EndLabels()
                .WriteValue(GC.CollectionCount(gen));
            }
        }
 public ScheduledMetricsEvaluator(
     ILogger <ScheduledMetricsEvaluator> logger,
     IMetricQueryExecutor queryExecutor,
     IMetricsWriter metricsWriter,
     IOptions <ExporterOptions> optionsProvider,
     IBackgroundJobClient jobClient)
 {
     _logger          = logger;
     _queryExecutor   = queryExecutor;
     _metricsWriter   = metricsWriter;
     _optionsProvider = optionsProvider;
     _jobClient       = jobClient;
 }
        public static IMetricsWriter WriteMetricHeader(
            this IMetricsWriter writer,
            string metricName,
            MetricType metricType,
            string help = "")
        {
            writer.StartMetric(metricName);
            if (!string.IsNullOrEmpty(help))
            {
                writer.WriteHelp(help);
            }

            writer.WriteType(metricType);
            return(writer);
        }
        public void Collect(IMetricsWriter writer)
        {
            writer.WriteMetricHeader(_name, MetricType.Counter, _help);

            for (var gen = 0; gen <= GC.MaxGeneration; gen++)
            {
                writer.StartSample()
                .StartLabels()
                .WriteLabel("generation", _genNames[gen])
                .EndLabels()
                .WriteValue(GC.CollectionCount(gen))
                .EndSample();
            }

            writer.EndMetric();
        }
Exemple #17
0
        void ICollector.Collect(IMetricsWriter writer)
        {
            writer.WriteMetricHeader(Configuration.Name, Type, Configuration.Help);
            if (!Configuration.SuppressEmptySamples || Unlabelled.HasObservations)
            {
                Unlabelled.Collect(writer);
            }

            foreach (var labelledMetric in LabelledMetrics)
            {
                if (!Configuration.SuppressEmptySamples || labelledMetric.Value.HasObservations)
                {
                    labelledMetric.Value.Collect(writer);
                }
            }

            writer.EndMetric();
        }
        void ICollector.Collect(IMetricsWriter writer)
        {
            writer.WriteMetricHeader(_configuration.Name, _metricType, _configuration.Help);
            if (!_configuration.SuppressEmptySamples || _unlabelled.HasObservations)
            {
                _unlabelled.Collect(writer);
            }

            if (_labelledMetrics != null)
            {
                foreach (var labelledMetric in _labelledMetrics)
                {
                    labelledMetric.Value.Collect(writer);
                }
            }

            writer.EndMetric();
        }
Exemple #19
0
        void ICollector.Collect(IMetricsWriter writer)
        {
            writer.WriteMetricHeader(_configuration.Name, _metricType, _configuration.Help);
            if (_unlabelled.IsValueCreated)
            {
                _unlabelled.Value.Collect(writer);
            }

            if (_labelledMetrics != null)
            {
                foreach (var labelledMetric in _labelledMetrics)
                {
                    labelledMetric.Value.Collect(writer);
                }
            }

            writer.EndMetric();
        }
Exemple #20
0
            protected internal override void Collect(IMetricsWriter writer)
            {
                var state = ForkState(DateTime.UtcNow);

                for (int i = 0; i < state.Values.Length; i++)
                {
                    var bucketSample = writer.StartSample();
                    var labelWriter  = bucketSample.StartLabels();
                    labelWriter.WriteLabels(Labels);
                    labelWriter.WriteLabel("quantile", state.Values[i].Key.ToString(CultureInfo.InvariantCulture));
                    labelWriter.EndLabels();

                    bucketSample.WriteValue(state.Values[i].Value);
                    if (Timestamp.HasValue)
                    {
                        bucketSample.WriteTimestamp(Timestamp.Value);
                    }
                }

                writer.WriteSample(state.Sum, "_sum", Labels, Timestamp);
                writer.WriteSample(state.Count, "_count", Labels, Timestamp);
            }
        public static IMetricsWriter WriteSample(
            this IMetricsWriter writer,
            double value,
            string suffix = "",
            KeyValuePair <string, string>[] labels = null,
            long?timestamp = null)
        {
            var sampleWriter = writer.StartSample(suffix);

            if ((labels != null) && (labels.Length > 0))
            {
                var labelWriter = sampleWriter.StartLabels();
                labelWriter.WriteLabels(labels);
                labelWriter.EndLabels();
            }

            sampleWriter.WriteValue(value);
            if (timestamp.HasValue)
            {
                sampleWriter.WriteTimestamp(timestamp.Value);
            }

            return(writer);
        }
 protected internal override void Collect(IMetricsWriter writer)
 {
 }
 protected internal abstract void Collect(IMetricsWriter writer);
Exemple #24
0
 protected internal override void Collect(IMetricsWriter writer)
 {
     writer.WriteSample(Value, string.Empty, Labels, Timestamp);
 }
 public void Collect(IMetricsWriter writer)
 {
     writer.WriteMetricHeader(_name, MetricType.Gauge, _help);
     writer.WriteSample(GC.GetTotalMemory(false));
     writer.EndMetric();
 }
Exemple #26
0
 protected internal override void Collect(IMetricsWriter writer)
 {
     writer.WriteSample(Value, string.Empty, Configuration.LabelNames, LabelValues, Timestamp);
 }
 public MetricWriterWrapper(IMetricsWriter baseWriter)
 {
     _baseWriter = baseWriter;
 }
Exemple #28
0
 public void Collect(IMetricsWriter writer)
 {
     throw new System.NotImplementedException();
 }
Exemple #29
0
 public void Collect(IMetricsWriter writer)
 {
 }