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(googlecloudmonitoring): support monitoring filters #37264

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
27 changes: 27 additions & 0 deletions .chloggen/feat-googlecloudmonitoring-filter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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: googlecloudmonitoringreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: support use [monitoring filters](https://cloud.google.com/monitoring/api/v3/filters) to filter metrics

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

# (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: []
4 changes: 3 additions & 1 deletion receiver/googlecloudmonitoringreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ receivers:

Each single metric can have the following configuration:

- `metric_name` (Required): The specific metric name to collect.
- `metric_name` (Optional): The specific metric name to collect.
- `monitoring_filter` (Optional): The [monitoring filter](https://cloud.google.com/monitoring/api/v3/filters) to filter metrics.

One of `metric_name` and `monitoring_filter` MUST be specified, but should not be specified at the same time.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
One of `metric_name` and `monitoring_filter` MUST be specified, but should not be specified at the same time.
One of `metric_name` and `monitoring_filter` MUST be specified, but MUST not be specified at the same time.

Copy link
Contributor

Choose a reason for hiding this comment

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

Based on https://cloud.google.com/monitoring/api/v3/filters, if we are applying these to only to ListMetricDescriptor, there are a lot of restrictions on what you can filter by that might confuse users. They can't use resource or group selectors, and presumably can't filter on labels: metric.labels.instance_name = monitoring.regex.full_match("gke-(hipster|nginx).*").

One solution would be to make this a metric_descriptor_filter config option, so that it is clear we are filtering metric descriptors (and we should also document that there are restrictions on that filter). That way we could later add a separate time_series_filter that we add to actual ListTimeSeries queries if we want.


## Authentication with Google Cloud

Expand Down
11 changes: 10 additions & 1 deletion receiver/googlecloudmonitoringreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ type Config struct {
}

type MetricConfig struct {
MetricName string `mapstructure:"metric_name"`
MetricName string `mapstructure:"metric_name"`
MonitoringFilter string `mapstructure:"monitoring_filter"`
}

func (config *Config) Validate() error {
Expand All @@ -47,6 +48,14 @@ func (config *Config) Validate() error {
}

func (metric MetricConfig) Validate() error {
if metric.MetricName != "" && metric.MonitoringFilter != "" {
return errors.New("fields \"metric_name\" and \"monitoring_filter\" cannot both have value")
}

if metric.MetricName == "" && metric.MonitoringFilter == "" {
return errors.New("fields \"metric_name\" and \"monitoring_filter\" cannot both be empty")
}

if metric.MetricName == "" {
return errors.New("field \"metric_name\" is required and cannot be empty for metric configuration")
}
Expand Down
3 changes: 3 additions & 0 deletions receiver/googlecloudmonitoringreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func TestLoadConfig(t *testing.T) {
{
MetricName: "connectors.googleapis.com/flex/instance/cpu/usage_time",
},
{
MonitoringFilter: "metric.type = starts_with(\"compute.googleapis.com\")",
},
},
},
cfg,
Expand Down
27 changes: 12 additions & 15 deletions receiver/googlecloudmonitoringreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,9 @@ func (mr *monitoringReceiver) Scrape(ctx context.Context) (pmetric.Metrics, erro
metrics := pmetric.NewMetrics()

// Iterate over each metric in the configuration to calculate start/end times and construct the filter query.
for _, metric := range mr.config.MetricsList {
// Acquire read lock to safely read metricDescriptors
mr.mutex.RLock()
metricDesc, exists := mr.metricDescriptors[metric.MetricName]
mr.mutex.RUnlock()
if !exists {
mr.logger.Warn("Metric descriptor not found", zap.String("metric_name", metric.MetricName))
continue
}

mr.mutex.RLock()
defer mr.mutex.RUnlock()
for metricType, metricDesc := range mr.metricDescriptors {
// Set interval and delay times, using defaults if not provided
gInternal = mr.config.CollectionInterval
if gInternal <= 0 {
Expand All @@ -114,7 +107,7 @@ func (mr *monitoringReceiver) Scrape(ctx context.Context) (pmetric.Metrics, erro
calStartTime, calEndTime = calculateStartEndTime(gInternal, gDelay)

// Get the filter query for the metric
filterQuery = getFilterQuery(metric)
filterQuery = fmt.Sprintf(`metric.type = "%s"`, metricType)

// Define the request to list time series data
tsReq := &monitoringpb.ListTimeSeriesRequest{
Expand Down Expand Up @@ -241,10 +234,14 @@ func calculateStartEndTime(interval, delay time.Duration) (time.Time, time.Time)
// getFilterQuery constructs a filter query string based on the provided metric.
func getFilterQuery(metric MetricConfig) string {
var filterQuery string
const baseQuery = `metric.type =`

// If a specific metric name is provided, use it in the filter query
filterQuery = fmt.Sprintf(`%s "%s"`, baseQuery, metric.MetricName)
// see https://cloud.google.com/monitoring/api/v3/filters
if metric.MetricName != "" {
filterQuery = fmt.Sprintf(`metric.type = "%s"`, metric.MetricName)
} else {
filterQuery = metric.MonitoringFilter
}

return filterQuery
}

Expand Down Expand Up @@ -322,7 +319,7 @@ func (mr *monitoringReceiver) convertGCPTimeSeriesToMetrics(metrics pmetric.Metr
// TODO: Add support for EXPONENTIAL_HISTOGRAM
default:
metricError := fmt.Sprintf("\n Unsupported metric kind: %v\n", timeSeries.GetMetricKind())
mr.logger.Info(metricError)
mr.logger.Warn(metricError)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ googlecloudmonitoring:
metrics_list:
- metric_name: "compute.googleapis.com/instance/cpu/usage_time"
- metric_name: "connectors.googleapis.com/flex/instance/cpu/usage_time"
- monitoring_filter: "metric.type = starts_with(\"compute.googleapis.com\")"
Loading