Skip to content

Commit 04655fe

Browse files
committed
Trim spaces from values
Signed-off-by: Vasileios Tsaknis <[email protected]>
1 parent 97bfd95 commit 04655fe

File tree

7 files changed

+113
-1
lines changed

7 files changed

+113
-1
lines changed

cmd/main_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ func TestCorrectResponse(t *testing.T) {
138138
}{
139139
{"../test/config/good.yml", "/serve/good.json", "../test/response/good.txt", true},
140140
{"../test/config/good.yml", "/serve/repeat-metric.json", "../test/response/good.txt", false},
141+
{"../test/config/trim.yml", "/serve/trim.json", "../test/response/good.txt", false},
141142
}
142143

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

examples/config.yml

+12
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ modules:
4343
values:
4444
population: '{ .population }'
4545

46+
trim:
47+
metrics:
48+
- name: animal
49+
type: object
50+
help: Example of top-level lists in a separate module
51+
path: '{ [*] }'
52+
labels:
53+
name: '{ .noun }'
54+
predator: '{ .predator }'
55+
values:
56+
population: '{ .population }'
57+
4658
## 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
4759
#
4860
# http_client_config:

examples/trim-data.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"counter": 1234,
3+
"timestamp": 1657568506,
4+
"values": [
5+
{
6+
"id": " id-A",
7+
"count": 1,
8+
"some_boolean": true,
9+
"state": " ACTIVE"
10+
},
11+
{
12+
"id": "id-B",
13+
"count": 2,
14+
"some_boolean": true,
15+
"state": " INACTIVE"
16+
},
17+
{
18+
"id": "id-C",
19+
"count": 3,
20+
"some_boolean": false,
21+
"state": " ACTIVE"
22+
}
23+
],
24+
"location": "mars "
25+
}

exporter/collector.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ package exporter
1616
import (
1717
"bytes"
1818
"encoding/json"
19+
"log/slog"
20+
"strings"
1921
"time"
2022

2123
"github.com/go-kit/log"
@@ -145,7 +147,7 @@ func extractValue(logger log.Logger, data []byte, path string, enableJSONOutput
145147
return res, nil
146148
}
147149

148-
return buf.String(), nil
150+
return strings.TrimSpace(buf.String()), nil
149151
}
150152

151153
// Returns the list of labels created from the list of provided json paths

test/config/trim.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
modules:
3+
default:
4+
metrics:
5+
- name: example_global_value
6+
path: "{ .counter }"
7+
help: Example of a top-level global value scrape in the json
8+
valuetype: gauge
9+
labels:
10+
environment: beta # static label
11+
location: "planet-{.location}" # dynamic label
12+
13+
- name: example_value_trim
14+
type: object
15+
help: Example of sub-level value scrapes from a json
16+
path: '{.values[?(@.state == "ACTIVE")]}'
17+
valuetype: counter
18+
trimvalues: true
19+
labels:
20+
environment: beta # static label
21+
id: '{.id}' # dynamic label
22+
values:
23+
active: 1 # static value
24+
count: '{.count}' # dynamic value
25+
boolean: '{.some_boolean}'
26+
float: '{.float}'

test/response/trim.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# HELP example_global_value Example of a top-level global value scrape in the json
2+
# TYPE example_global_value gauge
3+
example_global_value{environment="beta",location="planet-mars"} 1234
4+
# HELP example_value_trim_active Example of sub-level value scrapes from a json
5+
# TYPE example_value_trim_active counter
6+
example_value_trim_active{environment="beta",id="id-A"} 1
7+
example_value_trim_active{environment="beta",id="id-C"} 1
8+
# HELP example_value_trim_boolean Example of sub-level value scrapes from a json
9+
# TYPE example_value_trim_boolean counter
10+
example_value_trim_boolean{environment="beta",id="id-A"} 1
11+
example_value_trim_boolean{environment="beta",id="id-C"} 0
12+
# HELP example_value_trim_count Example of sub-level value scrapes from a json
13+
# TYPE example_value_trim_count counter
14+
example_value_trim_count{environment="beta",id="id-A"} 1
15+
example_value_trim_count{environment="beta",id="id-C"} 3
16+
# HELP example_value_count Example of sub-level value scrapes from a json
17+
# TYPE example_value_count counter
18+
example_value_float{environment="beta",id="id-A"} 3.1415
19+
example_value_float{environment="beta",id="id-C"} 3.14

test/serve/trim.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"counter": 1234,
3+
"values": [
4+
{
5+
"id": "id-A",
6+
"count": 1,
7+
"some_boolean": true,
8+
"state": " ACTIVE ",
9+
"float": " 3.1415 "
10+
},
11+
{
12+
"id": "id-B",
13+
"count": 2,
14+
"some_boolean": true,
15+
"state": " INACTIVE",
16+
"float": " 3.141 "
17+
},
18+
{
19+
"id": "id-C",
20+
"count": 3,
21+
"some_boolean": false,
22+
"state": " ACTIVE",
23+
"float": " 3.14"
24+
}
25+
],
26+
"location": " mars "
27+
}

0 commit comments

Comments
 (0)