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

#if SNIPPET
            Uri    endpoint    = new Uri("https://api.loganalytics.io");
            string workspaceId = "<workspace_id>";
#else
            Uri    endpoint    = TestEnvironment.LogsEndpoint;
            string workspaceId = TestEnvironment.WorkspaceId;
#endif

            LogsClient client = new LogsClient(endpoint, new DefaultAzureCredential());

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

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

            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
        }
示例#2
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
        }
示例#3
0
        public async Task CanQueryBatch()
        {
            var            client = CreateClient();
            LogsBatchQuery batch  = new LogsBatchQuery();
            string         id1    = batch.AddQuery(TestEnvironment.WorkspaceId, "Heartbeat", _logsTestData.DataTimeRange);
            string         id2    = batch.AddQuery(TestEnvironment.WorkspaceId, "Heartbeat", _logsTestData.DataTimeRange);
            Response <LogsBatchQueryResult> response = await client.QueryBatchAsync(batch);

            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 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 void CanSetServiceTimeoutForBatch_Mocked()
        {
            string   preferHeader    = null;
            TimeSpan?networkOverride = default;

            var mockTransport = MockTransport.FromMessageCallback(message =>
            {
                Assert.True(message.Request.Headers.TryGetValue("prefer", out preferHeader));
                networkOverride = message.NetworkTimeout;

                return(new MockResponse(403));
            });

            var client = new LogsQueryClient(new Uri("https://api.loganalytics.io"), new MockCredential(), new LogsQueryClientOptions()
            {
                Transport = mockTransport
            });

            var batch = new LogsBatchQuery();

            batch.AddQuery("wid", "tid", TimeSpan.FromDays(1), options: new LogsQueryOptions()
            {
                ServerTimeout = TimeSpan.FromMinutes(1)
            });
            batch.AddQuery("wid", "tid", TimeSpan.FromDays(1), options: new LogsQueryOptions()
            {
                ServerTimeout = TimeSpan.FromMinutes(2)
            });
            batch.AddQuery("wid", "tid", TimeSpan.FromDays(1), options: new LogsQueryOptions()
            {
                ServerTimeout = TimeSpan.FromMinutes(3)
            });

            Assert.ThrowsAsync <RequestFailedException>(() => client.QueryBatchAsync(batch));

            // 3 minutes (180 sec) is the max out of all individual queries
            Assert.AreEqual("wait=180", preferHeader);
            // The network timeout is adjusted with 15 sec buffer
            Assert.AreEqual(TimeSpan.FromMinutes(3).Add(TimeSpan.FromSeconds(15)), networkOverride);
        }
示例#6
0
        public async Task BatchQuery()
        {
            #region Snippet:BatchQuery

#if SNIPPET
            string workspaceId = "<workspace_id>";
#else
            string workspaceId = TestEnvironment.WorkspaceId;
#endif

            var client = new LogsQueryClient(new DefaultAzureCredential());

            // Query TOP 10 resource groups by event count
            // And total event count
            var batch = new LogsBatchQuery();

            #region Snippet:BatchQueryAddAndGet
            string countQueryId = batch.AddQuery(
                workspaceId,
                "AzureActivity | count",
                new QueryTimeRange(TimeSpan.FromDays(1)));
            string topQueryId = batch.AddQuery(
                workspaceId,
                "AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count",
                new QueryTimeRange(TimeSpan.FromDays(1)));

            Response <LogsBatchQueryResultCollection> response = await client.QueryBatchAsync(batch);

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

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

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

            LogsBatchQuery batch = new LogsBatchQuery();
            var queryId = batch.AddQuery(TestEnvironment.WorkspaceId, "set truncationmaxrecords=1; datatable (s: string) ['a', 'b']", _logsTestData.DataTimeRange);
            var batchResult = await client.QueryBatchAsync(batch);

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

            Assert.AreEqual("PartialError", exception.ErrorCode);
            StringAssert.StartsWith("The result was returned but contained a partial error", exception.Message);
        }
        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 = new LogsBatchQuery();
            string id1 = batch.AddQuery(TestEnvironment.WorkspaceId, $"{_logsTestData.TableAName} | distinct * | project {LogsTestData.TimeGeneratedColumnName}", _logsTestData.DataTimeRange);
            string id2 = batch.AddQuery(TestEnvironment.WorkspaceId, $"{_logsTestData.TableAName} | distinct * | project {LogsTestData.TimeGeneratedColumnName}", timespan);
            Response<LogsBatchQueryResultCollection> response = await client.QueryBatchAsync(batch);

            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));
        }
        public async Task CanQueryBatchMixed()
        {
            var client = CreateClient();
            LogsBatchQuery batch = new LogsBatchQuery();
            string id1 = batch.AddQuery(TestEnvironment.WorkspaceId, "Heartbeat", _logsTestData.DataTimeRange);
            string id2 = batch.AddQuery(TestEnvironment.WorkspaceId, "Heartbeats", _logsTestData.DataTimeRange);
            string id3 = batch.AddQuery(TestEnvironment.WorkspaceId, "set truncationmaxrecords=1; datatable (s: string) ['a', 'b']", _logsTestData.DataTimeRange);

            Response<LogsBatchQueryResultCollection> response = await client.QueryBatchAsync(batch);

            Assert.AreEqual(LogsQueryResultStatus.Success, response.Value.Single(r => r.Id == id1).Status);

            var failedResult = response.Value.Single(r => r.Id == id2);
            Assert.AreEqual(LogsQueryResultStatus.Failure, failedResult.Status);
            Assert.NotNull(failedResult.Error.Code);
            Assert.NotNull(failedResult.Error.Message);

            var partialResult = response.Value.Single(r => r.Id == id3);
            Assert.AreEqual(LogsQueryResultStatus.PartialFailure, partialResult.Status);
            CollectionAssert.IsNotEmpty(partialResult.Table.Rows);
            Assert.NotNull(partialResult.Error.Code);
            Assert.NotNull(partialResult.Error.Message);
        }