Skip to content

Commit

Permalink
feat(googlecloudmonitoring): support monitoring filters
Browse files Browse the repository at this point in the history
  • Loading branch information
chenlujjj committed Jan 17, 2025
1 parent 2a184ca commit 6fd50f2
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 10 deletions.
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.

## Authentication with Google Cloud

Expand Down
12 changes: 6 additions & 6 deletions receiver/googlecloudmonitoringreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type Config struct {
}

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

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

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

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

if metric.MetricName == "" {
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
5 changes: 2 additions & 3 deletions receiver/googlecloudmonitoringreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,12 @@ 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 =`

// see https://cloud.google.com/monitoring/api/v3/filters
if metric.MetricName != "" {
filterQuery = fmt.Sprintf(`%s "%s"`, baseQuery, metric.MetricName)
filterQuery = fmt.Sprintf(`metric.type = "%s"`, metric.MetricName)
} else {
filterQuery = fmt.Sprintf(`%s monitoring.regex.full_match("%s")`, baseQuery, metric.MetricName)
filterQuery = metric.MonitoringFilter
}

return filterQuery
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\")"

0 comments on commit 6fd50f2

Please sign in to comment.