-
-
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 Cache for Redis resource type (#595)
* 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
1 parent
e1289b5
commit cdeeb08
Showing
14 changed files
with
345 additions
and
65 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,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 | ||
``` | ||
[← back to metrics declarations](/configuration/metrics)<br /> | ||
[← back to introduction](/) |
8 changes: 8 additions & 0 deletions
8
...tor.Core.Scraping/Configuration/Model/Metrics/ResourceTypes/RedisCacheMetricDefinition.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 RedisCacheMetricDefinition : MetricDefinition | ||
{ | ||
public string CacheName { get; set; } | ||
public override ResourceType ResourceType { get; } = ResourceType.RedisCache; | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
...r.Core.Scraping/Configuration/Serialization/Deserializers/RedisCacheMetricDeserializer.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 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; | ||
} | ||
} | ||
} |
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/RedisCacheScraper.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 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); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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/RedisCacheMetricValidator.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 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"; | ||
} | ||
} | ||
} | ||
} |
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/MetricsDeclarationWithRedisCacheYamlSerializationTests.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 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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.