Exemple #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);
        }
        } // - 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