示例#1
0
        public void PrometheusExporterTest1()
        {
            var promOptions = new PrometheusExporterOptions()
            {
                Url = "http://localhost:9184/metrics/"
            };
            List <Metric <long> > metrics = new List <Metric <long> >();
            var promExporter      = new PrometheusExporter(promOptions);
            var metricsHttpServer = new PrometheusExporterMetricsHttpServer(promExporter);

            try
            {
                metricsHttpServer.Start();
                var label1 = new List <KeyValuePair <string, string> >();
                label1.Add(new KeyValuePair <string, string>("dim1", "value1"));
                metrics.Add(new Metric <long>("ns", "metric1", "desc", label1, AggregationType.LongSum));
                metrics.Add(new Metric <long>("ns", "metric1", "desc", label1, AggregationType.LongSum));
                metrics.Add(new Metric <long>("ns", "metric1", "desc", label1, AggregationType.LongSum));

                promExporter.ExportAsync(metrics, CancellationToken.None);
            }
            finally
            {
                // Change delay to higher value to manually check Promtheus.
                // These tests are just to temporarily validate export to prometheus.
                Task.Delay(10).Wait();
                metricsHttpServer.Stop();
            }
        }
示例#2
0
        public async Task E2ETestMetricsHttpServerAsync()
        {
            var promOptions = new PrometheusExporterOptions()
            {
                Url = "http://localhost:9184/metrics/"
            };
            var promExporter    = new PrometheusExporter(promOptions);
            var simpleProcessor = new UngroupedBatcher();

            var metricsHttpServer = new PrometheusExporterMetricsHttpServer(promExporter);

            try
            {
                metricsHttpServer.Start();
                CollectMetrics(simpleProcessor, promExporter);
            }
            finally
            {
                await Task.Delay(WaitDuration);

                var client   = new HttpClient();
                var response = await client.GetAsync("http://localhost:9184/metrics/");

                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                var responseText = response.Content.ReadAsStringAsync().Result;
                this.output.WriteLine($"Response from metrics API is \n {responseText}");
                this.ValidateResponse(responseText);
                metricsHttpServer.Stop();
            }
        }
示例#3
0
        internal static object Run()
        {
            var promOptions = new PrometheusExporterOptions()
            {
                Url = "http://localhost:9184/metrics/"
            };
            var promExporter    = new PrometheusExporter(promOptions);
            var simpleProcessor = new UngroupedBatcher(promExporter, TimeSpan.FromSeconds(5));
            var meter           = MeterFactory.Create(simpleProcessor).GetMeter("library1");
            var testCounter     = meter.CreateInt64Counter("testCounter");

            var labels1 = new List <KeyValuePair <string, string> >();

            labels1.Add(new KeyValuePair <string, string>("dim1", "value1"));

            var labels2 = new List <KeyValuePair <string, string> >();

            labels2.Add(new KeyValuePair <string, string>("dim1", "value2"));

            var httpServer     = new PrometheusExporterMetricsHttpServer(promExporter);
            var defaultContext = default(SpanContext);

            try
            {
                httpServer.Start();

                for (int i = 0; i < 1000; i++)
                {
                    testCounter.Add(defaultContext, 100, meter.GetLabelSet(labels1));
                    testCounter.Add(defaultContext, 10, meter.GetLabelSet(labels1));
                    testCounter.Add(defaultContext, 200, meter.GetLabelSet(labels2));
                    testCounter.Add(defaultContext, 10, meter.GetLabelSet(labels2));

                    if (i % 10 == 0)
                    {
                        // Collect is called here explicitly as there is
                        // no controller implementation yet.
                        // TODO: There should be no need to cast to MeterSdk.
                        (meter as MeterSdk).Collect();
                    }

                    Task.Delay(1000).Wait();
                }
            }
            finally
            {
                Task.Delay(3000).Wait();
                httpServer.Stop();
            }

            return(null);
        }
示例#4
0
        public void E2ETest1()
        {
            var promOptions = new PrometheusExporterOptions()
            {
                Url = "http://localhost:9184/metrics/"
            };
            var promExporter    = new PrometheusExporter(promOptions);
            var simpleProcessor = new UngroupedBatcher(promExporter);
            var meter           = MeterFactory.Create(simpleProcessor).GetMeter("library1") as MeterSdk;
            var testCounter     = meter.CreateInt64Counter("testCounter");

            var labels1 = new List <KeyValuePair <string, string> >();

            labels1.Add(new KeyValuePair <string, string>("dim1", "value1"));

            var labels2 = new List <KeyValuePair <string, string> >();

            labels2.Add(new KeyValuePair <string, string>("dim1", "value2"));

            var metricsHttpServer = new PrometheusExporterMetricsHttpServer(promExporter);

            try
            {
                metricsHttpServer.Start();

                var defaultContext = default(SpanContext);

                for (int i = 0; i < 1000; i++)
                {
                    testCounter.Add(defaultContext, 100, meter.GetLabelSet(labels1));
                    testCounter.Add(defaultContext, 10, meter.GetLabelSet(labels1));
                    testCounter.Add(defaultContext, 200, meter.GetLabelSet(labels2));
                    testCounter.Add(defaultContext, 10, meter.GetLabelSet(labels2));

                    if (i % 10 == 0)
                    {
                        meter.Collect();
                    }

                    // Change delay to higher value to manually check Promtheus.
                    // These tests are just to temporarily validate export to prometheus.
                    // Task.Delay(100).Wait();
                }
            }
            finally
            {
                Task.Delay(100).Wait();
                metricsHttpServer.Stop();
            }
        }
        public static MeterProviderBuilder AddPrometheusExporter(this MeterProviderBuilder builder, Action <PrometheusExporterOptions> configure = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var options = new PrometheusExporterOptions();

            configure?.Invoke(options);
            var exporter            = new PrometheusExporter(options);
            var pullMetricProcessor = new PullMetricProcessor(exporter, false);

            exporter.MakePullRequest = pullMetricProcessor.PullRequest;

            var metricsHttpServer = new PrometheusExporterMetricsHttpServer(exporter);

            metricsHttpServer.Start();
            return(builder.AddMetricProcessor(pullMetricProcessor));
        }
