Beispiel #1
0
        public void ToKustoExpression_PrometheusQueryShouldReturnValidKustoQuery(string name,
                                                                                 LabelMatcher.Types.Type type, string val, string expected)
        {
            // Arrange
            // Act
            var kql = KustoManipulations.ToKustoExpression(name, type, val);

            // Assert
            CollectionAssert.AreEqual(expected, kql);
        }
Beispiel #2
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"
                    );
            }
        }
        } // - InitializeKustoClient


        private static ReadResponse CreateResponse(ReadRequest readrequest, ILogger log)
        {
            var result = new ReadResponse();
            var taskList = new List<Task<IDataReader>>();

            const string metricQueryTemplate = @"
                Metrics
                | where (EndDatetime >= unixtime_milliseconds_todatetime({0})) and (StartDatetime <= unixtime_milliseconds_todatetime({1})) and ( {2} )
                | summarize Labels=tostring(any(Labels)), Samples=make_list( Samples ) by LabelsHash
                | mv-apply Samples = Samples on
                (
                    order by tolong(Samples['Timestamp']) asc
                    | summarize Samples=make_list(pack('Timestamp', Samples['Timestamp'], 'Value', Samples['Value']))
                )
            ";

            var timer = new Stopwatch();
            timer.Start();

            foreach (var aQuery in readrequest.Queries)
            {
                var kql = string.Format(
                    metricQueryTemplate,
                    aQuery.StartTimestampMs,
                    aQuery.EndTimestampMs,
                    string.Join(
                        " and ",
                        aQuery.Matchers.Select(
                            item => KustoManipulations.ToKustoExpression(item.Name, item.Type, item.Value)
                        )
                    )
                );

                log.LogInformation($"KQL: {kql}");

                taskList.Add(
                    _adx.ExecuteQueryAsync(
                        Environment.GetEnvironmentVariable("kustoDatabase"),
                        kql,
                        null
                    )
                );
            } // - foreach readrequest.Queries

            log.LogInformation("[PrometheusRead] [ExecuteQueries] Queries count: " + taskList.Count());
            Task.WaitAll(taskList.ToArray());

            timer.Stop();
            log.LogInformation("[PrometheusRead] [ExecuteQueries] Execution time: " + timer.Elapsed);

            log.LogInformation("[PrometheusRead] [CreateQueryResult] Start serializing results");
            timer.Reset();

            result.Results.AddRange(taskList.Select(aTask => CreateQueryResult(aTask.Result, log)));

            timer.Stop();
            log.LogInformation(
                "[PrometheusRead] [CreateQueryResult] Serializing done. Execution time: " + timer.Elapsed);

            return result;
        } // - ReadResponse