Skip to content

Add Monitor Metrics Batchquery command - #3393

Open
msalaman wants to merge 2 commits into
mainfrom
masalama/queryMetricsMultipleResources
Open

Add Monitor Metrics Batchquery command#3393
msalaman wants to merge 2 commits into
mainfrom
masalama/queryMetricsMultipleResources

Conversation

@msalaman

Copy link
Copy Markdown
Contributor

What does this PR do?

Add Monitor Metrics Batchquery command to support querying for multiple resources

GitHub issue number?

#1464

Pre-merge Checklist

  • Required for All PRs
    • Read contribution guidelines
    • PR title clearly describes the change
    • Commit history is clean with descriptive messages (cleanup guide)
    • Added comprehensive tests for new/modified functionality
    • Created a changelog entry if the change falls among the following: new feature, bug fix, UI/UX update, breaking change, or updated dependencies. Follow the changelog entry guide
  • For MCP tool changes:
    • One tool per PR: This PR adds or modifies only one MCP tool for faster review cycles
    • Updated servers/Azure.Mcp.Server/README.md and/or servers/Fabric.Mcp.Server/README.md documentation
    • Validate README.md changes running the script ./eng/scripts/Process-PackageReadMe.ps1. See Package README
    • For new or modified tool descriptions, ran ToolDescriptionEvaluator and obtained a score of 0.4 or more and a top 3 ranking for all related test prompts
    • For tools with new names, including new tools or renamed tools, update consolidated-tools.json
    • For renamed tools, follow the Tool Rename Checklist and tag the PR with the breaking-change label
    • For new tools associated with Azure services or publicly available tools/APIs/products, add URL to documentation in the PR description
  • Extra steps for Azure MCP Server tool changes:
    • Updated command list in servers/Azure.Mcp.Server/docs/azmcp-commands.md
    • Ran ./eng/scripts/Update-AzCommandsMetadata.ps1 to update tool metadata in azmcp-commands.md (required for CI)
    • Updated test prompts in servers/Azure.Mcp.Server/docs/e2eTestPrompts.md
    • 👉 For Community (non-Microsoft team member) PRs:
      • Security review: Reviewed code for security vulnerabilities, malicious code, or suspicious activities before running tests (crypto mining, spam, data exfiltration, etc.)
      • Manual tests run: added comment /azp run mcp - pullrequest - live to run Live Test Pipeline

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new Azure Monitor Metrics batch query command (azmcp monitor metrics batchquery) to query metrics across multiple resources in a single request, wiring it into the Monitor toolset, serialization, docs, and tests.

Changes:

  • Introduces MetricsBatchQueryCommand with new options and result model for multi-resource metrics queries.
  • Extends IMonitorMetricsService / MonitorMetricsService with a QueryMetricsBatchAsync implementation backed by Azure.Monitor.Query.Metrics.
  • Updates Azure MCP Server docs (command reference, e2e prompts) and adds a changelog entry.
Show a summary per file
File Description
tools/Azure.Mcp.Tools.Monitor/tests/Azure.Mcp.Tools.Monitor.Tests/Metrics/MetricsBatchQueryCommandTests.cs Adds unit tests for option binding, validation, and bucket-limit handling for the new command.
tools/Azure.Mcp.Tools.Monitor/src/Services/MonitorMetricsService.cs Implements multi-resource metrics querying via MetricsClient.QueryResourcesAsync.
tools/Azure.Mcp.Tools.Monitor/src/Services/IMonitorMetricsService.cs Adds service interface contract for batch metrics querying.
tools/Azure.Mcp.Tools.Monitor/src/Options/Metrics/MetricsBatchQueryOptions.cs Introduces CLI options surface for batchquery.
tools/Azure.Mcp.Tools.Monitor/src/MonitorSetup.cs Registers the new command in DI and the monitor metrics command group.
tools/Azure.Mcp.Tools.Monitor/src/Models/ResourceMetricsResult.cs Adds a per-resource result wrapper for batch query responses.
tools/Azure.Mcp.Tools.Monitor/src/Commands/MonitorJsonContext.cs Registers new result types for source-generated System.Text.Json (AOT).
tools/Azure.Mcp.Tools.Monitor/src/Commands/Metrics/MetricsBatchQueryCommand.cs Adds the new tool command, validation, defaults, execution, and response shaping.
tools/Azure.Mcp.Tools.Monitor/src/Azure.Mcp.Tools.Monitor.csproj Adds package reference for Azure.Monitor.Query.Metrics.
servers/Azure.Mcp.Server/docs/e2eTestPrompts.md Adds e2e prompts for monitor_metrics_batchquery.
servers/Azure.Mcp.Server/docs/azmcp-commands.md Documents the new azmcp monitor metrics batchquery command usage and example.
servers/Azure.Mcp.Server/changelog-entries/monitor-metrics-batchquery.yml Adds changelog entry for the new command.
Directory.Packages.props Centrally pins Azure.Monitor.Query.Metrics version.

Review details

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

  • Files reviewed: 13/13 changed files
  • Comments generated: 4
  • Review effort level: Lite

Comment on lines +365 to +371
// Resolve each resource name (or already-valid resource ID) to a full resource identifier.
var resourceIds = new List<ResourceIdentifier>(resourceNames.Count);
foreach (var resourceName in resourceNames)
{
var resourceId = await _resourceResolverService.ResolveResourceIdAsync(subscription, resourceGroup, resourceType, resourceName, tenant, retryPolicy, cancellationToken);
resourceIds.Add(resourceId);
}
Comment on lines 154 to 156
metrics.AddCommand<MetricsQueryCommand>(serviceProvider);
metrics.AddCommand<MetricsBatchQueryCommand>(serviceProvider);
metrics.AddCommand<MetricsDefinitionsCommand>(serviceProvider);
Comment on lines +19 to +24
[CommandMetadata(
Id = "6c1b0f5f-04c1-4b2e-8f0b-0d6f4f7cba2e",
Name = "batchquery",
Title = "Query Azure Monitor Metrics for Multiple Resources",
Description = "Query Azure Monitor metrics for multiple resources in a single batch request. Returns time series data for the specified metrics, grouped by resource. All resources must belong to the same subscription, Azure region, and resource type.",
Destructive = false,
Comment on lines +75 to +82
public override async Task<CommandResponse> ExecuteAsync(CommandContext context, MetricsBatchQueryOptions options, CancellationToken cancellationToken)
{
try
{
string[] resources = [.. options.Resources.Split(',').Select(t => t.Trim())];
string[] metricNames = [.. options.MetricNames.Split(',').Select(t => t.Trim())];

var results = await _metricsService.QueryMetricsBatchAsync(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants