Skip to content

Commit 1a864d6

Browse files
committed
receiver/azuremonitor - Added maximumResourcesPerBatch config
1 parent 4b283be commit 1a864d6

File tree

6 files changed

+66
-2
lines changed

6 files changed

+66
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: azuremonitorreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Added new config that sets the number of unique resource IDs to fetch per Batch API call"
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [40112]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

receiver/azuremonitorreceiver/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ The following settings are optional:
3838
- `dimensions.enabled` (default = `true`): allows to opt out from automatically split by all the dimensions of the resource type.
3939
- `dimensions.overrides` (default = `{}`): if dimensions are enabled, it allows you to specify a set of dimensions for a particular metric. This is a two levels map with first key being the resource type and second key being the metric name. Programmatic value should be used for metric name https://learn.microsoft.com/en-us/azure/azure-monitor/reference/metrics-index
4040
- `use_batch_api` (default = `false`): Use the batch API to fetch metrics. This is useful when the number of subscriptions is high and the API calls are rate limited.
41+
- `maximum_resources_per_batch` (default = 50): If batch is enabled, the maximum number of unique resource IDs to fetch per API call, current limit is 50 (as of 06/16/2025)
4142

4243
Authenticating using service principal requires following additional settings:
4344

@@ -92,7 +93,7 @@ Good news is that it's **very easy for you to try out!**
9293
```yaml
9394
receivers:
9495
azuremonitor:
95-
use_bath_api: true
96+
use_batch_api: true
9697
... # no change for other configs
9798
```
9899

receiver/azuremonitorreceiver/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var (
2828
errMissingClientSecret = errors.New(`"ClientSecret" is not specified in config`)
2929
errMissingFedTokenFile = errors.New(`"FederatedTokenFile" is not specified in config`)
3030
errInvalidCloud = errors.New(`"Cloud" is invalid`)
31+
errInvalidMaxResPerBatch = errors.New(`"MaximumResourcesPerBatch" is invalid`)
3132

3233
monitorServices = []string{
3334
"Microsoft.EventGrid/eventSubscriptions",
@@ -254,6 +255,7 @@ type Config struct {
254255
AppendTagsAsAttributes bool `mapstructure:"append_tags_as_attributes"`
255256
UseBatchAPI bool `mapstructure:"use_batch_api"`
256257
Dimensions DimensionsConfig `mapstructure:"dimensions"`
258+
MaximumResourcesPerBatch int `mapstructure:"maximum_resources_per_batch"`
257259

258260
// Authentication accepts the component azureauthextension,
259261
// and uses it to get an access token to make requests.
@@ -284,6 +286,8 @@ const (
284286
managedIdentity = "managed_identity"
285287
)
286288

289+
const defaultMaximumResourcesPerBatch = 50
290+
287291
// Validate validates the configuration by checking for missing or invalid fields
288292
func (c Config) Validate() (err error) {
289293
if len(c.SubscriptionIDs) == 0 && !c.DiscoverSubscriptions {
@@ -329,5 +333,9 @@ func (c Config) Validate() (err error) {
329333
err = multierr.Append(err, errInvalidCloud)
330334
}
331335

336+
if c.UseBatchAPI && c.MaximumResourcesPerBatch != 0 && (c.MaximumResourcesPerBatch > defaultMaximumResourcesPerBatch || c.MaximumResourcesPerBatch < 0) {
337+
err = multierr.Append(err, errInvalidMaxResPerBatch)
338+
}
339+
332340
return
333341
}

receiver/azuremonitorreceiver/config_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ func TestLoadConfig(t *testing.T) {
109109
return cfg
110110
}(),
111111
},
112+
{
113+
id: component.NewIDWithName(metadata.Type, "max_resources_per_batch"),
114+
expectedErr: errInvalidMaxResPerBatch.Error(),
115+
},
116+
{
117+
id: component.NewIDWithName(metadata.Type, "max_resources_per_batch_negative_value"),
118+
expectedErr: errInvalidMaxResPerBatch.Error(),
119+
},
112120
}
113121

114122
for _, tt := range tests {

receiver/azuremonitorreceiver/scraper_batch.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,10 @@ func (s *azureBatchScraper) storeMetricsDefinitionByType(subscriptionID string,
396396

397397
func (s *azureBatchScraper) getBatchMetricsValues(ctx context.Context, subscriptionID, resourceType string) {
398398
resType := *s.resourceTypes[subscriptionID][resourceType]
399+
maxPerBatch := defaultMaximumResourcesPerBatch
400+
if s.cfg.MaximumResourcesPerBatch > 0 {
401+
maxPerBatch = s.cfg.MaximumResourcesPerBatch
402+
}
399403

400404
for compositeKey, metricsByGrain := range resType.metricsByCompositeKey {
401405
now := time.Now().UTC()
@@ -419,7 +423,7 @@ func (s *azureBatchScraper) getBatchMetricsValues(ctx context.Context, subscript
419423

420424
startResources := 0
421425
for startResources < len(resType.resourceIDs) {
422-
endResources := startResources + 50 // getBatch API is limited to 50 resources max
426+
endResources := startResources + maxPerBatch
423427
if endResources > len(resType.resourceIDs) {
424428
endResources = len(resType.resourceIDs)
425429
}

receiver/azuremonitorreceiver/testdata/config.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,19 @@ azuremonitor/valid_authenticator_2:
4040
auth:
4141
authenticator: azureauth
4242
credentials: does-not-matter
43+
44+
azuremonitor/max_resources_per_batch:
45+
discover_subscriptions: true
46+
auth:
47+
authenticator: azureauth
48+
credentials: does-not-matter
49+
use_batch_api: true
50+
maximum_resources_per_batch: 51
51+
52+
azuremonitor/max_resources_per_batch_negative_value:
53+
discover_subscriptions: true
54+
auth:
55+
authenticator: azureauth
56+
credentials: does-not-matter
57+
use_batch_api: true
58+
maximum_resources_per_batch: -1

0 commit comments

Comments
 (0)