示例#6
0
        public void E2ETestMetricsHttpServer()
        {
            var promOptions = new PrometheusExporterOptions()
            {
                Url = "http://localhost:9184/metrics/"
            };
            var promExporter    = new PrometheusExporter(promOptions);
            var simpleProcessor = new UngroupedBatcher(promExporter);

            var metricsHttpServer = new PrometheusExporterMetricsHttpServer(promExporter);

            try
            {
                metricsHttpServer.Start();
                CollectMetrics(simpleProcessor);
            }
            finally
            {
                // Change delay to higher value to manually check Promtheus.
                // These tests are just to temporarily validate export to prometheus.
                Task.Delay(100).Wait();
                metricsHttpServer.Stop();
            }
        }
示例#7
0
        internal static async Task <object> RunAsync(int port, int pushIntervalInSecs, int totalDurationInMins)
        {
            System.Console.WriteLine($"OpenTelemetry Prometheus Exporter is making metrics available at http://localhost:{port}/metrics/");

            /*
             * Following is sample prometheus.yml config. Adjust port,interval as needed.
             *
             * scrape_configs:
             # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
             # - job_name: 'OpenTelemetryTest'
             #
             # metrics_path defaults to '/metrics'
             # scheme defaults to 'http'.
             #
             #  static_configs:
             #  - targets: ['localhost:9184']
             */

            // Create and Setup Prometheus Exporter
            var promOptions = new PrometheusExporterOptions()
            {
                Url = $"http://localhost:{port}/metrics/"
            };
            var promExporter      = new PrometheusExporter(promOptions);
            var metricsHttpServer = new PrometheusExporterMetricsHttpServer(promExporter);

            metricsHttpServer.Start();

            // Create Processor (called Batcher in Metric spec, this is still not decided)
            var processor = new UngroupedBatcher();

            // Application which decides to enable OpenTelemetry metrics
            // would setup a MeterProvider and make it default.
            // All meters from this factory will be configured with the common processing pipeline.
            MeterProvider.SetDefault(Sdk.CreateMeterProviderBuilder()
                                     .SetProcessor(processor)
                                     .SetExporter(promExporter)
                                     .SetPushInterval(TimeSpan.FromSeconds(pushIntervalInSecs))
                                     .Build());

            // The following shows how libraries would obtain a MeterProvider.
            // MeterProvider is the entry point, which provides Meter.
            // If user did not set the Default MeterProvider (shown in earlier lines),
            // all metric operations become no-ops.
            var meterProvider = MeterProvider.Default;
            var meter         = meterProvider.GetMeter("MyMeter");

            // the rest is purely from Metrics API.
            var testCounter  = meter.CreateInt64Counter("MyCounter");
            var testMeasure  = meter.CreateInt64Measure("MyMeasure");
            var testObserver = meter.CreateInt64Observer("MyObservation", CallBackForMyObservation);
            var labels1      = new List <KeyValuePair <string, string> >();

            labels1.Add(new KeyValuePair <string, string>("dim1", "value1"));

            var labels2 = new List <KeyValuePair <string, string> >();

            labels2.Add(new KeyValuePair <string, string>("dim1", "value2"));
            var defaultContext = default(SpanContext);

            Stopwatch sw = Stopwatch.StartNew();

            while (sw.Elapsed.TotalMinutes < totalDurationInMins)
            {
                testCounter.Add(defaultContext, 100, meter.GetLabelSet(labels1));

                testMeasure.Record(defaultContext, 100, meter.GetLabelSet(labels1));
                testMeasure.Record(defaultContext, 500, meter.GetLabelSet(labels1));
                testMeasure.Record(defaultContext, 5, meter.GetLabelSet(labels1));
                testMeasure.Record(defaultContext, 750, meter.GetLabelSet(labels1));

                // Obviously there is no testObserver.Oberve() here, as Observer instruments
                // have callbacks that are called by the Meter automatically at each collection interval.

                await Task.Delay(1000);

                var remaining = (totalDurationInMins * 60) - sw.Elapsed.TotalSeconds;
                System.Console.WriteLine("Running and emitting metrics. Remaining time:" + (int)remaining + " seconds");
            }

            // Stopping
            metricsHttpServer.Stop();
            System.Console.WriteLine("Metrics server shutdown.");
            System.Console.WriteLine("Press Enter key to exit.");
            return(null);
        }