Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename resource collections to resource discovery groups #1141

Merged
merged 3 commits into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ azureLandscape:
subscriptions:
- 0329dd2a-59dc-4493-aa54-cb01cb027dc2
- 0f9d7fea-99e8-4768-8672-06a28514f77e
resourceCollections:
resourceDiscoveryGroups:
- name: service-bus-landscape
type: ServiceBusQueue
criteria:
Expand Down
10 changes: 5 additions & 5 deletions config/promitor/scraper/metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ metrics:
type: Average
# Optionally override the default aggregation interval (metricDefaults.aggregation.interval)
interval: 00:15:00
resourceCollections:
resourceDiscoveryGroups:
- name: service-bus-landscape
- name: promitor_demo_servicebusqueue_queue_size
description: "Amount of active messages of the 'orders' queue (determined with ServiceBusQueue provider)"
Expand Down Expand Up @@ -153,7 +153,7 @@ metrics:
metricName: TotalPullCount
aggregation:
type: Average
resourceCollections:
resourceDiscoveryGroups:
- name: container-registry-landscape
- name: promitor_demo_appplan_percentage_cpu_discovered
description: "Average percentage of memory usage on an Azure App Plan"
Expand All @@ -162,7 +162,7 @@ metrics:
metricName: MemoryPercentage
aggregation:
type: Average
resourceCollections:
resourceDiscoveryGroups:
- name: app-plan-landscape
- name: promitor_demo_webapp_cpu_discovery
description: "Amount of CPU time used for an Azure Web App"
Expand All @@ -171,7 +171,7 @@ metrics:
metricName: CpuTime
aggregation:
type: Total
resourceCollections:
resourceDiscoveryGroups:
- name: web-app-landscape
- name: promitor_demo_function_memory_discovery
description: "Average memory for an Azure Function App"
Expand All @@ -180,5 +180,5 @@ metrics:
metricName: MemoryWorkingSet
aggregation:
type: Average
resourceCollections:
resourceDiscoveryGroups:
- name: function-app-landscape
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Promitor.Agents.ResourceDiscovery.Configuration
public class ResourceDeclaration
{
public AzureLandscape AzureLandscape { get; set; }
public List<ResourceCollection> ResourceCollections { get; set; }
public List<ResourceDiscoveryGroup> ResourceDiscoveryGroups { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Promitor.Agents.ResourceDiscovery.Configuration
{
public class ResourceCollection
public class ResourceDiscoveryGroup
{
public string Name { get; set; }
public ResourceType Type { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Promitor.Agents.ResourceDiscovery.Controllers
/// API endpoint to discover Azure resources
/// </summary>
[ApiController]
[Route("api/v1/resources/collections")]
[Route("api/v1/resources/groups")]
public class DiscoveryController : ControllerBase
{
private readonly JsonSerializerSettings _serializerSettings;
Expand All @@ -40,15 +40,15 @@ public DiscoveryController(ResourceRepository resourceRepository)
/// Discover Resources
/// </summary>
/// <remarks>Discovers Azure resources matching the criteria.</remarks>
[HttpGet("{resourceCollectionName}/discovery", Name = "Discovery_Get")]
public async Task<IActionResult> Get(string resourceCollectionName)
[HttpGet("{resourceDiscoveryGroup}/discover", Name = "Discovery_Get")]
public async Task<IActionResult> Get(string resourceDiscoveryGroup)
{
try
{
var foundResources = await _resourceRepository.GetResourcesAsync(resourceCollectionName);
var foundResources = await _resourceRepository.GetResourcesAsync(resourceDiscoveryGroup);
if (foundResources == null)
{
return NotFound(new {Information = "No resource collection was found with specified name"});
return NotFound(new {Information = "No resource discovery group was found with specified name"});
}

var serializedResources = JsonConvert.SerializeObject(foundResources, _serializerSettings);
Expand All @@ -59,7 +59,7 @@ public async Task<IActionResult> Get(string resourceCollectionName)
}
catch (ResourceTypeNotSupportedException resourceTypeNotSupportedException)
{
return StatusCode((int)HttpStatusCode.NotImplemented, new ResourceDiscoveryFailedDetails { Details=$"Resource type '{resourceTypeNotSupportedException.ResourceType}' for collection '{resourceCollectionName}' is not supported"});
return StatusCode((int)HttpStatusCode.NotImplemented, new ResourceDiscoveryFailedDetails { Details=$"Resource type '{resourceTypeNotSupportedException.ResourceType}' for discovery group '{resourceDiscoveryGroup}' is not supported"});
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@
namespace Promitor.Agents.ResourceDiscovery.Controllers
{
/// <summary>
/// API endpoint to interact with resource collections
/// API endpoint to interact with resource discovery groups
/// </summary>
[ApiController]
[Route("api/v1/resources/collections")]
public class ResourceCollectionsController : ControllerBase
[Route("api/v1/resources/groups")]
public class ResourceDiscoveryGroupsController : ControllerBase
{
private readonly IOptionsMonitor<ResourceDeclaration> _resourceDeclarationMonitor;

/// <summary>
/// Initializes a new instance of the <see cref="DiscoveryController" /> class.
/// Initializes a new instance of the <see cref="ResourceDiscoveryGroupsController" /> class.
/// </summary>
public ResourceCollectionsController(IOptionsMonitor<ResourceDeclaration> resourceDeclarationMonitor)
public ResourceDiscoveryGroupsController(IOptionsMonitor<ResourceDeclaration> resourceDeclarationMonitor)
{
Guard.NotNull(resourceDeclarationMonitor, nameof(resourceDeclarationMonitor));

_resourceDeclarationMonitor = resourceDeclarationMonitor;
}

/// <summary>
/// Get Resource Collections
/// Get Resource Discovery Groups
/// </summary>
/// <remarks>Lists all available resource collections.</remarks>
[HttpGet(Name = "ResourceCollections_Get")]
/// <remarks>Lists all available resource discovery groups.</remarks>
[HttpGet(Name = "ResourceDiscoveryGroups_Get")]
public IActionResult Get()
{
var resourceDeclaration = _resourceDeclarationMonitor.CurrentValue;
return Ok(resourceDeclaration.ResourceCollections);
return Ok(resourceDeclaration.ResourceDiscoveryGroups);
}
}
}
16 changes: 8 additions & 8 deletions src/Promitor.Agents.ResourceDiscovery/Docs/Open-Api.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ public ResourceRepository(AzureResourceGraph azureResourceGraph, IOptionsMonitor
/// <summary>
/// Get resources that are part of a given resource collection
/// </summary>
/// <param name="resourceCollectionName">Name of the resource collection</param>
public async Task<List<AzureResourceDefinition>> GetResourcesAsync(string resourceCollectionName)
/// <param name="resourceDiscoveryGroupName">Name of the resource collection</param>
public async Task<List<AzureResourceDefinition>> GetResourcesAsync(string resourceDiscoveryGroupName)
{
var resourceDeclaration = _resourceDeclarationMonitor.CurrentValue;
var resourceCollectionDefinition = resourceDeclaration.ResourceCollections.SingleOrDefault(collection => collection.Name.Equals(resourceCollectionName, StringComparison.InvariantCultureIgnoreCase));
if (resourceCollectionDefinition == null)
var resourceDiscoveryGroupDefinition = resourceDeclaration.ResourceDiscoveryGroups.SingleOrDefault(collection => collection.Name.Equals(resourceDiscoveryGroupName, StringComparison.InvariantCultureIgnoreCase));
if (resourceDiscoveryGroupDefinition == null)
{
// No collection found so nothing to return
return null;
}

var resourceDiscovery = ResourceDiscoveryFactory.UseResourceDiscoveryFor(resourceCollectionDefinition.Type);
var resourceDiscovery = ResourceDiscoveryFactory.UseResourceDiscoveryFor(resourceDiscoveryGroupDefinition.Type);

// 1. Create query per type
var query = resourceDiscovery.DefineQuery(resourceCollectionDefinition.Criteria);
var query = resourceDiscovery.DefineQuery(resourceDiscoveryGroupDefinition.Criteria);

// 2. Run Query
var unparsedResults = await _azureResourceGraph.QueryAsync(query);
Expand All @@ -59,8 +59,8 @@ public async Task<List<AzureResourceDefinition>> GetResourcesAsync(string resour

var contextualInformation = new Dictionary<string, object>
{
{"ResourceType",resourceCollectionDefinition.Type},
{"CollectionName",resourceCollectionName}
{"ResourceType",resourceDiscoveryGroupDefinition.Type},
{"CollectionName",resourceDiscoveryGroupName}
};
_logger.LogMetric("Discovered Resources", foundResources.Count, contextualInformation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public ResourceDiscoveryClient(IHttpClientFactory httpClientFactory, IOptionsMon
_httpClientFactory = httpClientFactory;
}

public async Task<List<AzureResourceDefinition>> GetAsync(string resourceCollectionName)
public async Task<List<AzureResourceDefinition>> GetAsync(string resourceDiscoveryGroupName)
{
var uri = $"/api/v1/resources/collections/{resourceCollectionName}/discovery";
var uri = $"/api/v1/resources/groups/{resourceDiscoveryGroupName}/discover";
var rawResponse = await SendGetRequestAsync(uri);

var foundResources = JsonConvert.DeserializeObject<List<AzureResourceDefinition>>(rawResponse, _serializerSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public ResourceDiscoveryRepository(ResourceDiscoveryClient resourceDiscoveryClie
_resourceDiscoveryClient = resourceDiscoveryClient;
}

public async Task<List<AzureResourceDefinition>> GetResourceCollectionAsync(string resourceCollectionName)
public async Task<List<AzureResourceDefinition>> GetResourceDiscoveryGroupAsync(string resourceDiscoveryGroupName)
{
Guard.NotNullOrWhitespace(resourceCollectionName,nameof(resourceCollectionName));
Guard.NotNullOrWhitespace(resourceDiscoveryGroupName,nameof(resourceDiscoveryGroupName));

var resources = await _resourceDiscoveryClient.GetAsync(resourceCollectionName);
var resources = await _resourceDiscoveryClient.GetAsync(resourceDiscoveryGroupName);
return resources;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static IServiceCollection DefineDependencies(this IServiceCollection serv
services.AddSingleton<IDeserializer<MetricDimensionV1>, MetricDimensionDeserializer>();
services.AddSingleton<IDeserializer<ScrapingV1>, ScrapingDeserializer>();
services.AddSingleton<IDeserializer<AzureMetricConfigurationV1>, AzureMetricConfigurationDeserializer>();
services.AddSingleton<IDeserializer<AzureResourceCollectionDefinitionV1>, AzureResourceCollectionDeserializer>();
services.AddSingleton<IDeserializer<AzureResourceDiscoveryGroupDefinitionV1>, AzureResourceDiscoveryGroupDeserializer>();
services.AddSingleton<IAzureResourceDeserializerFactory, AzureResourceDeserializerFactory>();
services.AddSingleton<IDeserializer<MetricAggregationV1>, MetricAggregationDeserializer>();
services.AddSingleton<IDeserializer<SecretV1>, SecretDeserializer>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace Promitor.Agents.Scraper.Scheduling
{
public class ResourceCollectionScrapingJob : MetricScrapingJob, IScheduledJob
public class ResourceDiscoveryGroupScrapingJob : MetricScrapingJob, IScheduledJob
{
private readonly ResourceDiscoveryRepository _resourceDiscoveryRepository;
private readonly MetricDefinition _metricDefinition;
Expand All @@ -33,14 +33,14 @@ public class ResourceCollectionScrapingJob : MetricScrapingJob, IScheduledJob

private readonly MetricScraperFactory _metricScraperFactory;

public ResourceCollectionScrapingJob(string jobName, string resourceCollectionName, AzureMetadata azureMetadata, MetricDefinition metricDefinition, ResourceDiscoveryRepository resourceDiscoveryRepository,
public ResourceDiscoveryGroupScrapingJob(string jobName, string resourceDiscoveryGroupName, AzureMetadata azureMetadata, MetricDefinition metricDefinition, ResourceDiscoveryRepository resourceDiscoveryRepository,
MetricSinkWriter metricSinkWriter,
MetricScraperFactory metricScraperFactory,
AzureMonitorClientFactory azureMonitorClientFactory, IRuntimeMetricsCollector runtimeMetricCollector, IConfiguration configuration, IOptions<AzureMonitorLoggingConfiguration> azureMonitorLoggingConfiguration, ILoggerFactory loggerFactory,
ILogger<ResourceCollectionScrapingJob> logger)
ILogger<ResourceDiscoveryGroupScrapingJob> logger)
: base(jobName, logger)
{
Guard.NotNullOrWhitespace(resourceCollectionName, nameof(resourceCollectionName));
Guard.NotNullOrWhitespace(resourceDiscoveryGroupName, nameof(resourceDiscoveryGroupName));
Guard.NotNull(resourceDiscoveryRepository, nameof(resourceDiscoveryRepository));
Guard.NotNull(metricDefinition, nameof(metricDefinition));
Guard.NotNull(azureMetadata, nameof(azureMetadata));
Expand All @@ -53,7 +53,7 @@ public ResourceCollectionScrapingJob(string jobName, string resourceCollectionNa
Guard.NotNull(loggerFactory, nameof(loggerFactory));
Guard.NotNull(metricSinkWriter, nameof(metricSinkWriter));

ResourceCollectionName = resourceCollectionName;
ResourceDiscoveryGroupName = resourceDiscoveryGroupName;

_azureMetadata = azureMetadata;
_metricDefinition = metricDefinition;
Expand All @@ -69,20 +69,20 @@ public ResourceCollectionScrapingJob(string jobName, string resourceCollectionNa
_metricScraperFactory = metricScraperFactory;
}

public string ResourceCollectionName { get; }
public string ResourceDiscoveryGroupName { get; }

public async Task ExecuteAsync(CancellationToken cancellationToken)
{
Logger.LogInformation("Scraping resource collection {ResourceCollection} - {Timestamp}", ResourceCollectionName, DateTimeOffset.UtcNow);
Logger.LogInformation("Scraping resource collection {ResourceDiscoveryGroup} - {Timestamp}", ResourceDiscoveryGroupName, DateTimeOffset.UtcNow);

try
{
var discoveredResources = await _resourceDiscoveryRepository.GetResourceCollectionAsync(ResourceCollectionName);
Logger.LogInformation("Discovered {ResourceCount} resources for resource collection {ResourceCollection}.", discoveredResources?.Count ?? 0, ResourceCollectionName);
var discoveredResources = await _resourceDiscoveryRepository.GetResourceDiscoveryGroupAsync(ResourceDiscoveryGroupName);
Logger.LogInformation("Discovered {ResourceCount} resources for resource collection {ResourceDiscoveryGroup}.", discoveredResources?.Count ?? 0, ResourceDiscoveryGroupName);

if (discoveredResources == null)
{
Logger.LogWarning("Discovered no resources for resource collection {ResourceCollection}.", ResourceCollectionName);
Logger.LogWarning("Discovered no resources for resource collection {ResourceDiscoveryGroup}.", ResourceDiscoveryGroupName);
return;
}

Expand All @@ -102,7 +102,7 @@ public async Task ExecuteAsync(CancellationToken cancellationToken)
}
catch (Exception exception)
{
Logger.LogCritical(exception, "Failed to scrape resource collection {ResourceCollection}: {Exception}", ResourceCollectionName, exception.Message);
Logger.LogCritical(exception, "Failed to scrape resource collection {ResourceDiscoveryGroup}: {Exception}", ResourceDiscoveryGroupName, exception.Message);
}
}

Expand Down
Loading