-
-
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.
* Creating some of the files necessary for virtual machine scraper * Add virtual machine scraper, no tests yet * Adding tests * Refactoring tests and deserialization code * Add VirtualMachine to MetricValidatorFactory
- Loading branch information
1 parent
efffd81
commit 1f54551
Showing
13 changed files
with
308 additions
and
1 deletion.
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,27 @@ | ||
--- | ||
layout: default | ||
title: Azure Virtual Machine Declaration | ||
--- | ||
|
||
## Azure Virtual Machine | ||
You can declare to scrape an Azure Virtual Machine via the `VirtualMachine` resource type. | ||
|
||
The following fields need to be provided: | ||
- `virtualMachineName` - The name of the virtual machine | ||
|
||
All supported metrics are documented in the official [Azure Monitor documentation](https://docs.microsoft.com/en-us/azure/azure-monitor/platform/metrics-supported#microsoftcomputevirtualmachines). | ||
|
||
Example: | ||
```yaml | ||
name: demo_virtualmachine_percentage_cpu | ||
description: "Average percentage cpu usage of our 'promitor-virtual-machine' virtual machine" | ||
resourceType: VirtualMachine | ||
virtualMachineName: promitor-virtual-machine | ||
azureMetricConfiguration: | ||
metricName: Percentage CPU | ||
aggregation: | ||
type: Average | ||
``` | ||
[← back to metrics declarations](/configuration/metrics)<br /> | ||
[← back to introduction](/) |
8 changes: 8 additions & 0 deletions
8
...Core.Scraping/Configuration/Model/Metrics/ResourceTypes/VirtualMachineMetricDefinition.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 VirtualMachineMetricDefinition : MetricDefinition | ||
{ | ||
public string VirtualMachineName { get; set; } | ||
public override ResourceType ResourceType { get; } = ResourceType.VirtualMachine; | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
...re.Scraping/Configuration/Serialization/Deserializers/VirtualMachineMetricDeserializer.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,22 @@ | ||
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 VirtualMachineMetricDeserializer : GenericAzureMetricDeserializer | ||
{ | ||
/// <summary>Deserializes the specified Virtual Machine metric node from the YAML configuration file.</summary> | ||
/// <param name="metricNode">The metric node containing 'virtualMachineName' parameter pointing to an instance of a Virtual Machine</param> | ||
/// <returns>A new <see cref="MetricDefinition"/> object (strongly typed as a <see cref="VirtualMachineMetricDefinition"/>) </returns> | ||
internal override MetricDefinition Deserialize(YamlMappingNode metricNode) | ||
{ | ||
var metricDefinition = base.DeserializeMetricDefinition<VirtualMachineMetricDefinition>(metricNode); | ||
var virtualMachineName = metricNode.Children[new YamlScalarNode("virtualMachineName")]; | ||
|
||
metricDefinition.VirtualMachineName = virtualMachineName?.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/VirtualMachineScraper.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 | ||
{ | ||
internal class VirtualMachineScraper : Scraper<VirtualMachineMetricDefinition> | ||
{ | ||
private const string ResourceUriTemplate = "subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Compute/virtualMachines/{2}"; | ||
|
||
public VirtualMachineScraper(AzureMetadata azureMetadata, MetricDefaults metricDefaults, AzureMonitorClient azureMonitorClient, ILogger logger, IExceptionTracker exceptionTracker) | ||
: base(azureMetadata, metricDefaults, azureMonitorClient, logger, exceptionTracker) | ||
{ | ||
} | ||
|
||
protected override async Task<double> ScrapeResourceAsync(VirtualMachineMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) | ||
{ | ||
var resourceUri = string.Format(ResourceUriTemplate, AzureMetadata.SubscriptionId, AzureMetadata.ResourceGroupName, metricDefinition.VirtualMachineName); | ||
|
||
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
23 changes: 23 additions & 0 deletions
23
....Scraper.Host/Validation/MetricDefinitions/ResourceTypes/VirtualMachineMetricValidator.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 System.Collections.Generic; | ||
using GuardNet; | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes; | ||
|
||
namespace Promitor.Scraper.Host.Validation.MetricDefinitions.ResourceTypes | ||
{ | ||
internal class VirtualMachineMetricValidator : MetricValidator<VirtualMachineMetricDefinition> | ||
{ | ||
protected override IEnumerable<string> Validate(VirtualMachineMetricDefinition virtualMachineMetricDefinition) | ||
{ | ||
Guard.NotNull(virtualMachineMetricDefinition, nameof(virtualMachineMetricDefinition)); | ||
|
||
var errorMessages = new List<string>(); | ||
|
||
if (string.IsNullOrWhiteSpace(virtualMachineMetricDefinition.VirtualMachineName)) | ||
{ | ||
errorMessages.Add("No virtual machine name is configured"); | ||
} | ||
|
||
return errorMessages; | ||
} | ||
} | ||
} |
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
75 changes: 75 additions & 0 deletions
75
...lization/MetricsDeclaration/MetricsDeclarationWithVirtualMachineYamlSerializationTests.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,75 @@ | ||
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 MetricsDeclarationWithVirtualMachineYamlSerializationTests : YamlSerializationTests<VirtualMachineMetricDefinition> | ||
{ | ||
[Fact] | ||
public void YamlSerialization_SerializeAndDeserializeValidConfigForVirtualMachine_SucceedsWithIdenticalOutput() | ||
{ | ||
// Arrange | ||
var azureMetadata = GenerateBogusAzureMetadata(); | ||
var virtualMachineMetricDefinition = GenerateBogusVirtualMachineMetricDefinition(); | ||
var metricDefaults = GenerateBogusMetricDefaults(); | ||
var scrapingConfiguration = new Core.Scraping.Configuration.Model.MetricsDeclaration | ||
{ | ||
AzureMetadata = azureMetadata, | ||
MetricDefaults = metricDefaults, | ||
Metrics = new List<MetricDefinition> | ||
{ | ||
virtualMachineMetricDefinition | ||
} | ||
}; | ||
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, virtualMachineMetricDefinition); | ||
var deserializedVirtualMachineMetricDefinition = deserializedMetricDefinition as VirtualMachineMetricDefinition; | ||
AssertVirtualMachineMetricDefinition(deserializedVirtualMachineMetricDefinition, virtualMachineMetricDefinition, deserializedMetricDefinition); | ||
} | ||
|
||
private static void AssertVirtualMachineMetricDefinition(VirtualMachineMetricDefinition deserializedVirtualMachineMetricDefinition, VirtualMachineMetricDefinition virtualMachineMetricDefinition, MetricDefinition deserializedMetricDefinition) | ||
{ | ||
Assert.NotNull(deserializedVirtualMachineMetricDefinition); | ||
Assert.Equal(virtualMachineMetricDefinition.VirtualMachineName, deserializedVirtualMachineMetricDefinition.VirtualMachineName); | ||
Assert.NotNull(deserializedMetricDefinition.AzureMetricConfiguration); | ||
Assert.Equal(virtualMachineMetricDefinition.AzureMetricConfiguration.MetricName, deserializedMetricDefinition.AzureMetricConfiguration.MetricName); | ||
Assert.NotNull(deserializedMetricDefinition.AzureMetricConfiguration.Aggregation); | ||
Assert.Equal(virtualMachineMetricDefinition.AzureMetricConfiguration.Aggregation.Type, deserializedMetricDefinition.AzureMetricConfiguration.Aggregation.Type); | ||
Assert.Equal(virtualMachineMetricDefinition.AzureMetricConfiguration.Aggregation.Interval, deserializedMetricDefinition.AzureMetricConfiguration.Aggregation.Interval); | ||
} | ||
private VirtualMachineMetricDefinition GenerateBogusVirtualMachineMetricDefinition() | ||
{ | ||
var bogusAzureMetricConfiguration = GenerateBogusAzureMetricConfiguration(); | ||
var bogusGenerator = new Faker<VirtualMachineMetricDefinition>() | ||
.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.VirtualMachine) | ||
.RuleFor(metricDefinition => metricDefinition.VirtualMachineName, faker => faker.Name.LastName()) | ||
.RuleFor(metricDefinition => metricDefinition.AzureMetricConfiguration, faker => bogusAzureMetricConfiguration); | ||
|
||
return bogusGenerator.Generate(); | ||
} | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
.../Validation/Metrics/ResourceTypes/VirtualMachineMetricsDeclarationValidationStepsTests.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 VirtualMachineMetricsDeclarationValidationStepTests | ||
{ | ||
[Fact] | ||
public void VirtualMachineMetricsDeclaration_DeclarationWithoutAzureMetricName_Succeeds() | ||
{ | ||
// Arrange | ||
var rawDeclaration = MetricsDeclarationBuilder.WithMetadata() | ||
.WithVirtualMachineMetric(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 VirtualMachineMetricsDeclaration_DeclarationWithoutMetricDescription_Succeeded() | ||
{ | ||
// Arrange | ||
var rawDeclaration = MetricsDeclarationBuilder.WithMetadata() | ||
.WithVirtualMachineMetric(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 VirtualMachineMetricsDeclaration_DeclarationWithoutMetricName_Fails() | ||
{ | ||
// Arrange | ||
var rawDeclaration = MetricsDeclarationBuilder.WithMetadata() | ||
.WithVirtualMachineMetric(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 VirtualMachineMetricsDeclaration_DeclarationWithoutVirtualMachineName_Fails() | ||
{ | ||
// Arrange | ||
var rawDeclaration = MetricsDeclarationBuilder.WithMetadata() | ||
.WithVirtualMachineMetric(virtualMachineName: 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 VirtualMachineMetricsDeclaration_ValidDeclaration_Succeeds() | ||
{ | ||
// Arrange | ||
var rawMetricsDeclaration = MetricsDeclarationBuilder.WithMetadata() | ||
.WithVirtualMachineMetric() | ||
.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"); | ||
} | ||
} | ||
} |