Skip to content

Commit f1fbb14

Browse files
authored
Merge pull request #210 from msaf1980/fix_render_unescape
render: revert unescape
2 parents ce030f7 + ff0b741 commit f1fbb14

File tree

7 files changed

+141
-7
lines changed

7 files changed

+141
-7
lines changed

cmd/e2e-test/clickhouse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (c *Clickhouse) Query(sql string) (string, error) {
141141
return string(msg), nil
142142
}
143143

144-
func (c *Clickhouse) IsUp() bool {
144+
func (c *Clickhouse) Alive() bool {
145145
if len(c.container) == 0 {
146146
return false
147147
}

cmd/e2e-test/e2etesting.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,12 @@ func testGraphiteClickhouse(test *TestSchema, clickhouse *Clickhouse, testDir, r
452452
)
453453
time.Sleep(500 * time.Millisecond)
454454
for i := 200; i < 2000; i += 200 {
455-
if clickhouse.IsUp() {
455+
if clickhouse.Alive() {
456456
break
457457
}
458458
time.Sleep(time.Duration(i) * time.Millisecond)
459459
}
460-
if !clickhouse.IsUp() {
460+
if !clickhouse.Alive() {
461461
logger.Error("starting clickhouse",
462462
zap.String("config", test.name),
463463
zap.Any("clickhouse version", clickhouse.Version),

cmd/graphite-clickhouse-client/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,16 @@ func main() {
5858
now := time.Now()
5959

6060
from := datetime.DateParamToEpoch(*fromStr, tz, now, 0)
61-
if from == 0 {
61+
if from == 0 && len(targets) > 0 {
6262
fmt.Printf("invalid from: %s\n", *fromStr)
6363
os.Exit(1)
6464
}
6565
var until int64
66-
if *untilStr == "" {
66+
if *untilStr == "" && len(targets) > 0 {
6767
*untilStr = "now"
6868
}
6969
until = datetime.DateParamToEpoch(*untilStr, tz, now, 0)
70-
if until == 0 {
70+
if until == 0 && len(targets) > 0 {
7171
fmt.Printf("invalid until: %s\n", *untilStr)
7272
os.Exit(1)
7373
}

finder/tagged.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,10 @@ func tagsParse(path string) (string, []string, error) {
474474
return name, nil, fmt.Errorf("incomplete tags in '%s'", path)
475475
}
476476
tags := strings.Split(args, "&")
477-
return name, tags, nil
477+
for i := range tags {
478+
tags[i] = unescape(tags[i])
479+
}
480+
return unescape(name), tags, nil
478481
}
479482

480483
func TaggedDecode(v []byte) []byte {

finder/tagged_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package finder
22

33
import (
44
"fmt"
5+
"net/url"
56
"strconv"
67
"testing"
78
"time"
@@ -315,3 +316,41 @@ func TestTaggedFinder_whereFilter(t *testing.T) {
315316
})
316317
}
317318
}
319+
320+
func TestTaggedFinder_Abs(t *testing.T) {
321+
tests := []struct {
322+
name string
323+
v []byte
324+
cached bool
325+
want []byte
326+
}{
327+
{
328+
name: "cached",
329+
v: []byte("test_metric;colon=:;forward=/;hash=#;host=127.0.0.1;minus=-;percent=%;plus=+;underscore=_"),
330+
cached: true,
331+
want: []byte("test_metric;colon=:;forward=/;hash=#;host=127.0.0.1;minus=-;percent=%;plus=+;underscore=_"),
332+
},
333+
{
334+
name: "escaped",
335+
v: []byte(url.QueryEscape("instance:cpu_utilization?ratio_avg") +
336+
"?" + url.QueryEscape("dc") + "=" + url.QueryEscape("qwe+1") +
337+
"&" + url.QueryEscape("fqdn") + "=" + url.QueryEscape("asd&a") +
338+
"&" + url.QueryEscape("instance") + "=" + url.QueryEscape("10.33.10.10:9100") +
339+
"&" + url.QueryEscape("job") + "=" + url.QueryEscape("node&a")),
340+
want: []byte("instance:cpu_utilization?ratio_avg;dc=qwe+1;fqdn=asd&a;instance=10.33.10.10:9100;job=node&a"),
341+
},
342+
}
343+
for _, tt := range tests {
344+
t.Run(tt.name, func(t *testing.T) {
345+
var tf *TaggedFinder
346+
if tt.cached {
347+
tf = NewCachedTags(nil)
348+
} else {
349+
tf = NewTagged("http:/127.0.0.1:8123", "graphite_tags", true, false, clickhouse.Options{}, nil)
350+
}
351+
if got := string(tf.Abs(tt.v)); got != string(tt.want) {
352+
t.Errorf("TaggedDecode() =\n%q\nwant\n%q", got, string(tt.want))
353+
}
354+
})
355+
}
356+
}

finder/unescape.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package finder
2+
3+
import "strings"
4+
5+
func ishex(c byte) bool {
6+
switch {
7+
case '0' <= c && c <= '9':
8+
return true
9+
case 'a' <= c && c <= 'f':
10+
return true
11+
case 'A' <= c && c <= 'F':
12+
return true
13+
}
14+
return false
15+
}
16+
17+
func unhex(c byte) byte {
18+
switch {
19+
case '0' <= c && c <= '9':
20+
return c - '0'
21+
case 'a' <= c && c <= 'f':
22+
return c - 'a' + 10
23+
case 'A' <= c && c <= 'F':
24+
return c - 'A' + 10
25+
}
26+
return 0
27+
}
28+
29+
func isPercentEscape(s string, i int) bool {
30+
return i+2 < len(s) && ishex(s[i+1]) && ishex(s[i+2])
31+
}
32+
33+
// unescape unescapes a string.
34+
func unescape(s string) string {
35+
first := strings.IndexByte(s, '%')
36+
if first == -1 {
37+
return s
38+
}
39+
var t strings.Builder
40+
t.Grow(len(s))
41+
t.WriteString(s[:first])
42+
43+
LOOP:
44+
for i := first; i < len(s); i++ {
45+
switch s[i] {
46+
case '%':
47+
if len(s) < i+3 {
48+
t.WriteString(s[i:])
49+
break LOOP
50+
}
51+
if !isPercentEscape(s, i) {
52+
t.WriteString(s[i : i+3])
53+
} else {
54+
t.WriteByte(unhex(s[i+1])<<4 | unhex(s[i+2]))
55+
}
56+
i += 2
57+
default:
58+
t.WriteByte(s[i])
59+
}
60+
}
61+
62+
return t.String()
63+
}

tests/one_table/test.toml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ points = [{value = 0.5, time = "rnow-30"}, {value = 1.5, time = "rnow-20"}, {val
5454
name = "metric2;tag2=value21;tag4=value4"
5555
points = [{value = 2.0, time = "rnow-30"}, {value = 1.0, time = "rnow-20"}, {value = 0.0, time = "rnow-10"}, {value = 1.0, time = "rnow"}]
5656

57+
[[test.input]]
58+
name = "test_metric;minus=-;plus=+;percent=%;underscore=_;colon=:;hash=#;forward=/;host=127.0.0.1"
59+
points = [{value = 2.1, time = "rnow-30"}, {value = 0.1, time = "rnow-20"}, {value = 0.2, time = "rnow-10"}, {value = 1.5, time = "rnow"}]
60+
5761
######################################
5862
# Check metrics find
5963

@@ -87,6 +91,12 @@ result = [
8791
"metric1",
8892
]
8993

94+
[[test.tags_checks]]
95+
query = "colon;percent=%"
96+
result = [
97+
":",
98+
]
99+
90100
# End - Check tags autocomplete
91101
##########################################################################
92102
# Plain metrics (carbonapi_v3_pb)
@@ -328,6 +338,25 @@ stop = "rnow+10"
328338
step = 10
329339
values = [0.0, 1.0]
330340

341+
# End - Tagged metrics (pickle)
342+
##########################################################################
343+
# Unescape
344+
345+
[[test.render_checks]]
346+
formats = [ "protobuf", "carbonapi_v2_pb" ]
347+
from = "rnow-10"
348+
until = "rnow+1"
349+
targets = [
350+
"seriesByTag('percent=%')",
351+
]
352+
353+
[[test.render_checks.result]]
354+
name = "test_metric;colon=:;forward=/;hash=#;host=127.0.0.1;minus=-;percent=%;plus=+;underscore=_"
355+
start = "rnow-10"
356+
stop = "rnow+10"
357+
step = 10
358+
values = [0.2, 1.5]
359+
331360
# End - Tagged metrics (pickle)
332361
##########################################################################
333362
# Midnight

0 commit comments

Comments
 (0)