diff --git a/cmd/main_test.go b/cmd/main_test.go index 047648b3..b0055a76 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -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"))) diff --git a/examples/config.yml b/examples/config.yml index 9d0745c0..cfa84cc2 100644 --- a/examples/config.yml +++ b/examples/config.yml @@ -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..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: diff --git a/examples/trim-data.json b/examples/trim-data.json new file mode 100644 index 00000000..60ca71f7 --- /dev/null +++ b/examples/trim-data.json @@ -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 " +} diff --git a/exporter/collector.go b/exporter/collector.go index 4effc10f..a297268e 100644 --- a/exporter/collector.go +++ b/exporter/collector.go @@ -16,6 +16,7 @@ package exporter import ( "bytes" "encoding/json" + "strings" "time" "github.com/go-kit/log" @@ -145,7 +146,7 @@ func extractValue(logger log.Logger, data []byte, path string, enableJSONOutput return res, nil } - return buf.String(), nil + return strings.TrimSpace(buf.String()), nil } // Returns the list of labels created from the list of provided json paths diff --git a/test/config/trim.yml b/test/config/trim.yml new file mode 100644 index 00000000..84f18861 --- /dev/null +++ b/test/config/trim.yml @@ -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}' diff --git a/test/response/trim.txt b/test/response/trim.txt new file mode 100644 index 00000000..b05fc495 --- /dev/null +++ b/test/response/trim.txt @@ -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 \ No newline at end of file diff --git a/test/serve/trim.json b/test/serve/trim.json new file mode 100644 index 00000000..7aa61a33 --- /dev/null +++ b/test/serve/trim.json @@ -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 " +}