Skip to content

Commit

Permalink
feat(googlecloudmonitoring): filter metric via regex
Browse files Browse the repository at this point in the history
  • Loading branch information
chenlujjj committed Jan 16, 2025
1 parent 0788185 commit 1cc5e3f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
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"`
}

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)
if metric.MetricName != "" {
// If a specific metric name is provided, use it in the filter query
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

0 comments on commit 1cc5e3f

Please sign in to comment.