Skip to content

Commit

Permalink
Virtual machine metrics (#454)
Browse files Browse the repository at this point in the history
* 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
Michelle Cone authored and tomkerkhove committed Mar 26, 2019
1 parent efffd81 commit 1f54551
Show file tree
Hide file tree
Showing 13 changed files with 308 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/configuration/metrics/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ We also provide a simplified way to configure the following Azure resources:
- [Azure Container Instances](container-instances)
- [Azure Service Bus Queue](service-bus-queue)
- [Azure Storage Queue](storage-queue)
- [Azure Virtual Machine](virtual-machine)

Want to help out? Create an issue and [contribute a new scraper](https://github.com/tomkerkhove/promitor/blob/master/adding-a-new-scraper.md).

Expand Down
27 changes: 27 additions & 0 deletions docs/configuration/metrics/virtual-machine.md
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
```
[&larr; back to metrics declarations](/configuration/metrics)<br />
[&larr; back to introduction](/)
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public enum ResourceType
ServiceBusQueue = 1,
Generic = 2,
StorageQueue = 3,
ContainerInstance = 4
ContainerInstance = 4,
VirtualMachine = 5
}
}
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ internal static GenericAzureMetricDeserializer GetDeserializerFor(Configuration.
return new StorageQueueMetricDeserializer();
case Configuration.Model.ResourceType.ContainerInstance:
return new ContainerInstanceMetricDeserializer();
case Configuration.Model.ResourceType.VirtualMachine:
return new VirtualMachineMetricDeserializer();
}

throw new ArgumentOutOfRangeException($@"Resource Type {resource} not supported.");
Expand Down
2 changes: 2 additions & 0 deletions src/Promitor.Core.Scraping/Factories/MetricScraperFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public static IScraper<MetricDefinition> CreateScraper(ResourceType metricDefini
return new StorageQueueScraper(azureMetadata, metricDefaults, azureMonitorClient, logger, exceptionTracker);
case ResourceType.ContainerInstance:
return new ContainerInstanceScraper(azureMetadata, metricDefaults, azureMonitorClient, logger, exceptionTracker);
case ResourceType.VirtualMachine:
return new VirtualMachineScraper(azureMetadata, metricDefaults, azureMonitorClient, logger, exceptionTracker);
default:
throw new ArgumentOutOfRangeException();
}
Expand Down
31 changes: 31 additions & 0 deletions src/Promitor.Core.Scraping/ResourceTypes/VirtualMachineScraper.cs
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ internal static IMetricValidator GetValidatorFor(ResourceType resourceType)
return new StorageQueueMetricValidator();
case ResourceType.ContainerInstance:
return new ContainerInstanceMetricValidator();
case ResourceType.VirtualMachine:
return new VirtualMachineMetricValidator();
}

throw new ArgumentOutOfRangeException(nameof(resourceType), $"No validation rules are defined for metric type '{resourceType}'");
Expand Down
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ public string Build()
return this;
}

public MetricsDeclarationBuilder WithVirtualMachineMetric(string metricName = "promitor-virtual-machine", string metricDescription = "Description for a metric", string virtualMachineName = "promitor-virtual-machine-name", string azureMetricName = "Total")
{
var azureMetricConfiguration = CreateAzureMetricConfiguration(azureMetricName);
var metric = new VirtualMachineMetricDefinition
{
Name = metricName,
Description = metricDescription,
VirtualMachineName = virtualMachineName,
AzureMetricConfiguration = azureMetricConfiguration
};

_metrics.Add(metric);

return this;
}

public MetricsDeclarationBuilder WithGenericMetric(string metricName = "foo", string metricDescription = "Description for a metric", string resourceUri = "Microsoft.ServiceBus/namespaces/promitor-messaging", string filter = "EntityName eq \'orders\'", string azureMetricName = "Total")
{
var azureMetricConfiguration = CreateAzureMetricConfiguration(azureMetricName);
Expand Down
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();
}
}
}
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");
}
}
}

0 comments on commit 1f54551

Please sign in to comment.