Beispiel #1
0
        public async Task BatchQuery()
        {
            #region Snippet:BatchQuery

            LogsClient    client      = new LogsClient(new DefaultAzureCredential());
            /*@@*/ string workspaceId = TestEnvironment.WorkspaceId;
            //@@string workspaceId = "<workspace_id>";

            // Query TOP 10 resource groups by event count
            // And total event count
            LogsBatchQuery batch        = client.CreateBatchQuery();
            string         countQueryId = batch.AddQuery(workspaceId, "AzureActivity | count");
            string         topQueryId   = batch.AddQuery(workspaceId, "AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count");

            Response <LogsBatchQueryResult> response = await batch.SubmitAsync();

            var count      = response.Value.GetResult <int>(countQueryId).Single();
            var topEntries = response.Value.GetResult <MyLogEntryModel>(topQueryId);

            Console.WriteLine($"AzureActivity has total {count} events");
            foreach (var logEntryModel in topEntries)
            {
                Console.WriteLine($"{logEntryModel.ResourceGroup} had {logEntryModel.Count} events");
            }

            #endregion
        }
        public async Task ThrowsExceptionWhenQueryFailsBatch()
        {
            var client = CreateClient();

            LogsBatchQuery batch       = InstrumentClient(client.CreateBatchQuery());
            var            queryId     = batch.AddQuery(TestEnvironment.WorkspaceId, "this won't work", _logsTestData.DataTimeRange);
            var            batchResult = await batch.SubmitAsync();

            var exception = Assert.Throws <RequestFailedException>(() => batchResult.Value.GetResult(queryId));

            Assert.AreEqual("BadArgumentError", exception.ErrorCode);
            StringAssert.StartsWith("The request had some invalid properties", exception.Message);
        }
        public async Task CanQueryBatch()
        {
            var            client = CreateClient();
            LogsBatchQuery batch  = InstrumentClient(client.CreateBatchQuery());
            string         id1    = batch.AddQuery(TestEnvironment.WorkspaceId, "Heartbeat");
            string         id2    = batch.AddQuery(TestEnvironment.WorkspaceId, "Heartbeat");
            Response <LogsBatchQueryResult> response = await batch.SubmitAsync();

            var result1 = response.Value.GetResult(id1);
            var result2 = response.Value.GetResult(id2);

            CollectionAssert.IsNotEmpty(result1.Tables[0].Columns);
            CollectionAssert.IsNotEmpty(result2.Tables[0].Columns);
        }
        public async Task ThrowsExceptionWhenBatchQueryNotFound()
        {
            var client = CreateClient();

            LogsBatchQuery batch = InstrumentClient(client.CreateBatchQuery());

            batch.AddQuery(TestEnvironment.WorkspaceId, _logsTestData.TableAName, _logsTestData.DataTimeRange);
            var batchResult = await batch.SubmitAsync();

            var exception = Assert.Throws <ArgumentException>(() => batchResult.Value.GetResult("12345"));

            Assert.AreEqual("queryId", exception.ParamName);
            StringAssert.StartsWith("Query with ID '12345' wasn't part of the batch. Please use the return value of the LogsBatchQuery.AddQuery as the 'queryId' argument.", exception.Message);
        }
        public async Task CanQueryWithStatisticsBatch(bool include)
        {
            var client = CreateClient();

            LogsBatchQuery batch   = InstrumentClient(client.CreateBatchQuery());
            var            queryId = batch.AddQuery(TestEnvironment.WorkspaceId, _logsTestData.TableAName, _logsTestData.DataTimeRange, options: new LogsQueryOptions()
            {
                IncludeStatistics = include
            });
            var batchResult = await batch.SubmitAsync();

            var result = batchResult.Value.GetResult(queryId);

            if (include)
            {
                Assert.Greater(result.Statistics.GetProperty("query").GetProperty("executionTime").GetDouble(), 0);
            }
            else
            {
                Assert.AreEqual(JsonValueKind.Undefined, result.Statistics.ValueKind);
            }
        }
        public async Task CanQueryBatchWithTimespan()
        {
            // Get the time of the second event and add a bit of buffer to it (events are 2d apart)
            var minOffset = (DateTimeOffset)_logsTestData.TableA[1][LogsTestData.TimeGeneratedColumnNameSent];
            var timespan  = Recording.UtcNow - minOffset;

            timespan = timespan.Add(TimeSpan.FromDays(1));

            var            client = CreateClient();
            LogsBatchQuery batch  = InstrumentClient(client.CreateBatchQuery());
            string         id1    = batch.AddQuery(TestEnvironment.WorkspaceId, $"{_logsTestData.TableAName} | project {LogsTestData.TimeGeneratedColumnName}", _logsTestData.DataTimeRange);
            string         id2    = batch.AddQuery(TestEnvironment.WorkspaceId, $"{_logsTestData.TableAName} | project {LogsTestData.TimeGeneratedColumnName}", timespan);
            Response <LogsBatchQueryResult> response = await batch.SubmitAsync();

            var result1 = response.Value.GetResult <DateTimeOffset>(id1);
            var result2 = response.Value.GetResult <DateTimeOffset>(id2);

            // All rows
            Assert.AreEqual(3, result1.Count);
            // Filtered by the timestamp
            Assert.AreEqual(2, result2.Count);
            Assert.True(result2.All(r => r >= minOffset));
        }