2015-05-01 23:48:34 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestVarName(t *testing.T) {
|
|
|
|
v := VarName("memstats.Alloc")
|
|
|
|
|
|
|
|
slice := v.ToSlice()
|
|
|
|
if len(slice) != 2 || slice[0] != "memstats" || slice[1] != "Alloc" {
|
|
|
|
t.Fatalf("ToSlice failed: %v", slice)
|
|
|
|
}
|
2015-05-02 01:12:23 +08:00
|
|
|
|
|
|
|
short := v.Short()
|
|
|
|
if short != "Alloc" {
|
|
|
|
t.Fatalf("Expecting Short() to be 'Alloc', but got: %s", short)
|
|
|
|
}
|
2015-05-02 15:12:38 +08:00
|
|
|
|
|
|
|
kind := v.Kind()
|
|
|
|
if kind != KindDefault {
|
|
|
|
t.Fatalf("Expecting kind to be %v, but got: %v", KindDefault, kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
v = VarName("mem:memstats.Alloc")
|
|
|
|
|
|
|
|
slice = v.ToSlice()
|
|
|
|
if len(slice) != 2 || slice[0] != "memstats" || slice[1] != "Alloc" {
|
|
|
|
t.Fatalf("ToSlice failed: %v", slice)
|
|
|
|
}
|
|
|
|
|
|
|
|
short = v.Short()
|
|
|
|
if short != "Alloc" {
|
|
|
|
t.Fatalf("Expecting Short() to be 'Alloc', but got: %s", short)
|
|
|
|
}
|
|
|
|
|
|
|
|
kind = v.Kind()
|
|
|
|
if kind != KindMemory {
|
|
|
|
t.Fatalf("Expecting kind to be %v, but got: %v", KindMemory, kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
v = VarName("duration:ResponseTimes.API.Users")
|
|
|
|
kind = v.Kind()
|
|
|
|
if kind != KindDuration {
|
|
|
|
t.Fatalf("Expecting kind to be %v, but got: %v", KindDuration, kind)
|
|
|
|
}
|
allow escaping the dot character in paths using a backslash
Occasionally expvar output contains keys that are hostnames,
IP addresses, or filenames that contain the "." character. For
example:
{
"bleve": {
"bootDuration": 16559,
"indexes": {
"bench.bleve": {
"index": {
"analysis_time": 10889841135,
"batches": 145,
"deletes": 0,
"errors": 0,
"index_time": 21277401883,
"lookup_queue_len": 0,
"updates": 14500
},
"search_time": 0,
"searches": 0
}
}
}
...
}
I can now chart the lookup_queue_len value using the var:
bleve.indexes.bench\.bleve.index.lookup_queue_len
This partially addresses #11 (still does not escape colon character).
2015-12-02 05:53:13 +08:00
|
|
|
|
|
|
|
// single \. escapes the dot
|
|
|
|
v = VarName(`bleve.indexes.bench\.bleve.index.lookup_queue_len`)
|
|
|
|
|
|
|
|
slice = v.ToSlice()
|
|
|
|
if len(slice) != 5 || slice[0] != "bleve" || slice[1] != "indexes" || slice[2] != "bench.bleve" ||
|
|
|
|
slice[3] != "index" || slice[4] != "lookup_queue_len" {
|
|
|
|
t.Fatalf("ToSlice failed: %v", slice)
|
|
|
|
}
|
|
|
|
|
|
|
|
// double \\. escapes backslash, not dot
|
|
|
|
v = VarName(`bleve.indexes.bench\\.bleve.index.lookup_queue_len`)
|
|
|
|
|
|
|
|
slice = v.ToSlice()
|
|
|
|
if len(slice) != 6 || slice[0] != "bleve" || slice[1] != "indexes" || slice[2] != "bench\\" ||
|
|
|
|
slice[3] != "bleve" || slice[4] != "index" || slice[5] != "lookup_queue_len" {
|
|
|
|
t.Fatalf("ToSlice failed: %v", slice)
|
|
|
|
}
|
|
|
|
|
|
|
|
// triple \\\. escapes backslash then dot
|
|
|
|
v = VarName(`bleve.indexes.bench\\\.bleve.index.lookup_queue_len`)
|
|
|
|
|
|
|
|
slice = v.ToSlice()
|
|
|
|
if len(slice) != 5 || slice[0] != "bleve" || slice[1] != "indexes" || slice[2] != "bench\\.bleve" ||
|
|
|
|
slice[3] != "index" || slice[4] != "lookup_queue_len" {
|
|
|
|
t.Fatalf("ToSlice failed: %v", slice)
|
|
|
|
}
|
|
|
|
|
|
|
|
// quadruple \\\\. escapes two backslashes, not dot
|
|
|
|
v = VarName(`bleve.indexes.bench\\\\.bleve.index.lookup_queue_len`)
|
|
|
|
|
|
|
|
slice = v.ToSlice()
|
|
|
|
if len(slice) != 6 || slice[0] != "bleve" || slice[1] != "indexes" || slice[2] != "bench\\\\" ||
|
|
|
|
slice[3] != "bleve" || slice[4] != "index" || slice[5] != "lookup_queue_len" {
|
|
|
|
t.Fatalf("ToSlice failed: %v", slice)
|
|
|
|
}
|
|
|
|
|
|
|
|
// unsupported \x passes through unaltered
|
|
|
|
v = VarName(`bleve.indexes.bench\xbleve.index.lookup_queue_len`)
|
|
|
|
|
|
|
|
slice = v.ToSlice()
|
|
|
|
if len(slice) != 5 || slice[0] != "bleve" || slice[1] != "indexes" || slice[2] != "bench\\xbleve" ||
|
|
|
|
slice[3] != "index" || slice[4] != "lookup_queue_len" {
|
|
|
|
t.Fatalf("ToSlice failed: %v", slice)
|
|
|
|
}
|
2015-05-01 23:48:34 +08:00
|
|
|
}
|