public void EmptyCounters()
        {
            var counters = new List <Counter>();
            var body     = PrometheusCounterConverter.ToString(counters, string.Empty, string.Empty);

            Assert.Equal(string.Empty, body);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Saves the current counters measurements.
        /// </summary>
        /// <param name="counters">current counters measurements to be saves.</param>
        protected override void Save(IEnumerable <Counter> counters)
        {
            if (_client == null || !_pushEnabled)
            {
                return;
            }

            try
            {
                var body = PrometheusCounterConverter.ToString(counters, null, null);

                using (HttpContent requestContent = new StringContent(body, Encoding.UTF8, "text/plain"))
                {
                    HttpResponseMessage response = _client.PutAsync(_requestUri, requestContent).Result;
                    if ((int)response.StatusCode >= 400)
                    {
                        _logger.Error("prometheus-counters", "Failed to push metrics to prometheus");
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error("prometheus-counters", ex, "Failed to push metrics to prometheus");
            }
        }
        public void SingleIncrement_SourceInstance()
        {
            var counters = new List <Counter>
            {
                new Counter("MyCounter", CounterType.Increment)
                {
                    Average = 2,
                    Min     = 1,
                    Max     = 3,
                    Count   = 2,
                    Last    = 3,
                    Time    = DateTime.MinValue
                }
            };
            var          body     = PrometheusCounterConverter.ToString(counters, "MyApp", "MyInstance");
            const string expected = "# TYPE mycounter gauge\nmycounter{source=\"MyApp\",instance=\"MyInstance\"} 2\n";

            Assert.Equal(expected, body);
        }
        public void SingleIncrement_NoLabels()
        {
            var counters = new List <Counter>
            {
                new Counter("MyCounter", CounterType.Increment)
                {
                    Average = 2,
                    Min     = 1,
                    Max     = 3,
                    Count   = 2,
                    Last    = 3,
                    Time    = DateTime.MinValue
                }
            };
            var          body     = PrometheusCounterConverter.ToString(counters, null, null);
            const string expected = "# TYPE mycounter gauge\nmycounter 2\n";

            Assert.Equal(expected, body);
        }
        public void MultiIncrement_ExecWithOnlyTwo_SourceInstance()
        {
            var counters = new List <Counter>
            {
                new Counter("MyCounter1.exec_time", CounterType.Increment)
                {
                    Count = 2,
                    Last  = 3,
                    Time  = DateTime.MinValue
                },
                new Counter("MyCounter2.exec_time", CounterType.Increment)
                {
                    Count = 5,
                    Last  = 10,
                    Time  = DateTime.MinValue
                }
            };
            var          body     = PrometheusCounterConverter.ToString(counters, "MyApp", "MyInstance");
            const string expected = "# TYPE mycounter1_exec_time gauge\nmycounter1_exec_time{source=\"MyApp\",instance=\"MyInstance\"} 2\n"
                                    + "# TYPE mycounter2_exec_time gauge\nmycounter2_exec_time{source=\"MyApp\",instance=\"MyInstance\"} 5\n";

            Assert.Equal(expected, body);
        }
        public void MultiLastValue_Exec_SourceInstance()
        {
            var counters = new List <Counter>
            {
                new Counter("MyService1.MyCommand1.exec_time", CounterType.LastValue)
                {
                    Count = 2,
                    Last  = 3,
                    Time  = DateTime.MinValue
                },
                new Counter("MyService2.MyCommand2.exec_time", CounterType.LastValue)
                {
                    Count = 5,
                    Last  = 10,
                    Time  = DateTime.MinValue
                }
            };
            var          body     = PrometheusCounterConverter.ToString(counters, "MyApp", "MyInstance");
            const string expected = "# TYPE exec_time gauge\nexec_time{source=\"MyApp\",instance=\"MyInstance\",service=\"MyService1\",command=\"MyCommand1\"} 3\n"
                                    + "# TYPE exec_time gauge\nexec_time{source=\"MyApp\",instance=\"MyInstance\",service=\"MyService2\",command=\"MyCommand2\"} 10\n";

            Assert.Equal(expected, body);
        }
        public void MultiStatistics_Exec_SourceInstance()
        {
            var counters = new List <Counter>
            {
                new Counter("MyService1.MyCommand1.exec_time", CounterType.Statistics)
                {
                    Min     = 1,
                    Max     = 3,
                    Average = 2,
                    Count   = 2,
                    Last    = 3,
                    Time    = DateTime.MinValue
                },
                new Counter("MyService2.MyCommand2.exec_time", CounterType.Statistics)
                {
                    Min     = 2,
                    Max     = 4,
                    Average = 3,
                    Count   = 5,
                    Last    = 10,
                    Time    = DateTime.MinValue
                }
            };
            var body = PrometheusCounterConverter.ToString(counters, "MyApp", "MyInstance");

            const string expected =
                "# TYPE exec_time_max gauge\nexec_time_max{source=\"MyApp\",instance=\"MyInstance\",service=\"MyService1\",command=\"MyCommand1\"} 3\n"
                + "# TYPE exec_time_min gauge\nexec_time_min{source=\"MyApp\",instance=\"MyInstance\",service=\"MyService1\",command=\"MyCommand1\"} 1\n"
                + "# TYPE exec_time_average gauge\nexec_time_average{source=\"MyApp\",instance=\"MyInstance\",service=\"MyService1\",command=\"MyCommand1\"} 2\n"
                + "# TYPE exec_time_count gauge\nexec_time_count{source=\"MyApp\",instance=\"MyInstance\",service=\"MyService1\",command=\"MyCommand1\"} 2\n"
                + "# TYPE exec_time_max gauge\nexec_time_max{source=\"MyApp\",instance=\"MyInstance\",service=\"MyService2\",command=\"MyCommand2\"} 4\n"
                + "# TYPE exec_time_min gauge\nexec_time_min{source=\"MyApp\",instance=\"MyInstance\",service=\"MyService2\",command=\"MyCommand2\"} 2\n"
                + "# TYPE exec_time_average gauge\nexec_time_average{source=\"MyApp\",instance=\"MyInstance\",service=\"MyService2\",command=\"MyCommand2\"} 3\n"
                + "# TYPE exec_time_count gauge\nexec_time_count{source=\"MyApp\",instance=\"MyInstance\",service=\"MyService2\",command=\"MyCommand2\"} 5\n";

            Assert.Equal(expected, body);
        }
Exemplo n.º 8
0
        public void KnownCounter_Exec_QueueMetrics_Good(string counterName, string expectedName)
        {
            var counters = new List <Counter>
            {
                new Counter(counterName, CounterType.Increment)
                {
                    Count = 1, Time = DateTime.MinValue
                },
                new Counter(counterName, CounterType.Interval)
                {
                    Count = 11, Max = 13, Min = 3, Average = 3.5, Time = DateTime.MinValue
                },
                new Counter(counterName, CounterType.LastValue)
                {
                    Last = 2, Time = DateTime.MinValue
                },
                new Counter(counterName, CounterType.Statistics)
                {
                    Count = 111, Max = 113, Min = 13, Average = 13.5, Time = DateTime.MinValue
                }
            };

            var body = PrometheusCounterConverter.ToString(counters, "MyApp", "MyInstance");

            var expected = $"# TYPE {expectedName} gauge\n{expectedName}{{source=\"MyApp\",instance=\"MyInstance\",queue=\"default\"}} 1\n"
                           + $"# TYPE {expectedName}_max gauge\n{expectedName}_max{{source=\"MyApp\",instance=\"MyInstance\",queue=\"default\"}} 13\n"
                           + $"# TYPE {expectedName}_min gauge\n{expectedName}_min{{source=\"MyApp\",instance=\"MyInstance\",queue=\"default\"}} 3\n"
                           + $"# TYPE {expectedName}_average gauge\n{expectedName}_average{{source=\"MyApp\",instance=\"MyInstance\",queue=\"default\"}} 3.5\n"
                           + $"# TYPE {expectedName}_count gauge\n{expectedName}_count{{source=\"MyApp\",instance=\"MyInstance\",queue=\"default\"}} 11\n"
                           + $"# TYPE {expectedName} gauge\n{expectedName}{{source=\"MyApp\",instance=\"MyInstance\",queue=\"default\"}} 2\n"
                           + $"# TYPE {expectedName}_max gauge\n{expectedName}_max{{source=\"MyApp\",instance=\"MyInstance\",queue=\"default\"}} 113\n"
                           + $"# TYPE {expectedName}_min gauge\n{expectedName}_min{{source=\"MyApp\",instance=\"MyInstance\",queue=\"default\"}} 13\n"
                           + $"# TYPE {expectedName}_average gauge\n{expectedName}_average{{source=\"MyApp\",instance=\"MyInstance\",queue=\"default\"}} 13.5\n"
                           + $"# TYPE {expectedName}_count gauge\n{expectedName}_count{{source=\"MyApp\",instance=\"MyInstance\",queue=\"default\"}} 111\n";

            Assert.Equal(expected, body);
        }
        public void NullValues()
        {
            var body = PrometheusCounterConverter.ToString(null, null, null);

            Assert.Equal(string.Empty, body);
        }