Exemplo n.º 1
0
        /// <summary>
        /// Creates a new histogram metric with tags.
        /// </summary>
        /// <param name="metricsCollector">The metrics collector being extended.</param>
        /// <param name="name">The name of the metric.</param>
        /// <param name="description">The description of the metric.</param>
        /// <param name="buckets">The upper bounds of the buckets to aggregate into in ascending order, a bucket with value <see cref="double.PositiveInfinity"/> will be automattically added if omitted.</param>
        /// <param name="tags">The tags to attach to the metric in the form "key:value".</param>
        /// <returns>The created histogram.</returns>
        public static TaggedMetricBuilder <IHistogramMetric> Histogram(this MetricsCollector metricsCollector, string name, string description, double[] buckets, params string[] tags)
        {
            if (!metricsCollector.IsWritingToPrometheus())
            {
                throw new InvalidOperationException("Prometheus metrics extensions cannot be used without a non-prometheus metric writer. Consider using core metric types, exensions for your chosen writer or switch to the Promethus metrics writer.");
            }

            ValidateTags(tags);

            // Ensure buckets always get larger
            double lastBucket = double.NegativeInfinity;

            foreach (double bucket in buckets)
            {
                if (lastBucket >= bucket)
                {
                    throw new ArgumentException("Buckets must be in ascending order and not start at double.NegativeInfinity.");
                }
                lastBucket = bucket;
            }

            // Ensure the last bucket upper bound is always infinity
            if (buckets.Length == 0 || buckets[buckets.Length - 1] != double.PositiveInfinity)
            {
                Array.Resize(ref buckets, buckets.Length + 1);
                buckets[buckets.Length - 1] = double.PositiveInfinity;
            }

            return(((PrometheusEndpoint)metricsCollector.Writer).CreateHistogram(metricsCollector, name, description, tags));
        }