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 1 commit
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
11 changes: 10 additions & 1 deletion receiver/googlecloudmonitoringreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ type Config struct {
}

type MetricConfig struct {
MetricName string `mapstructure:"metric_name"`
MetricName string `mapstructure:"metric_name"`
MetricNameRegex string `mapstructure:"metric_name_regex"`
dashpole marked this conversation as resolved.
Show resolved Hide resolved
}

func (config *Config) Validate() error {
Expand All @@ -46,6 +47,14 @@ 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.MetricNameRegex == "" {
return errors.New("fields \"metric_name\" and \"metric_name_regex\" cannot both be empty")
}

if metric.MetricName == "" {
return errors.New("field \"metric_name\" is required and cannot be empty for metric configuration")
}
Expand Down
26 changes: 12 additions & 14 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 @@ -243,8 +236,13 @@ 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(`%s "%s"`, baseQuery, metric.MetricName)
} else {
filterQuery = fmt.Sprintf(`%s monitoring.regex.full_match("%s")`, baseQuery, metric.MetricName)
}

return filterQuery
}

Expand Down Expand Up @@ -322,7 +320,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
Loading