Commit 53a2437
authored
azuremonitorreceiver: discover and collect custom metric namespaces (#49035)
## Description
The `MetricDefinitions` API only returns custom metric namespaces (e.g.
`azure.vm.linux.guestmetrics` published by AMA/MetricsExtension) when
the `metricnamespace` query parameter is explicitly set. Without it,
only standard platform metrics are returned, so any metrics configured
under `receiver::metrics` that belong to a custom namespace were
silently dropped — the receiver never discovered or collected them.
### Root cause
`loadMetricsDefinitions` called
`MetricDefinitionsClient.NewListPager(resourceID, nil)` (nil = no
namespace filter). Azure Monitor returns definitions only for the
resource's default namespace (e.g. `Microsoft.Compute/virtualMachines`).
Custom namespaces like `azure.vm.linux.guestmetrics` require an explicit
`metricnamespace` query parameter.
Additionally, the `Metrics` (values) API also requires the
`metricnamespace` parameter when querying a non-default namespace, so
`newResourceMetricsValuesRequestOptions` needed the same fix. The batch
scraper's `QueryResources` call had the same issue: it was passing
`resourceType` as the namespace parameter instead of the actual metric
namespace from the composite key.
### Changes
- **`scraper.go`**: Add `namespace` field to `metricsCompositeKey` so
metrics from different namespaces are batched in separate `Metrics` API
calls (Azure Monitor accepts only one namespace per request). Refactor
`loadMetricsDefinitions` into a `collectMetricDefinitions` helper that
accepts optional `MetricDefinitionsClientListOptions`. After the default
(no-filter) call, make one additional call per namespace configured in
`receiver.metrics` that was not already discovered, using the explicit
`metricnamespace` parameter. Pass `compositeKey.namespace` to
`newResourceMetricsValuesRequestOptions`.
- **`scraper_batch.go`**: Apply the same two-pass fix. Extract the pager
loop into a `collectMetricDefinitionsByType` helper with
`discoveredNamespaces` tracking. After the default call, make one
additional namespace-filtered call per custom namespace in `cfg.Metrics`
not already returned. Pass `compositeKey.namespace` instead of
`resourceType` to `QueryResources` so the correct namespace is used in
the batch values API call.
- **`scraper_test.go`**: Add
`TestAzureScraperScrapeCustomNamespaceMetrics` covering the end-to-end
flow: a VM with no standard metrics but a custom namespace metric
(`azure.vm.linux.guestmetrics/disk/free_percent`) is fully discovered
and collected.
- **`scraper_batch_test.go`**: Update mock data to use the actual
namespace values from metric definitions (instead of the resource type
string). Add `TestAzureScraperBatchScrapeCustomNamespaceMetrics`
covering the same end-to-end flow for the batch scraper path.
- **`mocks_test.go`**: Fix the mock to return an empty success page
(instead of a zero `PagerResponder`) when no matching key exists. The
Azure SDK fake transport uses the URL path as the tracker key (ignoring
query params), so without this fix the empty `PagerResponder` from the
default call was reused for subsequent namespace-filtered calls,
preventing the mock from being invoked again.
- **`testdata/expected_metrics/metrics_custom_namespace.yaml`**: Golden
file for the new tests.
- **`.chloggen/40989-azuremonitor-custom-namespace-discovery.yaml`**:
Changelog entry for the bug fix.
- **`README.md`**: Add a note explaining custom namespace API behavior,
an example config for `azure.vm.linux.guestmetrics`, and a cardinality
note in the API calls section.
### Testing
```
go test ./receiver/azuremonitorreceiver/... -v -run TestAzureScraperScrape
```
All existing tests continue to pass. The new tests
`TestAzureScraperScrapeCustomNamespaceMetrics` and
`TestAzureScraperBatchScrapeCustomNamespaceMetrics` cover the custom
namespace discovery path for both scraper implementations.
### Related issues
This fix enables collection of metrics published by the Azure Monitor
Agent (AMA) / MetricsExtension to custom namespaces, which is necessary
for guest OS metrics (disk, memory, CPU from within the VM) that are not
available through standard Azure platform metrics.
Fixes:
#409891 parent 3f592ab commit 53a2437
8 files changed
Lines changed: 393 additions & 23 deletions
File tree
- .chloggen
- receiver/azuremonitorreceiver
- testdata/expected_metrics
Lines changed: 13 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
74 | 79 | | |
75 | 80 | | |
76 | 81 | | |
| |||
93 | 98 | | |
94 | 99 | | |
95 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
96 | 113 | | |
97 | 114 | | |
98 | 115 | | |
| |||
248 | 265 | | |
249 | 266 | | |
250 | 267 | | |
| 268 | + | |
| 269 | + | |
251 | 270 | | |
252 | 271 | | |
253 | 272 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
62 | 67 | | |
63 | | - | |
| 68 | + | |
64 | 69 | | |
65 | | - | |
66 | | - | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
67 | 84 | | |
68 | 85 | | |
69 | 86 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| 81 | + | |
81 | 82 | | |
82 | 83 | | |
83 | 84 | | |
| |||
483 | 484 | | |
484 | 485 | | |
485 | 486 | | |
486 | | - | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
487 | 490 | | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
488 | 527 | | |
489 | 528 | | |
490 | 529 | | |
| |||
505 | 544 | | |
506 | 545 | | |
507 | 546 | | |
508 | | - | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
509 | 554 | | |
510 | 555 | | |
511 | 556 | | |
512 | 557 | | |
513 | 558 | | |
514 | 559 | | |
515 | 560 | | |
| 561 | + | |
516 | 562 | | |
517 | 563 | | |
518 | 564 | | |
519 | 565 | | |
520 | 566 | | |
521 | 567 | | |
522 | 568 | | |
523 | | - | |
524 | | - | |
525 | | - | |
526 | | - | |
527 | | - | |
528 | | - | |
529 | 569 | | |
530 | 570 | | |
531 | 571 | | |
| |||
577 | 617 | | |
578 | 618 | | |
579 | 619 | | |
| 620 | + | |
580 | 621 | | |
581 | 622 | | |
582 | 623 | | |
| |||
638 | 679 | | |
639 | 680 | | |
640 | 681 | | |
| 682 | + | |
641 | 683 | | |
642 | 684 | | |
643 | 685 | | |
644 | 686 | | |
645 | | - | |
| 687 | + | |
646 | 688 | | |
647 | 689 | | |
648 | 690 | | |
649 | 691 | | |
650 | 692 | | |
651 | 693 | | |
652 | 694 | | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
653 | 699 | | |
654 | 700 | | |
655 | 701 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
442 | 442 | | |
443 | 443 | | |
444 | 444 | | |
445 | | - | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
446 | 483 | | |
447 | 484 | | |
448 | 485 | | |
| |||
462 | 499 | | |
463 | 500 | | |
464 | 501 | | |
465 | | - | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
466 | 509 | | |
467 | 510 | | |
468 | 511 | | |
469 | 512 | | |
470 | 513 | | |
471 | 514 | | |
472 | 515 | | |
| 516 | + | |
473 | 517 | | |
474 | 518 | | |
475 | 519 | | |
476 | 520 | | |
477 | 521 | | |
478 | 522 | | |
479 | 523 | | |
480 | | - | |
481 | | - | |
482 | | - | |
483 | | - | |
484 | | - | |
485 | 524 | | |
486 | 525 | | |
487 | 526 | | |
| |||
580 | 619 | | |
581 | 620 | | |
582 | 621 | | |
583 | | - | |
| 622 | + | |
584 | 623 | | |
585 | 624 | | |
586 | 625 | | |
| |||
0 commit comments