예제 #1
0
        public async Task MultipleExportersTestAsync()
        {
            var opts  = new PrometheusEndpointOptions();
            var mopts = new ActuatorManagementOptions();

            mopts.EndpointOptions.Add(opts);
            var exporter1     = new PrometheusExporter();
            var exporter2     = new SteeltoeExporter();
            var multiExporter = new MultiExporter(new MetricExporter[] { exporter1, exporter2 }.ToList());
            var processor     = new SteeltoeProcessor(multiExporter);

            var factory = AutoCollectingMeterFactory.Create(processor);
            var meter   = factory.GetMeter("Test");

            SetupTestView(meter);
            factory.CollectAllMetrics();
            processor.ExportMetrics();

            Task.Delay(1000).Wait();

            var ep     = new PrometheusScraperEndpoint(opts, exporter1);
            var middle = new PrometheusScraperEndpointMiddleware(null, ep, mopts);

            var context = CreateRequest("GET", "/actuator/prometheus");

            await middle.HandleMetricsRequestAsync(context);

            context.Response.Body.Seek(0, SeekOrigin.Begin);
            var rdr  = new StreamReader(context.Response.Body);
            var text = await rdr.ReadToEndAsync();

            Assert.Equal("# HELP test Testtest\n# TYPE test counter\ntest{a=\"v1\",b=\"v1\",c=\"v1\"} 45\n", text);

            var meopts  = new MetricsEndpointOptions();
            var ep2     = new MetricsEndpoint(meopts, exporter2);
            var middle2 = new MetricsEndpointMiddleware(null, ep2, mopts);

            var context2 = CreateRequest("GET", "/actuator/metrics/test", "?tag=a:v1");

            await middle2.HandleMetricsRequestAsync(context2);

            Assert.Equal(200, context.Response.StatusCode);

            context2.Response.Body.Seek(0, SeekOrigin.Begin);
            StreamReader rdr2 = new StreamReader(context2.Response.Body);
            string       json = await rdr2.ReadToEndAsync();

            Assert.Equal("{\"name\":\"test\",\"measurements\":[{\"statistic\":\"COUNT\",\"value\":45}],\"availableTags\":[{\"tag\":\"a\",\"values\":[\"v1\"]},{\"tag\":\"b\",\"values\":[\"v1\"]},{\"tag\":\"c\",\"values\":[\"v1\"]}]}", json);
        }
 public PrometheusScraperEndpoint(IPrometheusEndpointOptions options, PrometheusExporter exporter)
     : base(options)
 {
     _exporter = exporter ?? throw new ArgumentNullException(nameof(exporter));
 }
예제 #3
0
        /// <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 metric in exporter.GetAndClearDoubleMetrics())
            {
                var labels  = metric.Labels;
                var builder = new PrometheusMetricBuilder()
                              .WithName(metric.MetricName)
                              .WithDescription(metric.MetricDescription);

                switch (metric.AggregationType)
                {
                case AggregationType.DoubleSum:
                {
                    var doubleSum   = metric.Data as SumData <double>;
                    var doubleValue = doubleSum.Sum;

                    builder = builder.WithType("counter");
                    var metricValueBuilder = builder.AddValue();
                    metricValueBuilder = metricValueBuilder.WithValue(doubleValue);

                    foreach (var label in labels)
                    {
                        metricValueBuilder.WithLabel(label.Key, label.Value);
                    }

                    builder.Write(writer);
                    break;
                }

                case AggregationType.Summary:
                {
                    var doubleSummary = metric.Data as SummaryData <double>;

                    builder = builder.WithType("summary");
                    var metricValueBuilder = builder.AddValue();
                    var mean = 0D;

                    if (doubleSummary.Count > 0)
                    {
                        mean = doubleSummary.Sum / doubleSummary.Count;
                    }

                    metricValueBuilder = metricValueBuilder.WithValue(mean);

                    foreach (var label in labels)
                    {
                        metricValueBuilder.WithLabel(label.Key, label.Value);
                    }

                    builder.Write(writer);
                    break;
                }
                }
            }

            foreach (var metric in exporter.GetAndClearLongMetrics())
            {
                var labels  = metric.Labels;
                var builder = new PrometheusMetricBuilder()
                              .WithName(metric.MetricName)
                              .WithDescription(metric.MetricDescription);

                switch (metric.AggregationType)
                {
                case AggregationType.LongSum:
                {
                    var longSum   = metric.Data as SumData <long>;
                    var longValue = longSum.Sum;
                    builder = builder.WithType("counter");

                    foreach (var label in labels)
                    {
                        var metricValueBuilder = builder.AddValue();
                        metricValueBuilder = metricValueBuilder.WithValue(longValue);
                        metricValueBuilder.WithLabel(label.Key, label.Value);
                    }

                    builder.Write(writer);
                    break;
                }

                case AggregationType.Summary:
                {
                    var longSummary = metric.Data as SummaryData <long>;

                    builder = builder.WithType("summary");
                    var metricValueBuilder = builder.AddValue();
                    var mean = 0L;

                    if (longSummary.Count > 0)
                    {
                        mean = longSummary.Sum / longSummary.Count;
                    }

                    metricValueBuilder = metricValueBuilder.WithValue(mean);

                    foreach (var label in labels)
                    {
                        metricValueBuilder.WithLabel(label.Key, label.Value);
                    }

                    builder.Write(writer);
                    break;
                }
                }
            }
        }
예제 #4
0
 public PrometheusScraperEndpoint(IPrometheusOptions options, PrometheusExporter exporter, ILogger <PrometheusScraperEndpoint> logger = null)
     : base(options)
 {
     _exporter = exporter ?? throw new ArgumentNullException(nameof(exporter));
     _logger   = logger;
 }