Skip to content

Commit

Permalink
Support for Azure Cache for Redis resource type (#595)
Browse files Browse the repository at this point in the history
* add resource and scraper for Redis Cache

* fix tab issue in resource types

* add deserializer for Cache for Redis resource type

* add unit test for Cache for Redis serialization

* add validators and tests for Cache for Redis resource type

* add cache for redis scraper to scrapers factory

* rename CacheForRedis resource type to RedisCache

* documentation for Redis Cache

* few updates according requested changes
  • Loading branch information
jcorioland authored and tomkerkhove committed Jun 18, 2019
1 parent e1289b5 commit cdeeb08
Show file tree
Hide file tree
Showing 14 changed files with 345 additions and 65 deletions.
1 change: 1 addition & 0 deletions docs/configuration/metrics/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ metrics:
We also provide a simplified way to scrape the following Azure resources:
- [Azure Cache for Redis](redis-cache)
- [Azure Container Instances](container-instances)
- [Azure Container Registry](container-registry)
- [Azure Cosmos DB](cosmos-db)
Expand Down
32 changes: 32 additions & 0 deletions docs/configuration/metrics/redis-cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
layout: default
title: Azure Cache for Redis Declaration
---

## Azure Cache for Redis - ![Availability Badge](https://img.shields.io/badge/Available%20Starting-v1.0.0-green.svg)
You can declare to scrape an Azure Cache for Redis via the `RedisCache` resource type.

The following fields need to be provided:
- `cacheName` - The name of the Redis Cache instance

All supported metrics are documented in the official [Azure Monitor documentation](https://docs.microsoft.com/en-us/azure/azure-monitor/platform/metrics-supported#microsoftcacheredis).

You can find more documentation on each metric in the Azure Cache for Redis [monitoring documentation](https://docs.microsoft.com/en-us/azure/azure-cache-for-redis/cache-how-to-monitor#available-metrics-and-reporting-intervals).

Example:
```yaml
name: redis_cache_hits
description: "The number of successful key lookups during the specified reporting interval. This maps to keyspace_hits from the Redis INFO command."
resourceType: RedisCache
cacheName: Promitor
scraping:
schedule: "0 */2 * ? * *"
azureMetricConfiguration:
metricName: CacheHits
aggregation:
type: Total
interval: 00:01:00
```
[&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 RedisCacheMetricDefinition : MetricDefinition
{
public string CacheName { get; set; }
public override ResourceType ResourceType { get; } = ResourceType.RedisCache;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public enum ResourceType
ContainerInstance = 4,
VirtualMachine = 5,
ContainerRegistry = 6,
NetworkInterface = 7,
CosmosDb = 8
NetworkInterface = 7,
CosmosDb = 8,
RedisCache = 9,
}
}
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 Redis Cache resource type
/// </summary>
internal class RedisCacheMetricDeserializer : GenericAzureMetricDeserializer
{
/// <summary>
/// Deserializes the specified Redis Cache metric node from the YAML configuration file.
/// </summary>
/// <param name="metricNode">The metric node to deserialize to Redis Cache configuration</param>
/// <returns>A new <see cref="MetricDefinition"/> object (strongly typed as a <see cref="RedisCacheMetricDefinition"/>) </returns>
internal override MetricDefinition Deserialize(YamlMappingNode metricNode)
{
var metricDefinition = base.DeserializeMetricDefinition<RedisCacheMetricDefinition>(metricNode);

var cacheName = metricNode.Children[new YamlScalarNode("cacheName")];
metricDefinition.CacheName = cacheName?.ToString();

return metricDefinition;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ internal static GenericAzureMetricDeserializer GetDeserializerFor(Configuration.
return new NetworkInterfaceMetricDeserializer();
case Configuration.Model.ResourceType.CosmosDb:
return new CosmosDbMetricDeserializer();
case Configuration.Model.ResourceType.RedisCache:
return new RedisCacheMetricDeserializer();
}

throw new ArgumentOutOfRangeException($@"Resource Type {resource} not supported.");
Expand Down
4 changes: 3 additions & 1 deletion src/Promitor.Core.Scraping/Factories/MetricScraperFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public static IScraper<MetricDefinition> CreateScraper(ResourceType metricDefini
case ResourceType.ContainerRegistry:
return new ContainerRegistryScraper(azureMetadata, azureMonitorClient, logger, exceptionTracker);
case ResourceType.CosmosDb:
return new CosmosDbScraper(azureMetadata, azureMonitorClient, logger, exceptionTracker);
return new CosmosDbScraper(azureMetadata, azureMonitorClient, logger, exceptionTracker);
case ResourceType.RedisCache:
return new RedisCacheScraper(azureMetadata, azureMonitorClient, logger, exceptionTracker);
default:
throw new ArgumentOutOfRangeException();
}
Expand Down
31 changes: 31 additions & 0 deletions src/Promitor.Core.Scraping/ResourceTypes/RedisCacheScraper.cs
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 RedisCacheScraper : Scraper<RedisCacheMetricDefinition>
{
private const string ResourceUriTemplate = "subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Cache/Redis/{2}";

public RedisCacheScraper(AzureMetadata azureMetadata, AzureMonitorClient azureMonitorClient, ILogger logger, IExceptionTracker exceptionTracker)
: base(azureMetadata, azureMonitorClient, logger, exceptionTracker)
{
}

protected override async Task<ScrapeResult> ScrapeResourceAsync(string subscriptionId, string resourceGroupName, RedisCacheMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval)
{
var resourceUri = string.Format(ResourceUriTemplate, subscriptionId, resourceGroupName, metricDefinition.CacheName);

var metricName = metricDefinition.AzureMetricConfiguration.MetricName;
var foundMetricValue = await AzureMonitorClient.QueryMetricAsync(metricName, aggregationType, aggregationInterval, resourceUri);

return new ScrapeResult(resourceUri, foundMetricValue);
}
}
}
122 changes: 61 additions & 61 deletions src/Promitor.Scraper.Host/Docs/Open-Api.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ internal static IMetricValidator GetValidatorFor(ResourceType resourceType)
case ResourceType.ContainerRegistry:
return new ContainerRegistryMetricValidator();
case ResourceType.CosmosDb:
return new CosmosDbMetricValidator();
return new CosmosDbMetricValidator();
case ResourceType.RedisCache:
return new RedisCacheMetricValidator();
}

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,19 @@
using GuardNet;
using Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes;
using System.Collections.Generic;

namespace Promitor.Scraper.Host.Validation.MetricDefinitions.ResourceTypes
{
internal class RedisCacheMetricValidator : MetricValidator<RedisCacheMetricDefinition>
{
protected override IEnumerable<string> Validate(RedisCacheMetricDefinition redisCacheMetricDefinition)
{
Guard.NotNull(redisCacheMetricDefinition, nameof(redisCacheMetricDefinition));

if (string.IsNullOrWhiteSpace(redisCacheMetricDefinition.CacheName))
{
yield return "No cache name is configured";
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,20 @@ private AzureMetricConfiguration CreateAzureMetricConfiguration(string azureMetr
}
};
}

public MetricsDeclarationBuilder WithRedisCacheMetric(string metricName = "promitor-redis", string metricDescription = "Description for a metric", string cacheName = "promitor-redis", string azureMetricName = "CacheHits")
{
var azureMetricConfiguration = CreateAzureMetricConfiguration(azureMetricName);
var metric = new RedisCacheMetricDefinition
{
Name = metricName,
Description = metricDescription,
CacheName = cacheName,
AzureMetricConfiguration = azureMetricConfiguration
};
_metrics.Add(metric);

return this;
}
}
}
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 MetricsDeclarationWithRedisCacheYamlSerializationTests : YamlSerializationTests<RedisCacheMetricDefinition>
{
[Theory]
[InlineData("promitor1", @"* */1 * * * *", @"* */2 * * * *")]
[InlineData(null, null, null)]
public void YamlSerialization_SerializeAndDeserializeConfigForRedisCache_SucceedsWithIdenticalOutput(string resourceGroupName, string defaultScrapingInterval, string metricScrapingInterval)
{
// Arrange
var azureMetadata = GenerateBogusAzureMetadata();
var redisCacheMetricDefinition = GenerateBogusRedisCacheMetricDefinition(resourceGroupName, metricScrapingInterval);
var metricDefaults = GenerateBogusMetricDefaults(defaultScrapingInterval);
var scrapingConfiguration = new Core.Scraping.Configuration.Model.MetricsDeclaration()
{
AzureMetadata = azureMetadata,
MetricDefaults = metricDefaults,
Metrics = new List<MetricDefinition>()
{
redisCacheMetricDefinition
}
};
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, redisCacheMetricDefinition);
var deserializedRedisCacheMetricDefinition = deserializedMetricDefinition as RedisCacheMetricDefinition;
AssertRedisCacheMetricDefinition(deserializedRedisCacheMetricDefinition, redisCacheMetricDefinition);
}

private static void AssertRedisCacheMetricDefinition(RedisCacheMetricDefinition deserializedRedisCacheMetricDefinition, RedisCacheMetricDefinition redisCacheMetricDefinition)
{
Assert.NotNull(deserializedRedisCacheMetricDefinition);
Assert.Equal(redisCacheMetricDefinition.CacheName, deserializedRedisCacheMetricDefinition.CacheName);
}

private RedisCacheMetricDefinition GenerateBogusRedisCacheMetricDefinition(string resourceGroupName, string metricScrapingInterval)
{
var bogusScrapingInterval = GenerateBogusScrapingInterval(metricScrapingInterval);
var bogusAzureMetricConfiguration = GenerateBogusAzureMetricConfiguration();

var bogusGenerator = new Faker<RedisCacheMetricDefinition>()
.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.RedisCache)
.RuleFor(metricDefinition => metricDefinition.CacheName, 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();
}
}
}
Loading

0 comments on commit cdeeb08

Please sign in to comment.