-
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simply scrapers to reduce duplicate code and make it easy to add… (#839)
* Reduce duplicate code in scraper Signed-off-by: Tom Kerkhove <[email protected]> * Use correct instance name and refactor resource definition Signed-off-by: Tom Kerkhove <[email protected]>
- Loading branch information
1 parent
3b5384a
commit c2aa7c4
Showing
45 changed files
with
337 additions
and
279 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System.Collections.Generic; | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics; | ||
|
||
// ReSharper disable All | ||
|
||
namespace Promitor.Core.Scraping | ||
{ | ||
/// <summary> | ||
/// Azure Monitor Scraper | ||
/// </summary> | ||
/// <typeparam name="TResourceDefinition">Type of metric definition that is being used</typeparam> | ||
public abstract class AppServiceScraper<TResourceDefinition> : AzureMonitorScraper<TResourceDefinition> | ||
where TResourceDefinition : class, IAppServiceResourceDefinition | ||
{ | ||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
protected AppServiceScraper(ScraperConfiguration scraperConfiguration) : | ||
base(scraperConfiguration) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Builds the URI without deployment slots of the App Service resource to scrape | ||
/// </summary> | ||
/// <param name="subscriptionId">Subscription id in which the resource lives</param> | ||
/// <param name="scrapeDefinition">Contains all the information needed to scrape the resource.</param> | ||
/// <param name="resource">Contains the resource cast to the specific resource type.</param> | ||
/// <returns>Uri of Azure resource</returns> | ||
protected abstract string BuildResourceUriWithoutDeploymentSlot(string subscriptionId, ScrapeDefinition<IAzureResourceDefinition> scrapeDefinition, TResourceDefinition resource); | ||
|
||
/// <inheritdoc /> | ||
protected override string BuildResourceUri(string subscriptionId, ScrapeDefinition<IAzureResourceDefinition> scrapeDefinition, TResourceDefinition resource) | ||
{ | ||
var slotName = DetermineSlotName(resource); | ||
var resourceUri = BuildResourceUriWithoutDeploymentSlot(subscriptionId, scrapeDefinition, resource); | ||
|
||
// Production slot should not be suffixed in resource URI | ||
if (slotName != "production") | ||
{ | ||
resourceUri += $"/slots/{slotName}"; | ||
} | ||
|
||
return resourceUri; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override Dictionary<string, string> DetermineMetricLabels(TResourceDefinition resourceDefinition) | ||
{ | ||
var metricLabels = base.DetermineMetricLabels(resourceDefinition); | ||
|
||
var slotName = DetermineSlotName(resourceDefinition); | ||
metricLabels?.TryAdd("slot_name", slotName); | ||
|
||
return metricLabels; | ||
} | ||
|
||
private static string DetermineSlotName(TResourceDefinition resource) | ||
{ | ||
return string.IsNullOrWhiteSpace(resource.SlotName) ? "production" : resource.SlotName; | ||
} | ||
} | ||
} |
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,58 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Management.Monitor.Fluent.Models; | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics; | ||
|
||
namespace Promitor.Core.Scraping | ||
{ | ||
/// <summary> | ||
/// Azure Monitor Scraper | ||
/// </summary> | ||
/// <typeparam name="TResourceDefinition">Type of metric definition that is being used</typeparam> | ||
public abstract class AzureMonitorScraper<TResourceDefinition> : Scraper<TResourceDefinition> | ||
where TResourceDefinition : class, IAzureResourceDefinition | ||
{ | ||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
protected AzureMonitorScraper(ScraperConfiguration scraperConfiguration) : | ||
base(scraperConfiguration) | ||
{ | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override async Task<ScrapeResult> ScrapeResourceAsync(string subscriptionId, ScrapeDefinition<IAzureResourceDefinition> scrapeDefinition, TResourceDefinition resourceDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) | ||
{ | ||
var resourceUri = BuildResourceUri(AzureMetadata.SubscriptionId, scrapeDefinition, resourceDefinition); | ||
|
||
var metricFilter = DetermineMetricFilter(resourceDefinition); | ||
var metricName = scrapeDefinition.AzureMetricConfiguration.MetricName; | ||
var dimensionName = scrapeDefinition.AzureMetricConfiguration.Dimension?.Name; | ||
var foundMetricValue = await AzureMonitorClient.QueryMetricAsync(metricName, dimensionName, aggregationType, aggregationInterval, resourceUri, metricFilter); | ||
|
||
var instanceName = resourceDefinition.GetResourceName(); | ||
var metricLabels = DetermineMetricLabels(resourceDefinition); | ||
|
||
return new ScrapeResult(subscriptionId, scrapeDefinition.ResourceGroupName, instanceName, resourceUri, foundMetricValue, metricLabels); | ||
} | ||
|
||
/// <summary> | ||
/// Determines the metric filter to use | ||
/// </summary> | ||
/// <param name="resourceDefinition">Contains the resource cast to the specific resource type.</param> | ||
protected virtual string DetermineMetricFilter(TResourceDefinition resourceDefinition) | ||
{ | ||
return null; | ||
} | ||
|
||
/// <summary> | ||
/// Determines the metric labels to include in the reported metric | ||
/// </summary> | ||
/// <param name="resourceDefinition">Contains the resource cast to the specific resource type.</param> | ||
protected virtual Dictionary<string, string> DetermineMetricLabels(TResourceDefinition resourceDefinition) | ||
{ | ||
return new Dictionary<string, string>(); | ||
} | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
src/Promitor.Core.Scraping/Configuration/Model/Metrics/IAppServiceResourceDefinition.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,7 @@ | ||
namespace Promitor.Core.Scraping.Configuration.Model.Metrics | ||
{ | ||
public interface IAppServiceResourceDefinition : IAzureResourceDefinition | ||
{ | ||
public string SlotName { get; } | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Promitor.Core.Scraping/Configuration/Model/Metrics/IAzureResourceDefinition.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,29 @@ | ||
namespace Promitor.Core.Scraping.Configuration.Model.Metrics | ||
{ | ||
/// <summary> | ||
/// Describes a resource in Azure that can be scraped. Inheriting classes can add whatever | ||
/// additional information is required to scrape a particular Azure resource. | ||
/// </summary> | ||
public interface IAzureResourceDefinition | ||
{ | ||
/// <summary> | ||
/// Type of resource that is configured | ||
/// </summary> | ||
public ResourceType ResourceType { get; } | ||
|
||
/// <summary> | ||
/// Specify a resource group to scrape that defers from the default resource group. | ||
/// This enables you to do multi-resource group scraping with one configuration file. | ||
/// </summary> | ||
public string ResourceGroupName { get; } | ||
|
||
/// <summary> | ||
/// Gets the name of the resource | ||
/// </summary> | ||
/// <remarks>This should return the name of the main resource</remarks> | ||
/// <example> | ||
/// For an Azure SQL Database it should be the name of the DB, not the server | ||
/// </example> | ||
string GetResourceName(); | ||
} | ||
} |
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
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
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
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
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
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
Oops, something went wrong.