Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(receiver/azuremonitor): Adds filtering by metric and/or aggregation #37421

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .chloggen/feat_azuremonitorreceiver_filter_metrics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: azuremonitorreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Adds filtering by metric and/or aggregation

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [37420]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]

21 changes: 21 additions & 0 deletions receiver/azuremonitorreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The following settings are optional:
- `auth` (default = service_principal): Specifies the used authentication method. Supported values are `service_principal`, `workload_identity`, `managed_identity`, `default_credentials`.
- `resource_groups` (default = none): Filter metrics for specific resource groups, not setting a value will scrape metrics for all resources in the subscription.
- `services` (default = none): Filter metrics for specific services, not setting a value will scrape metrics for all services integrated with Azure Monitor.
- `metrics` (default = none): Filter specific metrics (e.g., `metrics: ["MetricName"]`) and aggregations (e.g., `metrics: ["MetricName/Total"]`).
- `cache_resources` (default = 86400): List of resources will be cached for the provided amount of time in seconds.
- `cache_resources_definitions` (default = 86400): List of metrics definitions will be cached for the provided amount of time in seconds.
- `maximum_number_of_metrics_in_a_call` (default = 20): Maximum number of metrics to fetch in per API call, current limit in Azure is 20 (as of 03/27/2023).
Expand All @@ -48,6 +49,10 @@ Authenticating using managed identities has the following optional settings:

- `client_id`

### Filtering metrics

The `metrics` configuration setting is designed to constrain scraping to particular metrics and their specific aggregations. It accepts an array of metrics, where each metric can optionally include an aggregation method following a `/` (slash): `MetricName/Aggregation`. The metric name should correspond to a supported Azure Monitor metric for the designated resource groups and services. The aggregation method can be any aggregation compatible with Azure Monitor (e.g., Average, Minimum, Maximum, Total, Count). The case of the metric name and aggregation does not affect the functionality.

### Example Configurations

Using [Service Principal](https://learn.microsoft.com/en-us/azure/developer/go/azure-sdk-authentication?tabs=bash#service-principal-with-a-secret) for authentication:
Expand All @@ -66,6 +71,10 @@ receivers:
services:
- "${service1}"
- "${service2}"
metrics:
- "${metric1}"
- "${metric2}/${aggregator1}"
- "${metric2}/${aggregator2}"
collection_interval: 60s
initial_delay: 1s
```
Expand Down Expand Up @@ -101,6 +110,18 @@ receivers:
auth: "default_credentials"
```

Scrapping limited metrics and aggregations:

```yaml
receivers:
azuremonitor:
subscription_id: "${subscription_id}"
auth: "default_credentials"
metrics:
- metric1 # This will scrape all known aggregations for "metric1"
- metric2/total # This will include "metric2" with the "Total" aggregation in the scraping
- metric3/average # This will include "metric3" with the "Average" aggregation in the scraping
```

## Metrics

Expand Down
1 change: 1 addition & 0 deletions receiver/azuremonitorreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ type Config struct {
FederatedTokenFile string `mapstructure:"federated_token_file"`
ResourceGroups []string `mapstructure:"resource_groups"`
Services []string `mapstructure:"services"`
Metrics []string `mapstructure:"metrics"`
CacheResources float64 `mapstructure:"cache_resources"`
CacheResourcesDefinitions float64 `mapstructure:"cache_resources_definitions"`
MaximumNumberOfMetricsInACall int `mapstructure:"maximum_number_of_metrics_in_a_call"`
Expand Down
42 changes: 37 additions & 5 deletions receiver/azuremonitorreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ type azureResource struct {
}

type metricsCompositeKey struct {
dimensions string // comma separated sorted dimensions
timeGrain string
dimensions string // comma separated sorted dimensions
aggregations string // comma separated sorted aggregations
timeGrain string
}

type azureResourceMetrics struct {
Expand Down Expand Up @@ -337,9 +338,17 @@ func (s *azureScraper) getResourceMetricsDefinitions(ctx context.Context, resour
}

for _, v := range nextResult.Value {
timeGrain := *v.MetricAvailabilities[0].TimeGrain
name := *v.Name.Value
compositeKey := metricsCompositeKey{timeGrain: timeGrain}
metricAggregations := getMetricAggregations(name, s.cfg.Metrics)
if len(metricAggregations) == 0 {
continue
}

timeGrain := *v.MetricAvailabilities[0].TimeGrain
compositeKey := metricsCompositeKey{
timeGrain: timeGrain,
aggregations: strings.Join(metricAggregations, ","),
}

if len(v.Dimensions) > 0 {
var dimensionsSlice []string
Expand Down Expand Up @@ -388,6 +397,7 @@ func (s *azureScraper) getResourceMetricsValues(ctx context.Context, resourceID
metricsByGrain.metrics,
compositeKey.dimensions,
compositeKey.timeGrain,
compositeKey.aggregations,
start,
end,
s.cfg.MaximumNumberOfRecordsPerResource,
Expand Down Expand Up @@ -435,6 +445,7 @@ func getResourceMetricsValuesRequestOptions(
metrics []string,
dimensionsStr string,
timeGrain string,
aggregations string,
start int,
end int,
top int32,
Expand All @@ -444,7 +455,7 @@ func getResourceMetricsValuesRequestOptions(
Metricnames: &resType,
Interval: to.Ptr(timeGrain),
Timespan: to.Ptr(timeGrain),
Aggregation: to.Ptr(strings.Join(aggregations, ",")),
Aggregation: to.Ptr(aggregations),
Top: to.Ptr(top),
}

Expand Down Expand Up @@ -500,3 +511,24 @@ func (s *azureScraper) processTimeseriesData(
}
}
}

func getMetricAggregations(name string, filters []string) []string {
if len(filters) == 0 {
return aggregations
}

out := []string{}
for _, filter := range filters {
if strings.EqualFold(name, filter) {
return aggregations
}

for _, aggregation := range aggregations {
if strings.EqualFold(name+"/"+aggregation, filter) {
out = append(out, aggregation)
}
}
}

return out
}
Loading
Loading