-
-
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.
Implementation of Cosmos DB scraper (#472)
* Implementation of Cosmos DB scraper with TotalRequests metric * Implementation of Cosmos DB Scraper * fixed minor type in CosmosDB serialization unit test
- Loading branch information
Showing
17 changed files
with
311 additions
and
4 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 |
---|---|---|
|
@@ -300,4 +300,4 @@ docs/_site/* | |
#MAC | ||
.DS_Store | ||
|
||
*.orig | ||
*.orig |
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,27 @@ | ||
--- | ||
layout: default | ||
title: Cosmos Db Declaration | ||
--- | ||
|
||
## Cosmos Db | ||
You can declare to scrape Cosmos Db via the `CosmosDb` resource type. | ||
|
||
The following fields need to be provided: | ||
- `dbName`- The name of the Cosmos Db to be scraped | ||
|
||
All supported metrics are documented in the official [Azure Monitor documentation](https://docs.microsoft.com/en-us/azure/azure-monitor/platform/metrics-supported#microsoftcontainerinstancecontainergroups). | ||
|
||
Example: | ||
```yaml | ||
name: demo_cosmos_totalrequests | ||
description: "Demo cosmos query" | ||
resourceType: CosmosDb | ||
dbName: cognitiveanalytics | ||
azureMetricConfiguration: | ||
metricName: TotalRequests | ||
aggregation: | ||
type: Count | ||
``` | ||
[← back to metrics declarations](/configuration/metrics) | ||
[← back to introduction](/) |
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
9 changes: 9 additions & 0 deletions
9
...mitor.Core.Scraping/Configuration/Model/Metrics/ResourceTypes/CosmosDbMetricDefinition.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,9 @@ | ||
namespace Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes | ||
{ | ||
public class CosmosDbMetricDefinition : MetricDefinition | ||
{ | ||
public string DbName { get; set; } | ||
|
||
public override ResourceType ResourceType { get; } = ResourceType.CosmosDb; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -10,5 +10,6 @@ public enum ResourceType | |
VirtualMachine = 5, | ||
ContainerRegistry = 6, | ||
NetworkInterface = 7, | ||
CosmosDb = 8 | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/Promitor.Core.Scraping/Configuration/Serialization/Core/MetricDeserializer.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
23 changes: 23 additions & 0 deletions
23
...tor.Core.Scraping/Configuration/Serialization/Deserializers/CosmosDbMetricDeserializer.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,23 @@ | ||
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 | ||
{ | ||
internal class CosmosDbMetricDeserializer : GenericAzureMetricDeserializer | ||
{ | ||
/// <summary>Deserializes the specified Cosmos DB metric node from the YAML configuration file.</summary> | ||
/// <param name="metricNode">The metric node to deserialize to Cosmos DB configuration</param> | ||
/// <returns>A new <see cref="MetricDefinition"/> object (strongly typed as a <see cref="CosmosDbMetricDefinition"/>) </returns> | ||
internal override MetricDefinition Deserialize(YamlMappingNode metricNode) | ||
{ | ||
var metricDefinition = base.DeserializeMetricDefinition<CosmosDbMetricDefinition>(metricNode); | ||
|
||
var dbName = metricNode.Children[new YamlScalarNode("dbName")]; | ||
|
||
metricDefinition.DbName = dbName?.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/CosmosDbScraper.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 System; | ||
using System.Threading.Tasks; | ||
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; | ||
|
||
namespace Promitor.Core.Scraping.ResourceTypes | ||
{ | ||
public class CosmosDbScraper : Scraper<CosmosDbMetricDefinition> | ||
{ | ||
private const string ResourceUriTemplate = "subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.DocumentDB/databaseAccounts/{2}"; | ||
|
||
public CosmosDbScraper(AzureMetadata azureMetadata, AzureMonitorClient azureMonitorClient, ILogger logger, IExceptionTracker exceptionTracker) | ||
: base(azureMetadata, azureMonitorClient, logger, exceptionTracker) | ||
{ | ||
} | ||
|
||
protected override async Task<double> ScrapeResourceAsync(string subscriptionId, string resourceGroupName, CosmosDbMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) | ||
{ | ||
var resourceUri = string.Format(ResourceUriTemplate, subscriptionId, resourceGroupName, metricDefinition.DbName); | ||
|
||
var metricName = metricDefinition.AzureMetricConfiguration.MetricName; | ||
var foundMetricValue = await AzureMonitorClient.QueryMetricAsync(metricName, aggregationType, aggregationInterval, resourceUri); | ||
|
||
return 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
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
...omitor.Scraper.Host/Validation/MetricDefinitions/ResourceTypes/CosmosDbMetricValidator.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 System.Collections.Generic; | ||
using GuardNet; | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes; | ||
|
||
namespace Promitor.Scraper.Host.Validation.MetricDefinitions.ResourceTypes | ||
{ | ||
internal class CosmosDbMetricValidator : MetricValidator<CosmosDbMetricDefinition> | ||
{ | ||
protected override IEnumerable<string> Validate(CosmosDbMetricDefinition cosmosDbMetricDefinition) | ||
{ | ||
Guard.NotNull(cosmosDbMetricDefinition, nameof(cosmosDbMetricDefinition)); | ||
|
||
if (string.IsNullOrWhiteSpace(cosmosDbMetricDefinition.DbName)) | ||
{ | ||
yield return "No Cosmos db 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
78 changes: 78 additions & 0 deletions
78
.../Serialization/MetricsDeclaration/MetricsDeclarationWithCosmosDbYamlSerializationTests.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,78 @@ | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using Bogus; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Promitor.Core.Scraping.Configuration.Model; | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes; | ||
using Promitor.Core.Scraping.Configuration.Serialization.Core; | ||
using Xunit; | ||
using MetricDefinition = Promitor.Core.Scraping.Configuration.Model.Metrics.MetricDefinition; | ||
|
||
namespace Promitor.Scraper.Tests.Unit.Serialization.MetricsDeclaration | ||
{ | ||
[Category("Unit")] | ||
public class MetricsDeclarationWithCosmosDbYamlSerializationTests : YamlSerializationTests<CosmosDbMetricDefinition> | ||
{ | ||
[Theory] | ||
[InlineData("promitor1", @"* */1 * * * *", @"* */2 * * * *")] | ||
[InlineData(null, null, null)] | ||
public void YamlSerialization_SerializeAndDeserializeValidConfigForCosmosDb_SucceedsWithIdenticalOutput(string resourceGroupName, string defaultScrapingInterval, string metricScrapingInterval) | ||
{ | ||
// Arrange | ||
var azureMetadata = GenerateBogusAzureMetadata(); | ||
var cosmosDbMetricDefinition = GenerateBogusCosmosDbMetricDefinition(resourceGroupName, metricScrapingInterval); | ||
var metricDefaults = GenerateBogusMetricDefaults(defaultScrapingInterval); | ||
var scrapingConfiguration = new Core.Scraping.Configuration.Model.MetricsDeclaration | ||
{ | ||
AzureMetadata = azureMetadata, | ||
MetricDefaults = metricDefaults, | ||
Metrics = new List<MetricDefinition> | ||
{ | ||
cosmosDbMetricDefinition | ||
} | ||
}; | ||
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, cosmosDbMetricDefinition); | ||
var deserializedCosmosDbMetricDefinition = deserializedMetricDefinition as CosmosDbMetricDefinition; | ||
AssertCosmosDbMetricDefinition(deserializedCosmosDbMetricDefinition, cosmosDbMetricDefinition); | ||
} | ||
|
||
private static void AssertCosmosDbMetricDefinition(CosmosDbMetricDefinition deserializedCosmosDbMetricDefinition, CosmosDbMetricDefinition cosmosDbMetricDefinition) | ||
{ | ||
Assert.NotNull(deserializedCosmosDbMetricDefinition); | ||
Assert.Equal(cosmosDbMetricDefinition.DbName, deserializedCosmosDbMetricDefinition.DbName); | ||
} | ||
|
||
private CosmosDbMetricDefinition GenerateBogusCosmosDbMetricDefinition(string resourceGroupName, string metricScrapingInterval) | ||
{ | ||
var bogusScrapingInterval = GenerateBogusScrapingInterval(metricScrapingInterval); | ||
var bogusAzureMetricConfiguration = GenerateBogusAzureMetricConfiguration(); | ||
|
||
var bogusGenerator = new Faker<CosmosDbMetricDefinition>() | ||
.StrictMode(ensureRulesForAllProperties: true) | ||
.RuleFor(metricDefinition => metricDefinition.Name, faker => faker.Name.FirstName()) | ||
.RuleFor(metricDefinition => metricDefinition.Description, faker => faker.Lorem.Sentence(wordCount: 6)) | ||
.RuleFor(metricDefinition => metricDefinition.ResourceType, faker => ResourceType.CosmosDb) | ||
.RuleFor(metricDefinition => metricDefinition.DbName, faker => faker.Name.LastName()) | ||
.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(); | ||
} | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...ts.Unit/Validation/Metrics/ResourceTypes/CosmosDbMetricsDeclarationValidationStepTests.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,97 @@ | ||
using System.ComponentModel; | ||
using Promitor.Scraper.Host.Validation.Steps; | ||
using Promitor.Scraper.Tests.Unit.Builders; | ||
using Promitor.Scraper.Tests.Unit.Stubs; | ||
using Xunit; | ||
|
||
namespace Promitor.Scraper.Tests.Unit.Validation.Metrics.ResourceTypes | ||
{ | ||
[Category("Unit")] | ||
public class CosmosDbMetricsDeclarationValidationStepTests | ||
{ | ||
[Fact] | ||
public void CosmosDbMetricsDeclaration_DeclarationWithoutAzureMetricName_Succeeds() | ||
{ | ||
// Arrange | ||
var rawDeclaration = MetricsDeclarationBuilder.WithMetadata() | ||
.WithCosmosDbMetric(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 successful"); | ||
} | ||
|
||
[Fact] | ||
public void CosmosDbMetricsDeclaration_DeclarationWithoutMetricDescription_Succeeded() | ||
{ | ||
// Arrange | ||
var rawDeclaration = MetricsDeclarationBuilder.WithMetadata() | ||
.WithCosmosDbMetric(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 was not successful"); | ||
} | ||
|
||
[Fact] | ||
public void CosmosDbMetricsDeclaration_DeclarationWithoutMetricName_Fails() | ||
{ | ||
// Arrange | ||
var rawDeclaration = MetricsDeclarationBuilder.WithMetadata() | ||
.WithCosmosDbMetric(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 successful"); | ||
} | ||
|
||
[Fact] | ||
public void CosmosDbMetricsDeclaration_DeclarationWithoutDbName_Fails() | ||
{ | ||
// Arrange | ||
var rawDeclaration = MetricsDeclarationBuilder.WithMetadata() | ||
.WithCosmosDbMetric(dbName: 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 successful"); | ||
} | ||
|
||
[Fact] | ||
public void CosmosDbMetricsDeclaration_ValidDeclaration_Succeeds() | ||
{ | ||
// Arrange | ||
var rawMetricsDeclaration = MetricsDeclarationBuilder.WithMetadata() | ||
.WithCosmosDbMetric() | ||
.Build(); | ||
var metricsDeclarationProvider = new MetricsDeclarationProviderStub(rawMetricsDeclaration); | ||
|
||
// Act | ||
var scrapingScheduleValidationStep = new MetricsDeclarationValidationStep(metricsDeclarationProvider); | ||
var validationResult = scrapingScheduleValidationStep.Run(); | ||
|
||
// Assert | ||
Assert.True(validationResult.IsSuccessful, "Validation was not successful"); | ||
} | ||
} | ||
} |