Skip to content

Commit

Permalink
Move azure custom cloud variables to yaml configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Suraiya-Hameed committed Jan 16, 2025
1 parent 19d0186 commit f5c9f1d
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class AzureResourceGraph : IAzureResourceGraph
private readonly IOptionsMonitor<ResourceDeclaration> _resourceDeclarationMonitor;
private readonly ISystemMetricsPublisher _systemMetricsPublisher;
private readonly ILogger<AzureResourceGraph> _logger;
private readonly IConfiguration _configuration;

private ResourceGraphClient _graphClient;

Expand All @@ -54,6 +55,7 @@ public AzureResourceGraph(ISystemMetricsPublisher systemMetricsPublisher, IOptio
_logger = logger;
_resourceDeclarationMonitor = resourceDeclarationMonitor;
_systemMetricsPublisher = systemMetricsPublisher;
_configuration = configuration;
_azureAuthenticationInfo = AzureAuthenticationFactory.GetConfiguredAzureAuthentication(configuration);
}

Expand Down Expand Up @@ -267,8 +269,8 @@ private async Task OpenConnectionAsync()

private async Task<ResourceGraphClient> CreateClientAsync()
{
var azureEnvironment = _resourceDeclarationMonitor.CurrentValue.AzureLandscape.Cloud.GetAzureEnvironment();
var azureAuthorityHost = _resourceDeclarationMonitor.CurrentValue.AzureLandscape.Cloud.GetAzureAuthorityHost();
var azureEnvironment = _resourceDeclarationMonitor.CurrentValue.AzureLandscape.Cloud.GetAzureEnvironment(_configuration);
var azureAuthorityHost = _resourceDeclarationMonitor.CurrentValue.AzureLandscape.Cloud.GetAzureAuthorityHost(_configuration);

var credentials = await AzureAuthenticationFactory.GetTokenCredentialsAsync(azureEnvironment.ManagementEndpoint, TenantId, _azureAuthenticationInfo, azureAuthorityHost);
var resourceManagerBaseUri = new Uri(azureEnvironment.ResourceManagerEndpoint);
Expand Down
4 changes: 2 additions & 2 deletions src/Promitor.Agents.Scraper/AzureMonitorClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public IAzureMonitorClient CreateIfNotExists(AzureCloud cloud, string tenantId,
if (useAzureMonitorSdk) {
azureMonitorClient = CreateNewAzureMonitorQueryClient(cloud, tenantId, subscriptionId, metricSinkWriter, azureScrapingSystemMetricsPublisher, resourceMetricDefinitionMemoryCache, configuration, azureMonitorIntegrationConfiguration, azureMonitorLoggingConfiguration, loggerFactory);
} else {
azureMonitorClient = CreateNewLegacyAzureMonitorClient(cloud.GetAzureEnvironment(), tenantId, subscriptionId, metricSinkWriter, azureScrapingSystemMetricsPublisher, resourceMetricDefinitionMemoryCache, configuration, azureMonitorIntegrationConfiguration, azureMonitorLoggingConfiguration, loggerFactory);
azureMonitorClient = CreateNewLegacyAzureMonitorClient(cloud.GetAzureEnvironment(configuration), tenantId, subscriptionId, metricSinkWriter, azureScrapingSystemMetricsPublisher, resourceMetricDefinitionMemoryCache, configuration, azureMonitorIntegrationConfiguration, azureMonitorLoggingConfiguration, loggerFactory);
}
_azureMonitorClients.TryAdd(subscriptionId, azureMonitorClient);

Expand All @@ -60,7 +60,7 @@ private static LegacyAzureMonitorClient CreateNewLegacyAzureMonitorClient(AzureE
private static AzureMonitorQueryClient CreateNewAzureMonitorQueryClient(AzureCloud cloud, string tenantId, string subscriptionId, MetricSinkWriter metricSinkWriter, IAzureScrapingSystemMetricsPublisher azureScrapingSystemMetricsPublisher, IMemoryCache resourceMetricDefinitionMemoryCache, IConfiguration configuration, IOptions<AzureMonitorIntegrationConfiguration> azureMonitorIntegrationConfiguration, IOptions<AzureMonitorLoggingConfiguration> azureMonitorLoggingConfiguration, ILoggerFactory loggerFactory)
{
var azureCredentials = AzureAuthenticationFactory.GetConfiguredAzureAuthentication(configuration);
var azureMonitorQueryClient = new AzureMonitorQueryClient(cloud, tenantId, subscriptionId, azureCredentials, metricSinkWriter, azureScrapingSystemMetricsPublisher, resourceMetricDefinitionMemoryCache, loggerFactory, azureMonitorIntegrationConfiguration, azureMonitorLoggingConfiguration);
var azureMonitorQueryClient = new AzureMonitorQueryClient(cloud, tenantId, subscriptionId, azureCredentials, metricSinkWriter, azureScrapingSystemMetricsPublisher, resourceMetricDefinitionMemoryCache, loggerFactory, azureMonitorIntegrationConfiguration, azureMonitorLoggingConfiguration, configuration);
return azureMonitorQueryClient;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,10 @@ private async Task ScrapeMetricBatched(BatchScrapeDefinition<IAzureResourceDefin
var azureMonitorClient = _azureMonitorClientFactory.CreateIfNotExists(_metricsDeclaration.AzureMetadata.Cloud, _metricsDeclaration.AzureMetadata.TenantId,
resourceSubscriptionId, _metricSinkWriter, _azureScrapingSystemMetricsPublisher, _resourceMetricDefinitionMemoryCache, _configuration,
_azureMonitorIntegrationConfiguration, _azureMonitorLoggingConfiguration, _loggerFactory);
var azureEnvironent = _metricsDeclaration.AzureMetadata.Cloud.GetAzureEnvironment();
var azureEnvironent = _metricsDeclaration.AzureMetadata.Cloud.GetAzureEnvironment(_configuration);

var tokenCredential = AzureAuthenticationFactory.GetTokenCredential(azureEnvironent.ManagementEndpoint, _metricsDeclaration.AzureMetadata.TenantId,
AzureAuthenticationFactory.GetConfiguredAzureAuthentication(_configuration), new Uri(_metricsDeclaration.AzureMetadata.Cloud.GetAzureEnvironment().AuthenticationEndpoint));
AzureAuthenticationFactory.GetConfiguredAzureAuthentication(_configuration), new Uri(_metricsDeclaration.AzureMetadata.Cloud.GetAzureEnvironment(_configuration).AuthenticationEndpoint));
var logAnalyticsClient = new LogAnalyticsClient(_loggerFactory, azureEnvironent, tokenCredential);

var scraper = _metricScraperFactory.CreateScraper(batchScrapeDefinition.ScrapeDefinitionBatchProperties.ResourceType, _metricSinkWriter, _azureScrapingSystemMetricsPublisher, azureMonitorClient, logAnalyticsClient);
Expand All @@ -313,14 +313,14 @@ private async Task ScrapeMetric(ScrapeDefinition<IAzureResourceDefinition> scrap
var resourceSubscriptionId = !string.IsNullOrWhiteSpace(scrapeDefinition.Resource.SubscriptionId)
? scrapeDefinition.Resource.SubscriptionId
: _metricsDeclaration.AzureMetadata.SubscriptionId;
var azureEnvironent = _metricsDeclaration.AzureMetadata.Cloud.GetAzureEnvironment();
var azureEnvironent = _metricsDeclaration.AzureMetadata.Cloud.GetAzureEnvironment(_configuration);
Logger.LogInformation("Parsed SDK Config {UseAzureMonitorSdk}", _azureMonitorIntegrationConfiguration.Value.UseAzureMonitorSdk);
var azureMonitorClient = _azureMonitorClientFactory.CreateIfNotExists(_metricsDeclaration.AzureMetadata.Cloud, _metricsDeclaration.AzureMetadata.TenantId,
resourceSubscriptionId, _metricSinkWriter, _azureScrapingSystemMetricsPublisher, _resourceMetricDefinitionMemoryCache, _configuration,
_azureMonitorIntegrationConfiguration, _azureMonitorLoggingConfiguration, _loggerFactory);

var tokenCredential = AzureAuthenticationFactory.GetTokenCredential(azureEnvironent.ManagementEndpoint, _metricsDeclaration.AzureMetadata.TenantId,
AzureAuthenticationFactory.GetConfiguredAzureAuthentication(_configuration), new Uri(_metricsDeclaration.AzureMetadata.Cloud.GetAzureEnvironment().AuthenticationEndpoint));
AzureAuthenticationFactory.GetConfiguredAzureAuthentication(_configuration), new Uri(_metricsDeclaration.AzureMetadata.Cloud.GetAzureEnvironment(_configuration).AuthenticationEndpoint));
var logAnalyticsClient = new LogAnalyticsClient(_loggerFactory, azureEnvironent, tokenCredential);

var scraper = _metricScraperFactory.CreateScraper(scrapeDefinition.Resource.ResourceType, _metricSinkWriter, _azureScrapingSystemMetricsPublisher, azureMonitorClient, logAnalyticsClient);
Expand Down
9 changes: 5 additions & 4 deletions src/Promitor.Core/Extensions/AzureCloudExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Azure.Identity;
using Azure.Monitor.Query;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Extensions.Configuration;
using Promitor.Core.Serialization.Enum;

namespace Promitor.Core.Extensions
Expand All @@ -13,7 +14,7 @@ public static class AzureCloudExtensions
/// </summary>
/// <param name="azureCloud">Microsoft Azure cloud</param>
/// <returns>Azure environment information for specified cloud</returns>
public static AzureEnvironment GetAzureEnvironment(this AzureCloud azureCloud)
public static AzureEnvironment GetAzureEnvironment(this AzureCloud azureCloud, IConfiguration configuration)
{
switch (azureCloud)
{
Expand All @@ -26,7 +27,7 @@ public static AzureEnvironment GetAzureEnvironment(this AzureCloud azureCloud)
case AzureCloud.UsGov:
return AzureEnvironment.AzureUSGovernment;
case AzureCloud.Custom:
return AzureEnvironmentExtensions.AzureCustomCloud;
return AzureEnvironmentExtensions.GetAzureCustomCloud(configuration);
default:
throw new ArgumentOutOfRangeException(nameof(azureCloud), "No Azure environment is known for in legacy SDK");
}
Expand Down Expand Up @@ -71,7 +72,7 @@ public static MetricsClientAudience DetermineMetricsClientBatchQueryAudience(thi
}


public static Uri GetAzureAuthorityHost(this AzureCloud azureCloud)
public static Uri GetAzureAuthorityHost(this AzureCloud azureCloud, IConfiguration configuration)
{
switch (azureCloud)
{
Expand All @@ -84,7 +85,7 @@ public static Uri GetAzureAuthorityHost(this AzureCloud azureCloud)
case AzureCloud.UsGov:
return AzureAuthorityHosts.AzureGovernment;
case AzureCloud.Custom:
return new Uri(Environment.GetEnvironmentVariable("PROMITOR_AZURE_AUTH_ENDPOINT"));
return new Uri(AzureEnvironmentExtensions.GetAzureCustomCloud(configuration).AuthenticationEndpoint);
default:
throw new ArgumentOutOfRangeException(nameof(azureCloud), "No Azure environment is known for");
}
Expand Down
30 changes: 20 additions & 10 deletions src/Promitor.Core/Extensions/AzureEnvironmentExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Humanizer;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Extensions.Configuration;
using System;

namespace Promitor.Core.Extensions
Expand All @@ -16,15 +17,24 @@ public static string GetDisplayName(this AzureEnvironment azureCloud)
return azureCloud.Name.Replace("Azure", "").Replace("Cloud", "").Humanize(LetterCasing.Title);
}

public static AzureEnvironment AzureCustomCloud = new AzureEnvironment()
{
Name = nameof(AzureCustomCloud),
AuthenticationEndpoint = Environment.GetEnvironmentVariable("PROMITOR_AUTH_ENDPOINT"),
ResourceManagerEndpoint = Environment.GetEnvironmentVariable("PROMITOR_RESOURCE_MANAGER_ENDPOINT"),
ManagementEndpoint = Environment.GetEnvironmentVariable("PROMITOR_MANAGEMENT_ENDPOINT"),
GraphEndpoint = Environment.GetEnvironmentVariable("PROMITOR_GRAPH_ENDPOINT"),
StorageEndpointSuffix = Environment.GetEnvironmentVariable("PROMITOR_STORAGE_ENDPOINT_SUFFIX"),
KeyVaultSuffix = Environment.GetEnvironmentVariable("PROMITOR_KEY_VAULT_SUFFIX")
};

public static AzureEnvironment GetAzureCustomCloud(IConfiguration configuration) {
AzureEnvironment azureCustomCloud = configuration.GetSection("azureCustomCloud").Get<AzureEnvironment>();
if (azureCustomCloud == null)
{
throw new InvalidOperationException("Custom Azure Cloud configuration is missing or invalid.");
}
if (string.IsNullOrWhiteSpace(azureCustomCloud.AuthenticationEndpoint) ||
string.IsNullOrWhiteSpace(azureCustomCloud.ResourceManagerEndpoint) ||
string.IsNullOrWhiteSpace(azureCustomCloud.ManagementEndpoint) ||
string.IsNullOrWhiteSpace(azureCustomCloud.GraphEndpoint) ||
string.IsNullOrWhiteSpace(azureCustomCloud.StorageEndpointSuffix) ||
string.IsNullOrWhiteSpace(azureCustomCloud.KeyVaultSuffix))
{
throw new InvalidOperationException("One or more required Azure custom cloud configuration values are missing.");
}
azureCustomCloud.Name = "AzureCustomCloud";
return azureCustomCloud;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
using Azure.Core.Diagnostics;
using System.Diagnostics.Tracing;
using Promitor.Integrations.AzureMonitor.Extensions;
using Microsoft.Extensions.Configuration;
using static Promitor.Core.EnvironmentVariables;

namespace Promitor.Integrations.AzureMonitor
{
Expand All @@ -48,7 +50,7 @@ public class AzureMonitorQueryClient : IAzureMonitorClient
/// <param name="loggerFactory">Factory to create loggers with</param>
/// <param name="azureMonitorIntegrationConfiguration">Options for Azure Monitor integration</param>
/// <param name="azureMonitorLoggingConfiguration">Options for Azure Monitor logging</param>
public AzureMonitorQueryClient(AzureCloud azureCloud, string tenantId, string subscriptionId, AzureAuthenticationInfo azureAuthenticationInfo, MetricSinkWriter metricSinkWriter, IAzureScrapingSystemMetricsPublisher azureScrapingSystemMetricsPublisher, IMemoryCache resourceMetricDefinitionMemoryCache, ILoggerFactory loggerFactory, IOptions<AzureMonitorIntegrationConfiguration> azureMonitorIntegrationConfiguration, IOptions<AzureMonitorLoggingConfiguration> azureMonitorLoggingConfiguration)
public AzureMonitorQueryClient(AzureCloud azureCloud, string tenantId, string subscriptionId, AzureAuthenticationInfo azureAuthenticationInfo, MetricSinkWriter metricSinkWriter, IAzureScrapingSystemMetricsPublisher azureScrapingSystemMetricsPublisher, IMemoryCache resourceMetricDefinitionMemoryCache, ILoggerFactory loggerFactory, IOptions<AzureMonitorIntegrationConfiguration> azureMonitorIntegrationConfiguration, IOptions<AzureMonitorLoggingConfiguration> azureMonitorLoggingConfiguration, IConfiguration configuration)
{
Guard.NotNullOrWhitespace(tenantId, nameof(tenantId));
Guard.NotNullOrWhitespace(subscriptionId, nameof(subscriptionId));
Expand All @@ -60,10 +62,10 @@ public AzureMonitorQueryClient(AzureCloud azureCloud, string tenantId, string su
_resourceMetricDefinitionMemoryCache = resourceMetricDefinitionMemoryCache;
_azureMonitorIntegrationConfiguration = azureMonitorIntegrationConfiguration;
_logger = loggerFactory.CreateLogger<AzureMonitorQueryClient>();
_metricsQueryClient = CreateAzureMonitorMetricsClient(azureCloud, tenantId, subscriptionId, azureAuthenticationInfo, metricSinkWriter, azureScrapingSystemMetricsPublisher, azureMonitorLoggingConfiguration);
_metricsQueryClient = CreateAzureMonitorMetricsClient(azureCloud, tenantId, subscriptionId, azureAuthenticationInfo, metricSinkWriter, azureScrapingSystemMetricsPublisher, azureMonitorLoggingConfiguration, configuration);
if (_azureMonitorIntegrationConfiguration.Value.MetricsBatching.Enabled)
{
_metricsBatchQueryClient = CreateAzureMonitorMetricsBatchClient(azureCloud, tenantId, azureAuthenticationInfo, azureMonitorIntegrationConfiguration, azureMonitorLoggingConfiguration);
_metricsBatchQueryClient = CreateAzureMonitorMetricsBatchClient(azureCloud, tenantId, azureAuthenticationInfo, azureMonitorIntegrationConfiguration, azureMonitorLoggingConfiguration, configuration);
}
}

Expand Down Expand Up @@ -255,15 +257,15 @@ private MetricValue GetMostRecentMetricValue(string metricName, MetricTimeSeries
/// <summary>
/// Creates authenticated client to query for metrics
/// </summary>
private MetricsQueryClient CreateAzureMonitorMetricsClient(AzureCloud azureCloud, string tenantId, string subscriptionId, AzureAuthenticationInfo azureAuthenticationInfo, MetricSinkWriter metricSinkWriter, IAzureScrapingSystemMetricsPublisher azureScrapingSystemMetricsPublisher, IOptions<AzureMonitorLoggingConfiguration> azureMonitorLoggingConfiguration) {
private MetricsQueryClient CreateAzureMonitorMetricsClient(AzureCloud azureCloud, string tenantId, string subscriptionId, AzureAuthenticationInfo azureAuthenticationInfo, MetricSinkWriter metricSinkWriter, IAzureScrapingSystemMetricsPublisher azureScrapingSystemMetricsPublisher, IOptions<AzureMonitorLoggingConfiguration> azureMonitorLoggingConfiguration, IConfiguration configuration) {
var metricsQueryClientOptions = new MetricsQueryClientOptions{
Audience = azureCloud.DetermineMetricsClientAudience(),
};
var azureRateLimitPolicy = new RecordArmRateLimitMetricsPolicy(tenantId, subscriptionId, azureAuthenticationInfo, metricSinkWriter, azureScrapingSystemMetricsPublisher);
var addPromitorUserAgentPolicy = new RegisterPromitorAgentPolicy(tenantId, subscriptionId, azureAuthenticationInfo, metricSinkWriter);
metricsQueryClientOptions.AddPolicy(azureRateLimitPolicy, HttpPipelinePosition.PerCall);
metricsQueryClientOptions.AddPolicy(addPromitorUserAgentPolicy, HttpPipelinePosition.BeforeTransport);
var tokenCredential = AzureAuthenticationFactory.GetTokenCredential(nameof(azureCloud), tenantId, azureAuthenticationInfo, azureCloud.GetAzureAuthorityHost());
var tokenCredential = AzureAuthenticationFactory.GetTokenCredential(nameof(azureCloud), tenantId, azureAuthenticationInfo, azureCloud.GetAzureAuthorityHost(configuration));

var azureMonitorLogging = azureMonitorLoggingConfiguration.Value;
if (azureMonitorLogging.IsEnabled)
Expand All @@ -277,7 +279,7 @@ private MetricsQueryClient CreateAzureMonitorMetricsClient(AzureCloud azureCloud
/// <summary>
/// Creates authenticated client for metrics batch queries
/// </summary>
private MetricsClient CreateAzureMonitorMetricsBatchClient(AzureCloud azureCloud, string tenantId, AzureAuthenticationInfo azureAuthenticationInfo, IOptions<AzureMonitorIntegrationConfiguration> azureMonitorIntegrationConfiguration, IOptions<AzureMonitorLoggingConfiguration> azureMonitorLoggingConfiguration) {
private MetricsClient CreateAzureMonitorMetricsBatchClient(AzureCloud azureCloud, string tenantId, AzureAuthenticationInfo azureAuthenticationInfo, IOptions<AzureMonitorIntegrationConfiguration> azureMonitorIntegrationConfiguration, IOptions<AzureMonitorLoggingConfiguration> azureMonitorLoggingConfiguration, IConfiguration configuration) {
var azureRegion = azureMonitorIntegrationConfiguration.Value.MetricsBatching.AzureRegion;
var metricsClientOptions = new MetricsClientOptions{
Audience = azureCloud.DetermineMetricsClientBatchQueryAudience(),
Expand All @@ -289,7 +291,7 @@ private MetricsClient CreateAzureMonitorMetricsBatchClient(AzureCloud azureCloud
MaxDelay = TimeSpan.FromSeconds(30),
}
}; // retry policy as suggested in the documentation: https://learn.microsoft.com/en-us/azure/azure-monitor/essentials/migrate-to-batch-api?tabs=individual-response#529-throttling-errors
var tokenCredential = AzureAuthenticationFactory.GetTokenCredential(nameof(azureCloud), tenantId, azureAuthenticationInfo, azureCloud.GetAzureAuthorityHost());
var tokenCredential = AzureAuthenticationFactory.GetTokenCredential(nameof(azureCloud), tenantId, azureAuthenticationInfo, azureCloud.GetAzureAuthorityHost(configuration));
metricsClientOptions.AddPolicy(new ModifyOutgoingAzureMonitorRequestsPolicy(_logger), HttpPipelinePosition.BeforeTransport);
var azureMonitorLogging = azureMonitorLoggingConfiguration.Value;
if (azureMonitorLogging.IsEnabled)
Expand Down
Loading

0 comments on commit f5c9f1d

Please sign in to comment.