/// <summary>
        /// Serialize to Prometheus Format.
        /// </summary>
        /// <param name="exporter">Prometheus Exporter.</param>
        /// <param name="writer">StreamWriter to write to.</param>
        public static void WriteMetricsCollection(this PrometheusExporter exporter, StreamWriter writer)
        {
            foreach (var metricItem in exporter.Batch)
            {
                foreach (var metric in metricItem.Metrics)
                {
                    var builder = new PrometheusMetricBuilder()
                                  .WithName(metric.Name)
                                  .WithDescription(metric.Description);

                    // TODO: Use switch case for higher perf.
                    if (metric.MetricType == MetricType.LongSum)
                    {
                        WriteSum(writer, builder, metric.Attributes, (metric as ISumMetricLong).LongSum);
                    }
                    else if (metric.MetricType == MetricType.DoubleSum)
                    {
                        WriteSum(writer, builder, metric.Attributes, (metric as ISumMetricDouble).DoubleSum);
                    }
                    else if (metric.MetricType == MetricType.DoubleGauge)
                    {
                        var gaugeMetric = metric as IGaugeMetric;
                        var doubleValue = (double)gaugeMetric.LastValue.Value;
                        WriteGauge(writer, builder, metric.Attributes, doubleValue);
                    }
                    else if (metric.MetricType == MetricType.LongGauge)
                    {
                        var gaugeMetric = metric as IGaugeMetric;
                        var longValue   = (long)gaugeMetric.LastValue.Value;

                        // TODO: Prometheus only supports float/double
                        WriteGauge(writer, builder, metric.Attributes, longValue);
                    }
                    else if (metric.MetricType == MetricType.Histogram)
                    {
                        var histogramMetric = metric as IHistogramMetric;
                        WriteHistogram(writer, builder, metric.Attributes, metric.Name, histogramMetric.PopulationSum, histogramMetric.PopulationCount, histogramMetric.Buckets);
                    }
                }
            }
        }
예제 #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PrometheusExporterMetricsHttpServer"/> class.
 /// </summary>
 /// <param name="exporter">The <see cref="PrometheusExporter"/> instance.</param>
 public PrometheusExporterMetricsHttpServer(PrometheusExporter exporter)
 {
     this.exporter = exporter ?? throw new ArgumentNullException(nameof(exporter));
     this.httpListener.Prefixes.Add(exporter.Options.Url);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="PrometheusExporterMiddleware"/> class.
 /// </summary>
 /// <param name="next">The next middleware in the pipeline.</param>
 /// <param name="exporter">The <see cref="PrometheusExporter"/> instance.</param>
 public PrometheusExporterMiddleware(RequestDelegate next, PrometheusExporter exporter)
 {
     this.exporter = exporter ?? throw new ArgumentNullException(nameof(exporter));
 }