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: filter metric value using regex #240

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ $ curl "http://localhost:7979/probe?module=default&target=http://localhost:8000/
# HELP example_global_value Example of a top-level global value scrape in the json
# TYPE example_global_value untyped
example_global_value{environment="beta",location="planet-mars"} 1234
# HELP example_regex_value Example of a regex value scrapes in the json
# TYPE example_regex_value gauge
example_regex_value{environment="beta"} 1000
# HELP example_timestamped_value_count Example of a timestamped value scrape in the json
# TYPE example_timestamped_value_count untyped
example_timestamped_value_count{environment="beta"} 2
Expand Down
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
37 changes: 37 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,40 @@ func TestBodyPostQuery(t *testing.T) {
target.Close()
}
}
func TestRegexResponse(t *testing.T) {
tests := []struct {
name string
ConfigFile string
ServeFile string
ResponseFile string
ShouldSucceed bool
}{
{"case1_testCorrectResponse", "../test/config/config.yml", "/serve/correctGot.json", "../test/response/expected.txt", true},
{"case2_testFailResponse", "../test/config/config.yml", "/serve/failGot.json", "../test/response/expected.txt", false},
{"case3_testInvalidRegex", "../test/config/invalidConfig.yml", "/serve/correctGot.json", "../test/response/expected.txt", false},
{"case4_testNullVaule", "../test/config/config.yml", "/serve/nullVaule.json", "../test/response/expected.txt", false},
}

target := httptest.NewServer(http.FileServer(http.Dir("../test")))
defer target.Close()

for i, test := range tests {
c, err := config.LoadConfig(test.ConfigFile)
if err != nil {
t.Fatalf("Failed to load config file %s", test.ConfigFile)
}

req := httptest.NewRequest("GET", "http://example.com/foo"+"?module=default&target="+target.URL+test.ServeFile, nil)
recorder := httptest.NewRecorder()
probeHandler(recorder, req, log.NewNopLogger(), c)

resp := recorder.Result()
body, _ := io.ReadAll(resp.Body)

expected, _ := os.ReadFile(test.ResponseFile)

if test.ShouldSucceed && cap(body) != cap(expected) {
t.Fatalf("Correct response validation test %d fails unexpectedly.\nGOT:\n%s\nEXPECTED:\n%s", i, body, expected)
}
}
}
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
IncludeRegex string
}

type ScrapeType string
Expand Down
8 changes: 8 additions & 0 deletions examples/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ modules:
active: 1 # static value
count: '{.count}' # dynamic value
boolean: '{.some_boolean}'
- name: example_regex_value
valuetype: gauge
help: Example of a regex value scrapes in the json
path: '{ .available_memory }'
labels:
environment: beta # static label
regex: ^\d*\.?\d* # only match digits


animals:
metrics:
Expand Down
3 changes: 2 additions & 1 deletion examples/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
"state": "ACTIVE"
}
],
"location": "mars"
"location": "mars",
"available_memory": "1000 MB"
}
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
IncludeRegex *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.IncludeRegex != nil {
if value = m.IncludeRegex.FindString(value); value == "" {
level.Error(mc.Logger).Log("msg", fmt.Sprintf("No matching for this pattern '%s'", m.IncludeRegex.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.IncludeRegex != "" {
if re, err = regexp.Compile(metric.IncludeRegex); 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,
IncludeRegex: re,
}
metrics = append(metrics, jsonMetric)
case config.ObjectScrape:
Expand Down
9 changes: 9 additions & 0 deletions test/config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
modules:
default:
metrics:
- name: available_memory
path: '{.availableMemory}'
help: Available memory in MB
valuetype: gauge
includeregex: ^\d*\.?\d* # only match digits
9 changes: 9 additions & 0 deletions test/config/invalidConfig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
modules:
default:
metrics:
- name: available_memory
path: '{.availableMemory}'
help: Available memory in MB
valuetype: gauge
includeregex: ^\d*\.?\d*) # Invalid regular expression, parentheses do not match
3 changes: 3 additions & 0 deletions test/response/expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# HELP available_memory Available memory in MB
# TYPE available_memory gauge
available_memory 5
3 changes: 3 additions & 0 deletions test/serve/correctGot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"availableMemory": "5 MB"
}
5 changes: 5 additions & 0 deletions test/serve/failGot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"availableMemory": "1000 MB",
"availableMemory": " 50 GB",
"availableMemory": "1.333285ms"
}
3 changes: 3 additions & 0 deletions test/serve/nullVaule.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"availableMemory": ""
}