Example #1
0
        public void ToKustoRow_PrometheusTimeSeriesObjectShouldReturnValidKustoRowObject()
        {
            // Arrange
            var ts = new Prometheus.TimeSeries()
            {
                Labels =
                {
                    new Prometheus.Label()
                    {
                        Name  = "__name__",
                        Value = "TestName"
                    },
                    new Prometheus.Label()
                    {
                        Name  = "job",
                        Value = "TestJob"
                    }
                },
                Samples =
                {
                    new Prometheus.Sample()
                    {
                        Timestamp = 123,
                        Value     = 0
                    }
                }
            };
            var expectedKustoRow = new KustoRow()
            {
                Instance  = null,
                Job       = "TestJob",
                Labels    = @"{""__name__"":""TestName"",""job"":""TestJob""}",
                Name      = "TestName",
                Timestamp = 123,
                Value     = 0,
            };
            // Act
            var resultKustoRow = KustoManipulations.ToKustoRow(ts);

            // Assert
            foreach (PropertyInfo property in typeof(KustoRow).GetProperties())
            {
                Assert.AreEqual(
                    property.GetValue(expectedKustoRow, null),
                    property.GetValue(resultKustoRow, null),
                    $"KustoRow property '{property.Name}' are not equal"
                    );
            }
        }
        } // - ReadResponse

        private static QueryResult CreateQueryResult(IDataReader reader, ILogger log)
        {
            var result = new QueryResult();

            var jsonSerializerSettings = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore,
                MissingMemberHandling = MissingMemberHandling.Ignore
            };

            while (reader.Read())
            {
                var tsItem = new Prometheus.TimeSeries();
                var labels = (string)reader["Labels"];
                var samples = (JArray)reader["Samples"];

                var samplesRange =
                    JsonConvert.DeserializeObject<IEnumerable<Prometheus.Sample>>(samples.ToString(),
                        jsonSerializerSettings);
                tsItem.Samples.AddRange(samplesRange);

                var labelsKv = JsonConvert.DeserializeObject<Dictionary<string, string>>(labels);

                foreach (var label in labelsKv.Select(
                    item => new Prometheus.Label { Name = item.Key, Value = item.Value }))
                {
                    tsItem.Labels.Add(label);
                }

                result.Timeseries.Add(tsItem);
            }

            reader.Close();

            return result;
        } // - QueryResult