Skip to content
This repository was archived by the owner on Oct 21, 2020. It is now read-only.

Commit 0644c40

Browse files
authored
Merge pull request #16 from go-redis/fix/rediscensus
Move OpenCensus instrumentation to a sub-package
2 parents 0538196 + 4c20821 commit 0644c40

File tree

8 files changed

+358
-225
lines changed

8 files changed

+358
-225
lines changed

cmdutil/cmd.go

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
package cmdutil
2+
3+
import (
4+
"encoding/hex"
5+
"fmt"
6+
"strconv"
7+
"strings"
8+
"time"
9+
10+
"github.com/go-redis/redis/v8"
11+
"github.com/go-redis/redisext/internal"
12+
)
13+
14+
func CmdString(cmd redis.Cmder) string {
15+
b := make([]byte, 0, 32)
16+
b = AppendCmd(b, cmd)
17+
return internal.String(b)
18+
}
19+
20+
func CmdsString(cmds []redis.Cmder) (string, string) {
21+
const numCmdLimit = 100
22+
const numNameLimit = 10
23+
24+
seen := make(map[string]struct{}, numNameLimit)
25+
unqNames := make([]string, 0, numNameLimit)
26+
27+
b := make([]byte, 0, 32*len(cmds))
28+
29+
for i, cmd := range cmds {
30+
if i > numCmdLimit {
31+
break
32+
}
33+
34+
if i > 0 {
35+
b = append(b, '\n')
36+
}
37+
b = AppendCmd(b, cmd)
38+
39+
if len(unqNames) >= numNameLimit {
40+
continue
41+
}
42+
43+
name := cmd.FullName()
44+
if _, ok := seen[name]; !ok {
45+
seen[name] = struct{}{}
46+
unqNames = append(unqNames, name)
47+
}
48+
}
49+
50+
summary := strings.Join(unqNames, " ")
51+
return summary, internal.String(b)
52+
}
53+
54+
func AppendCmd(b []byte, cmd redis.Cmder) []byte {
55+
const numArgLimit = 32
56+
57+
for i, arg := range cmd.Args() {
58+
if i > numArgLimit {
59+
break
60+
}
61+
if i > 0 {
62+
b = append(b, ' ')
63+
}
64+
b = appendArg(b, arg)
65+
}
66+
67+
if err := cmd.Err(); err != nil {
68+
b = append(b, ": "...)
69+
b = append(b, err.Error()...)
70+
}
71+
72+
return b
73+
}
74+
75+
func appendArg(b []byte, v interface{}) []byte {
76+
const argLenLimit = 64
77+
78+
switch v := v.(type) {
79+
case nil:
80+
return append(b, "<nil>"...)
81+
case string:
82+
if len(v) > argLenLimit {
83+
v = v[:argLenLimit]
84+
}
85+
return appendUTF8String(b, internal.Bytes(v))
86+
case []byte:
87+
if len(v) > argLenLimit {
88+
v = v[:argLenLimit]
89+
}
90+
return appendUTF8String(b, v)
91+
case int:
92+
return strconv.AppendInt(b, int64(v), 10)
93+
case int8:
94+
return strconv.AppendInt(b, int64(v), 10)
95+
case int16:
96+
return strconv.AppendInt(b, int64(v), 10)
97+
case int32:
98+
return strconv.AppendInt(b, int64(v), 10)
99+
case int64:
100+
return strconv.AppendInt(b, v, 10)
101+
case uint:
102+
return strconv.AppendUint(b, uint64(v), 10)
103+
case uint8:
104+
return strconv.AppendUint(b, uint64(v), 10)
105+
case uint16:
106+
return strconv.AppendUint(b, uint64(v), 10)
107+
case uint32:
108+
return strconv.AppendUint(b, uint64(v), 10)
109+
case uint64:
110+
return strconv.AppendUint(b, v, 10)
111+
case float32:
112+
return strconv.AppendFloat(b, float64(v), 'f', -1, 64)
113+
case float64:
114+
return strconv.AppendFloat(b, v, 'f', -1, 64)
115+
case bool:
116+
if v {
117+
return append(b, "true"...)
118+
}
119+
return append(b, "false"...)
120+
case time.Time:
121+
return v.AppendFormat(b, time.RFC3339Nano)
122+
default:
123+
return append(b, fmt.Sprint(v)...)
124+
}
125+
}
126+
127+
func appendUTF8String(dst []byte, src []byte) []byte {
128+
if isSimple(src) {
129+
dst = append(dst, src...)
130+
return dst
131+
}
132+
133+
s := len(dst)
134+
dst = append(dst, make([]byte, hex.EncodedLen(len(src)))...)
135+
hex.Encode(dst[s:], src)
136+
return dst
137+
}
138+
139+
func isSimple(b []byte) bool {
140+
for _, c := range b {
141+
if !isSimpleByte(c) {
142+
return false
143+
}
144+
}
145+
return true
146+
}
147+
148+
func isSimpleByte(c byte) bool {
149+
return simple[c]
150+
}
151+
152+
var simple = [256]bool{
153+
'-': true,
154+
'+': true,
155+
'_': true,
156+
':': true,
157+
158+
'0': true,
159+
'1': true,
160+
'2': true,
161+
'3': true,
162+
'4': true,
163+
'5': true,
164+
'6': true,
165+
'7': true,
166+
'8': true,
167+
'9': true,
168+
169+
'a': true,
170+
'b': true,
171+
'c': true,
172+
'd': true,
173+
'e': true,
174+
'f': true,
175+
'g': true,
176+
'h': true,
177+
'i': true,
178+
'j': true,
179+
'k': true,
180+
'l': true,
181+
'm': true,
182+
'n': true,
183+
'o': true,
184+
'p': true,
185+
'q': true,
186+
'r': true,
187+
's': true,
188+
't': true,
189+
'u': true,
190+
'v': true,
191+
'w': true,
192+
'x': true,
193+
'y': true,
194+
'z': true,
195+
196+
'A': true,
197+
'B': true,
198+
'C': true,
199+
'D': true,
200+
'E': true,
201+
'F': true,
202+
'G': true,
203+
'H': true,
204+
'I': true,
205+
'J': true,
206+
'K': true,
207+
'L': true,
208+
'M': true,
209+
'N': true,
210+
'O': true,
211+
'P': true,
212+
'Q': true,
213+
'R': true,
214+
'S': true,
215+
'T': true,
216+
'U': true,
217+
'V': true,
218+
'W': true,
219+
'X': true,
220+
'Y': true,
221+
'Z': true,
222+
}

