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

Trim spaces from values #344

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func TestCorrectResponse(t *testing.T) {
}{
{"../test/config/good.yml", "/serve/good.json", "../test/response/good.txt", true},
{"../test/config/good.yml", "/serve/repeat-metric.json", "../test/response/good.txt", false},
{"../test/config/trim.yml", "/serve/trim.json", "../test/response/good.txt", false},
}

target := httptest.NewServer(http.FileServer(http.Dir("../test")))
Expand Down
12 changes: 12 additions & 0 deletions examples/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ modules:
values:
population: '{ .population }'

trim:
metrics:
- name: animal
type: object
help: Example of top-level lists in a separate module
path: '{ [*] }'
labels:
name: '{ .noun }'
predator: '{ .predator }'
values:
population: '{ .population }'

## HTTP connection configurations can be set in 'modules.<module_name>.http_client_config' field. For full http client config parameters, ref: https://pkg.go.dev/github.com/prometheus/common/config?tab=doc#HTTPClientConfig
#
# http_client_config:
Expand Down
25 changes: 25 additions & 0 deletions examples/trim-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"counter": 1234,
"timestamp": 1657568506,
"values": [
{
"id": " id-A",
"count": 1,
"some_boolean": true,
"state": " ACTIVE"
},
{
"id": "id-B",
"count": 2,
"some_boolean": true,
"state": " INACTIVE"
},
{
"id": "id-C",
"count": 3,
"some_boolean": false,
"state": " ACTIVE"
}
],
"location": "mars "
}
3 changes: 2 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"
"strings"
"time"

"github.com/prometheus-community/json_exporter/config"
Expand Down Expand Up @@ -144,7 +145,7 @@ func extractValue(logger *slog.Logger, data []byte, path string, enableJSONOutpu
return res, nil
}

return buf.String(), nil
return strings.TrimSpace(buf.String()), nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we always trim? Or add a boolean flag for trimming whitespace. 🤔

}

// Returns the list of labels created from the list of provided json paths
Expand Down
26 changes: 26 additions & 0 deletions test/config/trim.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
modules:
default:
metrics:
- name: example_global_value
path: "{ .counter }"
help: Example of a top-level global value scrape in the json
valuetype: gauge
labels:
environment: beta # static label
location: "planet-{.location}" # dynamic label

- name: example_value_trim
type: object
help: Example of sub-level value scrapes from a json
path: '{.values[?(@.state == "ACTIVE")]}'
valuetype: counter
trimvalues: true
labels:
environment: beta # static label
id: '{.id}' # dynamic label
values:
active: 1 # static value
count: '{.count}' # dynamic value
boolean: '{.some_boolean}'
float: '{.float}'
19 changes: 19 additions & 0 deletions test/response/trim.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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_value_trim_active Example of sub-level value scrapes from a json
# TYPE example_value_trim_active counter
example_value_trim_active{environment="beta",id="id-A"} 1
example_value_trim_active{environment="beta",id="id-C"} 1
# HELP example_value_trim_boolean Example of sub-level value scrapes from a json
# TYPE example_value_trim_boolean counter
example_value_trim_boolean{environment="beta",id="id-A"} 1
example_value_trim_boolean{environment="beta",id="id-C"} 0
# HELP example_value_trim_count Example of sub-level value scrapes from a json
# TYPE example_value_trim_count counter
example_value_trim_count{environment="beta",id="id-A"} 1
example_value_trim_count{environment="beta",id="id-C"} 3
# HELP example_value_count Example of sub-level value scrapes from a json
# TYPE example_value_count counter
example_value_float{environment="beta",id="id-A"} 3.1415
example_value_float{environment="beta",id="id-C"} 3.14
27 changes: 27 additions & 0 deletions test/serve/trim.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"counter": 1234,
"values": [
{
"id": "id-A",
"count": 1,
"some_boolean": true,
"state": " ACTIVE ",
"float": " 3.1415 "
},
{
"id": "id-B",
"count": 2,
"some_boolean": true,
"state": " INACTIVE",
"float": " 3.141 "
},
{
"id": "id-C",
"count": 3,
"some_boolean": false,
"state": " ACTIVE",
"float": " 3.14"
}
],
"location": " mars "
}