-
-
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.
Support for Azure Database for PostgreSQL resource type (#596)
* add configuration for PostgreSql scraper * add validation for PostgreSQL scraper * add scraper definition for PostgreSql * add documentation for PostgreSQL scraper
- Loading branch information
1 parent
cdeeb08
commit 07cb659
Showing
13 changed files
with
281 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
layout: default | ||
title: Azure Database for PostgreSQL | ||
--- | ||
|
||
## Azure Database for PostgreSQL - ![Availability Badge](https://img.shields.io/badge/Available%20Starting-v1.0.0-green.svg) | ||
You can declare to scrape an Azure Database for PostgreSQL server via the `PostgreSql` resource type. | ||
|
||
The following fields need to be provided: | ||
- `serverName` - The name of the PostgreSQL server | ||
|
||
All supported metrics are documented in the official [Azure Monitor documentation](https://docs.microsoft.com/en-us/azure/azure-monitor/platform/metrics-supported#microsoftdbforpostgresqlservers). | ||
|
||
Example: | ||
```yaml | ||
name: postgre_sql_cpu_percent | ||
description: "The CPU percentage on the server" | ||
resourceType: PostgreSql | ||
serverName: Promitor | ||
scraping: | ||
schedule: "0 */2 * ? * *" | ||
azureMetricConfiguration: | ||
metricName: cpu_percent | ||
aggregation: | ||
type: Average | ||
interval: 00:01:00 | ||
``` | ||
[← back to metrics declarations](/configuration/metrics)<br /> | ||
[← back to introduction](/) |
8 changes: 8 additions & 0 deletions
8
...tor.Core.Scraping/Configuration/Model/Metrics/ResourceTypes/PostgreSqlMetricDefinition.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,8 @@ | ||
namespace Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes | ||
{ | ||
public class PostgreSqlMetricDefinition : MetricDefinition | ||
{ | ||
public string ServerName { get; set; } | ||
public override ResourceType ResourceType { get; } = ResourceType.PostgreSql; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -12,5 +12,6 @@ public enum ResourceType | |
NetworkInterface = 7, | ||
CosmosDb = 8, | ||
RedisCache = 9, | ||
PostgreSql = 10, | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...r.Core.Scraping/Configuration/Serialization/Deserializers/PostgreSqlMetricDeserializer.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,27 @@ | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics; | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes; | ||
using YamlDotNet.RepresentationModel; | ||
|
||
namespace Promitor.Core.Scraping.Configuration.Serialization.Deserializers | ||
{ | ||
/// <summary> | ||
/// Defines a deserializer for the PostgreSQL Server resource type | ||
/// </summary> | ||
internal class PostgreSqlMetricDeserializer : GenericAzureMetricDeserializer | ||
{ | ||
/// <summary> | ||
/// Deserializes the specified PostgreSQL Server metric node from the YAML configuration file. | ||
/// </summary> | ||
/// <param name="metricNode">The metric node to deserialize to PostgreSQL Server configuration</param> | ||
/// <returns>A new <see cref="MetricDefinition"/> object (strongly typed as a <see cref="PostgreSqlMetricDefinition"/>) </returns> | ||
internal override MetricDefinition Deserialize(YamlMappingNode metricNode) | ||
{ | ||
var metricDefinition = base.DeserializeMetricDefinition<PostgreSqlMetricDefinition>(metricNode); | ||
|
||
var serverName = metricNode.Children[new YamlScalarNode("serverName")]; | ||
metricDefinition.ServerName = serverName?.ToString(); | ||
|
||
return metricDefinition; | ||
} | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
src/Promitor.Core.Scraping/ResourceTypes/PostgreSqlScraper.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,31 @@ | ||
using Microsoft.Azure.Management.Monitor.Fluent.Models; | ||
using Microsoft.Extensions.Logging; | ||
using Promitor.Core.Scraping.Configuration.Model; | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes; | ||
using Promitor.Core.Telemetry.Interfaces; | ||
using Promitor.Integrations.AzureMonitor; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Promitor.Core.Scraping.ResourceTypes | ||
{ | ||
public class PostgreSqlScraper : Scraper<PostgreSqlMetricDefinition> | ||
{ | ||
private const string ResourceUriTemplate = "subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.DBforPostgreSQL/servers/{2}"; | ||
|
||
public PostgreSqlScraper(AzureMetadata azureMetadata, AzureMonitorClient azureMonitorClient, ILogger logger, IExceptionTracker exceptionTracker) | ||
: base(azureMetadata, azureMonitorClient, logger, exceptionTracker) | ||
{ | ||
} | ||
|
||
protected override async Task<ScrapeResult> ScrapeResourceAsync(string subscriptionId, string resourceGroupName, PostgreSqlMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) | ||
{ | ||
var resourceUri = string.Format(ResourceUriTemplate, subscriptionId, resourceGroupName, metricDefinition.ServerName); | ||
|
||
var metricName = metricDefinition.AzureMetricConfiguration.MetricName; | ||
var foundMetricValue = await AzureMonitorClient.QueryMetricAsync(metricName, aggregationType, aggregationInterval, resourceUri); | ||
|
||
return new ScrapeResult(resourceUri, foundMetricValue); | ||
} | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
...itor.Scraper.Host/Validation/MetricDefinitions/ResourceTypes/PostgreSqlMetricValidator.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,19 @@ | ||
using GuardNet; | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes; | ||
using System.Collections.Generic; | ||
|
||
namespace Promitor.Scraper.Host.Validation.MetricDefinitions.ResourceTypes | ||
{ | ||
internal class PostgreSqlMetricValidator : MetricValidator<PostgreSqlMetricDefinition> | ||
{ | ||
protected override IEnumerable<string> Validate(PostgreSqlMetricDefinition postgreSqlMetricDefinition) | ||
{ | ||
Guard.NotNull(postgreSqlMetricDefinition, nameof(postgreSqlMetricDefinition)); | ||
|
||
if (string.IsNullOrWhiteSpace(postgreSqlMetricDefinition.ServerName)) | ||
{ | ||
yield return "No server name is configured"; | ||
} | ||
} | ||
} | ||
} |
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
77 changes: 77 additions & 0 deletions
77
...erialization/MetricsDeclaration/MetricsDeclarationWithPostgreSqlYamlSerializationTests.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,77 @@ | ||
using Bogus; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes; | ||
using Promitor.Core.Scraping.Configuration.Serialization.Core; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using Xunit; | ||
using MetricDefinition = Promitor.Core.Scraping.Configuration.Model.Metrics.MetricDefinition; | ||
|
||
namespace Promitor.Scraper.Tests.Unit.Serialization.MetricsDeclaration | ||
{ | ||
[Category("Unit")] | ||
public class MetricsDeclarationWithPostgreSqlYamlSerializationTests : YamlSerializationTests<PostgreSqlMetricDefinition> | ||
{ | ||
[Theory] | ||
[InlineData("promitor1", @"* */1 * * * *", @"* */2 * * * *")] | ||
[InlineData(null, null, null)] | ||
public void YamlSerialization_SerializeAndDeserializeConfigForPostgreSql_SucceedsWithIdenticalOutput(string resourceGroupName, string defaultScrapingInterval, string metricScrapingInterval) | ||
{ | ||
// Arrange | ||
var azureMetadata = GenerateBogusAzureMetadata(); | ||
var postgreSqlMetricDefinition = GenerateBogusPostgreSqlMetricDefinition(resourceGroupName, metricScrapingInterval); | ||
var metricDefaults = GenerateBogusMetricDefaults(defaultScrapingInterval); | ||
var scrapingConfiguration = new Core.Scraping.Configuration.Model.MetricsDeclaration() | ||
{ | ||
AzureMetadata = azureMetadata, | ||
MetricDefaults = metricDefaults, | ||
Metrics = new List<MetricDefinition>() | ||
{ | ||
postgreSqlMetricDefinition | ||
} | ||
}; | ||
var configurationSerializer = new ConfigurationSerializer(NullLogger.Instance); | ||
|
||
// Act | ||
var serializedConfiguration = configurationSerializer.Serialize(scrapingConfiguration); | ||
var deserializedConfiguration = configurationSerializer.Deserialize(serializedConfiguration); | ||
|
||
// Assert | ||
Assert.NotNull(deserializedConfiguration); | ||
AssertAzureMetadata(deserializedConfiguration, azureMetadata); | ||
AssertMetricDefaults(deserializedConfiguration, metricDefaults); | ||
Assert.NotNull(deserializedConfiguration.Metrics); | ||
Assert.Single(deserializedConfiguration.Metrics); | ||
var deserializedMetricDefinition = deserializedConfiguration.Metrics.FirstOrDefault(); | ||
AssertMetricDefinition(deserializedMetricDefinition, postgreSqlMetricDefinition); | ||
var deserializedPostgreSqlMetricDefinition = deserializedMetricDefinition as PostgreSqlMetricDefinition; | ||
AssertPostgreSqlMetricDefinition(deserializedPostgreSqlMetricDefinition, postgreSqlMetricDefinition); | ||
} | ||
|
||
private static void AssertPostgreSqlMetricDefinition(PostgreSqlMetricDefinition deserializedPostgreSqlMetricDefinition, PostgreSqlMetricDefinition postgreSqlMetricDefinition) | ||
{ | ||
Assert.NotNull(deserializedPostgreSqlMetricDefinition); | ||
Assert.Equal(postgreSqlMetricDefinition.ServerName, deserializedPostgreSqlMetricDefinition.ServerName); | ||
} | ||
|
||
private PostgreSqlMetricDefinition GenerateBogusPostgreSqlMetricDefinition(string resourceGroupName, string metricScrapingInterval) | ||
{ | ||
var bogusScrapingInterval = GenerateBogusScrapingInterval(metricScrapingInterval); | ||
var bogusAzureMetricConfiguration = GenerateBogusAzureMetricConfiguration(); | ||
|
||
var bogusGenerator = new Faker<PostgreSqlMetricDefinition>() | ||
.StrictMode(ensureRulesForAllProperties: true) | ||
.RuleFor(metricDefinition => metricDefinition.Name, faker => faker.Lorem.Word()) | ||
.RuleFor(metricDefinition => metricDefinition.Description, faker => faker.Lorem.Sentence(wordCount: 6)) | ||
.RuleFor(metricDefinition => metricDefinition.ResourceType, faker => Core.Scraping.Configuration.Model.ResourceType.PostgreSql) | ||
.RuleFor(metricDefinition => metricDefinition.ServerName, faker => faker.Lorem.Word()) | ||
.RuleFor(metricDefinition => metricDefinition.AzureMetricConfiguration, faker => bogusAzureMetricConfiguration) | ||
.RuleFor(metricDefinition => metricDefinition.ResourceGroupName, faker => resourceGroupName) | ||
.RuleFor(metricDefinition => metricDefinition.Scraping, faker => bogusScrapingInterval) | ||
.Ignore(metricDefinition => metricDefinition.ResourceGroupName); | ||
|
||
return bogusGenerator.Generate(); | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
....Unit/Validation/Metrics/ResourceTypes/PostgreSqlMetricsDeclarationValidationStepTests.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,63 @@ | ||
using Promitor.Scraper.Host.Validation.Steps; | ||
using Promitor.Scraper.Tests.Unit.Builders; | ||
using Promitor.Scraper.Tests.Unit.Stubs; | ||
using System.ComponentModel; | ||
using Xunit; | ||
|
||
namespace Promitor.Scraper.Tests.Unit.Validation.Metrics.ResourceTypes | ||
{ | ||
[Category("Unit")] | ||
public class PostgreSqlMetricsDeclarationValidationStepTests | ||
{ | ||
[Fact] | ||
public void PostgreSqlMetricsDeclaration_DeclarationWithoutAzureMetricName_Fails() | ||
{ | ||
// Arrange | ||
var rawDeclaration = MetricsDeclarationBuilder.WithMetadata() | ||
.WithPostgreSqlMetric(azureMetricName: string.Empty) | ||
.Build(); | ||
var metricsDeclarationProvider = new MetricsDeclarationProviderStub(rawDeclaration); | ||
|
||
// Act | ||
var scrapingScheduleValidationStep = new MetricsDeclarationValidationStep(metricsDeclarationProvider); | ||
var validationResult = scrapingScheduleValidationStep.Run(); | ||
|
||
// Assert | ||
Assert.False(validationResult.IsSuccessful, "Validation is not successful"); | ||
} | ||
|
||
[Fact] | ||
public void PostgreSqlMetricsDeclaration_DeclarationWithoutAzureMetricDescription_Succeeds() | ||
{ | ||
// Arrange | ||
var rawDeclaration = MetricsDeclarationBuilder.WithMetadata() | ||
.WithPostgreSqlMetric(metricDescription: string.Empty) | ||
.Build(); | ||
var metricsDeclarationProvider = new MetricsDeclarationProviderStub(rawDeclaration); | ||
|
||
// Act | ||
var scrapingScheduleValidationStep = new MetricsDeclarationValidationStep(metricsDeclarationProvider); | ||
var validationResult = scrapingScheduleValidationStep.Run(); | ||
|
||
// Assert | ||
Assert.True(validationResult.IsSuccessful, "Validation is successful"); | ||
} | ||
|
||
[Fact] | ||
public void PostgreSqlMetricsDeclaration_DeclarationWithoutServerName_Fails() | ||
{ | ||
// Arrange | ||
var rawDeclaration = MetricsDeclarationBuilder.WithMetadata() | ||
.WithPostgreSqlMetric(serverName: string.Empty) | ||
.Build(); | ||
var metricsDeclarationProvider = new MetricsDeclarationProviderStub(rawDeclaration); | ||
|
||
// Act | ||
var scrapingScheduleValidationStep = new MetricsDeclarationValidationStep(metricsDeclarationProvider); | ||
var validationResult = scrapingScheduleValidationStep.Run(); | ||
|
||
// Assert | ||
Assert.False(validationResult.IsSuccessful, "Validation is not successful"); | ||
} | ||
} | ||
} |