internal_test.go renamed to cmdutil/cmd_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package redisext
1+
package cmdutil
22

33
import (
44
"testing"
@@ -13,7 +13,7 @@ func TestGinkgo(t *testing.T) {
1313
RunSpecs(t, "redisext")
1414
}
1515

16-
var _ = Describe("appendArg", func() {
16+
var _ = Describe("AppendArg", func() {
1717
DescribeTable("...",
1818
func(src string, wanted string) {
1919
b := appendArg(nil, src)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ require (
77
github.com/onsi/ginkgo v1.14.1
88
github.com/onsi/gomega v1.10.2
99
go.opentelemetry.io/otel v0.13.0
10+
golang.org/x/text v0.3.3 // indirect
1011
)

go.sum

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cu
77
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
88
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
99
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
10-
github.com/go-redis/redis/v8 v8.2.3 h1:eNesND+DWt/sjQOtPFxAbQkTIXaXX00qNLxjVWkZ70k=
11-
github.com/go-redis/redis/v8 v8.2.3/go.mod h1:ysgGY09J/QeDYbu3HikWEIPCwaeOkuNoTgKayTEaEOw=
1210
github.com/go-redis/redis/v8 v8.3.0 h1:Xrwvn8+QqUYD1MbQmda3cVR2U9li5XbtRFkKZN5Y0hk=
1311
github.com/go-redis/redis/v8 v8.3.0/go.mod h1:a2xkpBM7NJUN5V5kiF46X5Ltx4WeXJ9757X/ScKUBdE=
1412
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -22,8 +20,6 @@ github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw
2220
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
2321
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
2422
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
25-
github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=
26-
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
2723
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
2824
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
2925
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
@@ -42,8 +38,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
4238
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4339
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
4440
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
45-
go.opentelemetry.io/otel v0.11.0 h1:IN2tzQa9Gc4ZVKnTaMbPVcHjvzOdg5n9QfnmlqiET7E=
46-
go.opentelemetry.io/otel v0.11.0/go.mod h1:G8UCk+KooF2HLkgo8RHX9epABH/aRGYET7gQOqBVdB0=
4741
go.opentelemetry.io/otel v0.13.0 h1:2isEnyzjjJZq6r2EKMsFj4TxiQiexsM04AVhwbR/oBA=
4842
go.opentelemetry.io/otel v0.13.0/go.mod h1:dlSNewoRYikTkotEnxdmuBHgzT+k/idJSfDv/FxEnOY=
4943
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@@ -62,6 +56,8 @@ golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7w
6256
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
6357
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
6458
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
59+
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
60+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
6561
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
6662
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
6763
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 commit comments

Comments
 (0)