Skip to content

Commit

Permalink
Trim spaces from values
Browse files Browse the repository at this point in the history
Signed-off-by: Vasileios Tsaknis <[email protected]>
  • Loading branch information
vaionicle committed Nov 21, 2024
1 parent 97bfd95 commit fec07c8
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 1 deletion.
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 @@ -16,6 +16,7 @@ package exporter
import (
"bytes"
"encoding/json"
"strings"
"time"

"github.com/go-kit/log"
Expand Down Expand Up @@ -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
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 "
}

0 comments on commit fec07c8

Please sign in to comment.