Skip to content

Commit

Permalink
feat: filter metric value using regex
Browse files Browse the repository at this point in the history
Signed-off-by: raed altuwaijri <[email protected]>
  • Loading branch information
raedtwj committed Jun 19, 2023
1 parent 08133ca commit 2fadfb1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func probeHandler(w http.ResponseWriter, r *http.Request, logger log.Logger, con

registry := prometheus.NewPedanticRegistry()

metrics, err := exporter.CreateMetricsList(config.Modules[module])
metrics, err := exporter.CreateMetricsList(config.Modules[module], logger)
if err != nil {
level.Error(logger).Log("msg", "Failed to create metrics list from config", "err", err)
}
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Metric struct {
EpochTimestamp string
Help string
Values map[string]string
Regex string
}

type ScrapeType string
Expand Down
10 changes: 9 additions & 1 deletion exporter/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package exporter
import (
"bytes"
"encoding/json"
"fmt"
"regexp"
"time"

"github.com/go-kit/log"
Expand All @@ -39,6 +41,7 @@ type JSONMetric struct {
LabelsJSONPaths []string
ValueType prometheus.ValueType
EpochTimestampJSONPath string
Regex *regexp.Regexp
}

func (mc JSONMetricCollector) Describe(ch chan<- *prometheus.Desc) {
Expand All @@ -56,7 +59,12 @@ func (mc JSONMetricCollector) Collect(ch chan<- prometheus.Metric) {
level.Error(mc.Logger).Log("msg", "Failed to extract value for metric", "path", m.KeyJSONPath, "err", err, "metric", m.Desc)
continue
}

if m.Regex != nil {
if value = m.Regex.FindString(value); value == "" {
level.Error(mc.Logger).Log("msg", fmt.Sprintf("No matching for this pattern '%s'", m.Regex.String()))
continue
}
}
if floatValue, err := SanitizeValue(value); err == nil {
metric := prometheus.MustNewConstMetric(
m.Desc,
Expand Down
12 changes: 11 additions & 1 deletion exporter/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"math"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"text/template"
Expand Down Expand Up @@ -74,7 +75,7 @@ func SanitizeIntValue(s string) (int64, error) {
return value, fmt.Errorf(resultErr)
}

func CreateMetricsList(c config.Module) ([]JSONMetric, error) {
func CreateMetricsList(c config.Module, logger log.Logger) ([]JSONMetric, error) {
var (
metrics []JSONMetric
valueType prometheus.ValueType
Expand All @@ -91,10 +92,18 @@ func CreateMetricsList(c config.Module) ([]JSONMetric, error) {
switch metric.Type {
case config.ValueScrape:
var variableLabels, variableLabelsValues []string
var err error
var re *regexp.Regexp
for k, v := range metric.Labels {
variableLabels = append(variableLabels, k)
variableLabelsValues = append(variableLabelsValues, v)
}
if metric.Regex != "" {
if re, err = regexp.Compile(metric.Regex); err != nil {
level.Error(logger).Log("msg", "invalid regex expression", "err", err)
continue
}
}
jsonMetric := JSONMetric{
Type: config.ValueScrape,
Desc: prometheus.NewDesc(
Expand All @@ -107,6 +116,7 @@ func CreateMetricsList(c config.Module) ([]JSONMetric, error) {
LabelsJSONPaths: variableLabelsValues,
ValueType: valueType,
EpochTimestampJSONPath: metric.EpochTimestamp,
Regex: re,
}
metrics = append(metrics, jsonMetric)
case config.ObjectScrape:
Expand Down

0 comments on commit 2fadfb1

Please sign in to comment.