Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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 @@ -121,7 +121,7 @@ func probeHandler(w http.ResponseWriter, r *http.Request, logger *slog.Logger, c

registry := prometheus.NewPedanticRegistry()

metrics, err := exporter.CreateMetricsList(config.Modules[module])
metrics, err := exporter.CreateMetricsList(config.Modules[module], logger)
if err != nil {
logger.Error("Failed to create metrics list from config", "err", err)
}
Expand Down
25 changes: 25 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,28 @@ func TestBodyPostQuery(t *testing.T) {
target.Close()
}
}
func TestInvalidRegexConfig(t *testing.T) {
_, err := config.LoadConfig("../test/config/invalidConfig.yml")
if err != nil {
return
}

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

c, err := config.LoadConfig("../test/config/invalidConfig.yml")
if err != nil {
t.Fatalf("Failed to load config file")
}

req := httptest.NewRequest("GET", "http://example.com/foo"+"?module=default&target="+target.URL+"/serve/good.json", nil)
recorder := httptest.NewRecorder()
logBuffer := strings.Builder{}
promslogConfig := &promslog.Config{Writer: &logBuffer}
logger := promslog.New(promslogConfig)
probeHandler(recorder, req, logger, c)

if logBuffer.Len() == 0 {
t.Fatal("Expected error log for invalid regex, but got none")
}
}
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
AllowMissingKey bool `yaml:"allow_missing_key,omitempty"`
}

Expand Down
7 changes: 7 additions & 0 deletions examples/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ 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
- name: example_missing_key
path: '{ .missing_key }'
allow_missing_key: true
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"
}
9 changes: 8 additions & 1 deletion exporter/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"bytes"
"encoding/json"
"log/slog"
"regexp"
"time"

"github.com/prometheus-community/json_exporter/config"
Expand All @@ -38,6 +39,7 @@ type JSONMetric struct {
LabelsJSONPaths []string
ValueType prometheus.ValueType
EpochTimestampJSONPath string
IncludeRegex *regexp.Regexp
AllowMissingKey bool
}

Expand All @@ -59,7 +61,12 @@ func (mc JSONMetricCollector) Collect(ch chan<- prometheus.Metric) {
if missing {
continue
}

if m.IncludeRegex != nil {
if value = m.IncludeRegex.FindString(value); value == "" {
mc.Logger.Error("No matching for this pattern", "pattern", 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 @@ -22,6 +22,7 @@ import (
"math"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"text/template"
Expand Down Expand Up @@ -73,7 +74,7 @@ func SanitizeIntValue(s string) (int64, error) {
return value, errors.New(resultErr)
}

func CreateMetricsList(c config.Module) ([]JSONMetric, error) {
func CreateMetricsList(c config.Module, logger *slog.Logger) ([]JSONMetric, error) {
var (
metrics []JSONMetric
valueType prometheus.ValueType
Expand All @@ -90,10 +91,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 {
logger.Error("Invalid regex expression", "err", err)
continue
}
}
jsonMetric := JSONMetric{
Type: config.ValueScrape,
Desc: prometheus.NewDesc(
Expand All @@ -106,6 +115,7 @@ func CreateMetricsList(c config.Module) ([]JSONMetric, error) {
LabelsJSONPaths: variableLabelsValues,
ValueType: valueType,
EpochTimestampJSONPath: metric.EpochTimestamp,
IncludeRegex: re,
AllowMissingKey: metric.AllowMissingKey,
}
metrics = append(metrics, jsonMetric)
Expand Down
6 changes: 6 additions & 0 deletions test/config/good.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ modules:
active: 1 # static value
count: '{.count}' # dynamic value
boolean: '{.some_boolean}'

- name: example_regex_value
path: '{.availableMemory}'
help: Example of a regex filtered value scrape in the json
valuetype: gauge
includeregex: ^\d*\.?\d*
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/good.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# HELP example_global_value Example of a top-level global value scrape in the json
# TYPE example_global_value gauge
example_global_value{environment="beta",location="planet-mars"} 1234
# HELP example_regex_value Example of a regex filtered value scrape in the json
# TYPE example_regex_value gauge
example_regex_value 5
# HELP example_value_active Example of sub-level value scrapes from a json
# TYPE example_value_active counter
example_value_active{environment="beta",id="id-A"} 1
Expand Down
3 changes: 2 additions & 1 deletion test/serve/good.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
"state": "ACTIVE"
}
],
"location": "mars"
"location": "mars",
"availableMemory": "5 MB"
}