-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Provide support for batch scraping (#2459)
Co-authored-by: Tom Kerkhove <[email protected]>
- Loading branch information
1 parent
880d10e
commit 5fa547b
Showing
42 changed files
with
1,488 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/Promitor.Core.Scraping/Batching/AzureResourceDefinitionBatching.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using GuardNet; | ||
using Promitor.Core.Contracts; | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics; | ||
|
||
namespace Promitor.Core.Scraping.Batching | ||
{ | ||
public static class AzureResourceDefinitionBatching | ||
{ | ||
/// <summary> | ||
/// groups scrape definitions based on following conditions: | ||
/// 1. Definitions in a batch must target the same resource type | ||
/// 2. Definitions in a batch must target the same Azure metric with identical dimensions | ||
/// 3. Definitions in a batch must have the same time granularity | ||
/// 4. Batch size cannot exceed configured maximum | ||
/// <see href="https://learn.microsoft.com/en-us/azure/azure-monitor/essentials/migrate-to-batch-api?tabs=individual-response#batching-restrictions"/> | ||
/// </summary> | ||
public static List<BatchScrapeDefinition<IAzureResourceDefinition>> GroupScrapeDefinitions(IEnumerable<ScrapeDefinition<IAzureResourceDefinition>> allScrapeDefinitions, int maxBatchSize) | ||
{ | ||
// ReSharper disable PossibleMultipleEnumeration | ||
Guard.NotNull(allScrapeDefinitions, nameof(allScrapeDefinitions)); | ||
|
||
return allScrapeDefinitions.GroupBy(def => def.BuildScrapingBatchInfo()) | ||
.ToDictionary(group => group.Key, group => group.ToList()) // first pass to build batches that could exceed max | ||
.ToDictionary(group => group.Key, group => SplitScrapeDefinitionBatch(group.Value, maxBatchSize)) // split to right-sized batches | ||
.SelectMany(group => group.Value.Select(batch => new BatchScrapeDefinition<IAzureResourceDefinition>(group.Key, batch))) | ||
.ToList(); // flatten | ||
} | ||
|
||
/// <summary> | ||
/// splits the "raw" batch according to max batch size configured | ||
/// </summary> | ||
private static List<List<ScrapeDefinition<IAzureResourceDefinition>>> SplitScrapeDefinitionBatch(List<ScrapeDefinition<IAzureResourceDefinition>> batchToSplit, int maxBatchSize) | ||
{ | ||
// ReSharper disable PossibleMultipleEnumeration | ||
Guard.NotNull(batchToSplit, nameof(batchToSplit)); | ||
|
||
int numNewGroups = ((batchToSplit.Count - 1) / maxBatchSize) + 1; | ||
|
||
return Enumerable.Range(0, numNewGroups) | ||
.Select(i => batchToSplit.Skip(i * maxBatchSize).Take(maxBatchSize).ToList()) | ||
.ToList(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/Promitor.Core.Scraping/Configuration/Model/Metrics/BatchScrapeDefinition.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Collections.Generic; | ||
using GuardNet; | ||
using Promitor.Core.Contracts; | ||
|
||
namespace Promitor.Core.Scraping.Configuration.Model.Metrics | ||
{ | ||
/// <summary> | ||
/// Defines a batch of ScrapeDefinitions to be executed in a single request | ||
/// Scrape definitions within a batch should share | ||
/// 1. The same resource type | ||
/// 2. The same Azure metric scrape target with identical dimensions | ||
/// 3. The same time granularity | ||
/// 4. The same filters | ||
/// </summary> | ||
public class BatchScrapeDefinition<TResourceDefinition> where TResourceDefinition : class, IAzureResourceDefinition | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of the <see cref="BatchScrapeDefinition{TResourceDefinition}"/> class. | ||
/// </summary> | ||
/// <param name="scrapeDefinitionBatchProperties">Shared Properties Among ScrapeDefinition's in the batch</param> | ||
/// <param name="groupedScrapeDefinitions">Scape definitions in the batch</param> | ||
public BatchScrapeDefinition(ScrapeDefinitionBatchProperties scrapeDefinitionBatchProperties, List<ScrapeDefinition<TResourceDefinition>> groupedScrapeDefinitions) | ||
{ | ||
Guard.NotNull(groupedScrapeDefinitions, nameof(groupedScrapeDefinitions)); | ||
Guard.NotLessThan(groupedScrapeDefinitions.Count, 1, nameof(groupedScrapeDefinitions)); | ||
Guard.NotNull(scrapeDefinitionBatchProperties, nameof(scrapeDefinitionBatchProperties)); | ||
|
||
ScrapeDefinitionBatchProperties = scrapeDefinitionBatchProperties; | ||
ScrapeDefinitions = groupedScrapeDefinitions; | ||
} | ||
|
||
/// <summary> | ||
/// A batch of scrape job definitions to be executed as a single request | ||
/// </summary> | ||
public List<ScrapeDefinition<TResourceDefinition>> ScrapeDefinitions { get; set; } | ||
|
||
public ScrapeDefinitionBatchProperties ScrapeDefinitionBatchProperties { get; set; } | ||
} | ||
} |
Oops, something went wrong.